No description
Find a file
2022-08-31 17:22:40 +03:00
.github/workflows Create docs.yml 2022-08-31 16:06:06 +03:00
src docs fix 2022-08-31 16:08:41 +03:00
tests add docs 2022-08-31 14:10:52 +03:00
.gitignore add array example 2022-08-31 15:34:47 +03:00
LICENSE initial 2022-08-31 13:11:40 +03:00
README.md Update README.md 2022-08-31 17:22:40 +03:00
tlv.nimble 1.0.0 2022-08-31 16:07:09 +03:00

nim-tlv

Simplified TLV building and parsing in nim. Currently supported types:

  • int32's add 4 bytes
  • chars add 1 byte
  • strings add 4 bytes (int32 length) + 1 byte x string len (string chars)
  • booleans add 1 byte
  • more types coming soon

install

nimble install tlv

docs

https://d4rckh.github.io/nim-tlv/tlv

examples

simple values

import tlv

var b = initBuilder()

b.addInt32(10)
b.addChar('A')
b.addString("hello world")
b.addBool(true)

echo b.buf

var p = initParser()
p.setBuffer(b.buf)

echo p.extractInt32() # => 10
echo p.extractChar() # => A
echo p.extractString() # => hello world
echo p.extractBool() # => true

arrays

import tlv

var b = initBuilder()

### build the array

proc addUser*(b: Builder, username: string, age: int32) =
  b.addInt32(age)
  b.addString(username)

# set the length of the array
b.addInt32(5)

# add each user
b.addUser("user1", 30)
b.addUser("user2", 20)
b.addUser("user3", 21)
b.addUser("user4", 19)
b.addUser("user5", 27)

### parse the array

var p = initParser()
p.setBuffer(b.buf)

type User = object
  username: string
  age: int32

proc extractUser*(parser: Parser): User =
  result.age = parser.extractInt32()
  result.username = parser.extractString()

let usersLen = p.extractInt32()
var users = newSeqOfCap[User](usersLen)

for _ in 1..(usersLen):
  users.add(p.extractUser())

for user in users:
  echo "username: " & user.username & "; age: " & $user.age