mirror of
https://github.com/status-im/nim-rlp.git
synced 2025-02-13 14:36:41 +00:00
commit
70e6bf714b
@ -25,5 +25,6 @@ before_install:
|
||||
export PATH=$PWD/bin:$PATH
|
||||
cd ..
|
||||
script:
|
||||
- nimble install -y
|
||||
- nimble test
|
||||
|
||||
|
9
rlp.nim
9
rlp.nim
@ -168,7 +168,7 @@ proc isInt*(self: Rlp): bool =
|
||||
return bytes[offset] != 0
|
||||
return false
|
||||
|
||||
template maxBytes*(o: typedesc[Ordinal]): int = sizeof(o)
|
||||
template maxBytes*(o: typedesc[Ordinal | uint64 | uint]): int = sizeof(o)
|
||||
|
||||
proc toInt*(self: Rlp, IntType: typedesc): IntType =
|
||||
# XXX: self insertions are not working in generic procs
|
||||
@ -184,7 +184,7 @@ proc toInt*(self: Rlp, IntType: typedesc): IntType =
|
||||
raise newException(BadCastError, "")
|
||||
|
||||
for i in payloadStart ..< (payloadStart + payloadSize):
|
||||
result = (result shl 8) or int(self.bytes[self.position + i])
|
||||
result = cast[IntType](result shl 8) or cast[IntType](self.bytes[self.position + i])
|
||||
|
||||
proc toString*(self: Rlp): string =
|
||||
if not isBlob():
|
||||
@ -266,8 +266,8 @@ proc read*(rlp: var Rlp, T: type string): string =
|
||||
result = rlp.toString
|
||||
rlp.skipElem
|
||||
|
||||
proc read*(rlp: var Rlp, T: type int): int =
|
||||
result = rlp.toInt(int)
|
||||
proc read*(rlp: var Rlp, T: type Integer): Integer =
|
||||
result = rlp.toInt(T)
|
||||
rlp.skipElem
|
||||
|
||||
proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
|
||||
@ -306,6 +306,7 @@ proc decode*(bytes: openarray[byte]): RlpNode =
|
||||
bytesCopy = @bytes
|
||||
rlp = rlpFromBytes initBytesRange(bytesCopy)
|
||||
return rlp.toNodes
|
||||
|
||||
|
||||
template decode*(bytes: BytesRange, T: typedesc): untyped =
|
||||
var rlp = rlpFromBytes bytes
|
||||
|
@ -7,7 +7,7 @@ description = "RLP serialization library for Nim"
|
||||
license = "Apache2"
|
||||
skipDirs = @["tests"]
|
||||
|
||||
requires "nim >= 0.17.0", "ranges >= 0.0.1"
|
||||
requires "nim >= 0.17.0", "https://github.com/status-im/nim-ranges >= 0.0.1"
|
||||
|
||||
proc configForTests() =
|
||||
--hints: off
|
||||
|
@ -15,4 +15,3 @@ macro rlpFields*(T: typedesc, fields: varargs[untyped]): untyped =
|
||||
result = quote do:
|
||||
template enumerateRlpFields*(`ins`: `T`, `op`: untyped) {.inject.} =
|
||||
`body`
|
||||
|
||||
|
@ -13,18 +13,17 @@ type
|
||||
|
||||
PrematureFinalizationError* = object of Exception
|
||||
|
||||
IntLike* = concept x, y
|
||||
x + y
|
||||
x * y
|
||||
x - y
|
||||
x div y
|
||||
x mod y
|
||||
x shr y
|
||||
x shl y
|
||||
IntLike* = concept x, y, type T
|
||||
x + y is T
|
||||
x * y is T
|
||||
x - y is T
|
||||
x div y is T
|
||||
x mod y is T
|
||||
x shr y is T
|
||||
x shl y is T
|
||||
x and int # for masking
|
||||
|
||||
# Integer* = SomeOrdinal or IntLike
|
||||
Integer = int
|
||||
Integer* = SomeOrdinal or IntLike or uint or uint64
|
||||
|
||||
proc bytesNeeded(num: Integer): int =
|
||||
var n = num
|
||||
@ -154,7 +153,7 @@ proc append*(self; data: MemRange) =
|
||||
proc append*(self; i: Integer) =
|
||||
if i == 0:
|
||||
self.output.add BLOB_START_MARKER
|
||||
elif i < Integer(BLOB_START_MARKER):
|
||||
elif i < BLOB_START_MARKER.Integer:
|
||||
self.output.add byte(i)
|
||||
else:
|
||||
let bytesNeeded = i.bytesNeeded
|
||||
|
@ -9,7 +9,7 @@ type
|
||||
receiver: string
|
||||
|
||||
Foo = object
|
||||
x: int
|
||||
x: uint64
|
||||
y: string
|
||||
z: seq[int]
|
||||
|
||||
@ -27,7 +27,7 @@ proc default(T: typedesc): T = discard
|
||||
|
||||
test "encoding and decoding an object":
|
||||
var originalBar = Bar(b: "abracadabra",
|
||||
f: Foo(x: 5, y: "hocus pocus", z: @[100, 200, 300]))
|
||||
f: Foo(x: 5'u64, y: "hocus pocus", z: @[100, 200, 300]))
|
||||
|
||||
var bytes = encode(originalBar)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user