mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-06 07:33:14 +00:00
56 lines
1.8 KiB
Nim
56 lines
1.8 KiB
Nim
# This file is a modified version of Emery Hemingway’s CBOR library for Nim,
|
||
# originally available at https://github.com/ehmry/cbor-nim and released under The Unlicense.
|
||
|
||
import ./types
|
||
import ./errors
|
||
from macros import newDotExpr, newIdentNode, strVal
|
||
|
||
|
||
template tryNext*(c: var CborParser) =
|
||
let nextRes = c.next()
|
||
if nextRes.isFailure:
|
||
return failure(nextRes.error)
|
||
|
||
template trySkip*(c: var CborParser) =
|
||
let skipRes = c.skipNode()
|
||
if skipRes.isFailure:
|
||
return failure(skipRes.error)
|
||
|
||
template exceptCborKind*(expectedType: type, expectedKinds: set[CborNodeKind],
|
||
cbor: CborNode) =
|
||
if cbor.kind notin expectedKinds:
|
||
return failure(newUnexpectedKindError(expectedType, expectedKinds, cbor))
|
||
|
||
template exceptCborKind*(expectedType: type, expectedKind: CborNodeKind,
|
||
cbor: CborNode) =
|
||
exceptCborKind(expectedType, {expectedKind}, cbor)
|
||
|
||
template exceptCborKind*(expectedType: type, expectedKinds: set[CborEventKind],
|
||
cbor: CborNode) =
|
||
if cbor.kind notin expectedKinds:
|
||
return failure(newUnexpectedKindError(expectedType, expectedKinds, cbor))
|
||
|
||
template exceptCborKind*(expectedType: type, expectedKind: CborEventKind,
|
||
cbor: CborNode) =
|
||
exceptCborKind(expectedType, {expectedKind}, cbor)
|
||
|
||
macro dot*(obj: object, fld: string): untyped =
|
||
## Turn ``obj.dot("fld")`` into ``obj.fld``.
|
||
newDotExpr(obj, newIdentNode(fld.strVal))
|
||
|
||
|
||
func floatSingle*(half: uint16): float32 =
|
||
## Convert a 16-bit float to 32-bits.
|
||
func ldexp(x: float64; exponent: int): float64 {.importc: "ldexp",
|
||
header: "<math.h>".}
|
||
let
|
||
exp = (half shr 10) and 0x1f
|
||
mant = float64(half and 0x3ff)
|
||
val = if exp == 0:
|
||
ldexp(mant, -24)
|
||
elif exp != 31:
|
||
ldexp(mant + 1024, exp.int - 25)
|
||
else:
|
||
if mant == 0: Inf else: NaN
|
||
if (half and 0x8000) == 0: val else: -val
|