mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-02 13:43:06 +00:00
add tests for Time and TimeDate
This commit is contained in:
parent
a96971968d
commit
ff37e61b54
@ -494,30 +494,32 @@ proc parseTime(n: CborNode): Time =
|
||||
else:
|
||||
assert false
|
||||
|
||||
proc fromCborHook*(v: var DateTime; n: CborNode): ?!void =
|
||||
proc fromCbor*(_: type DateTime; n: CborNode): ?!DateTime =
|
||||
## Parse a `DateTime` from the tagged string representation
|
||||
## defined in RCF7049 section 2.4.1.
|
||||
var v: DateTime
|
||||
if n.tag.isSome:
|
||||
try:
|
||||
if n.tag.get == 0 and n.kind == cborText:
|
||||
v = parseDateText(n)
|
||||
return success()
|
||||
return success(v)
|
||||
elif n.tag.get == 1 and n.kind in {cborUnsigned, cborNegative, cborFloat}:
|
||||
v = parseTime(n).utc
|
||||
return success()
|
||||
return success(v)
|
||||
except ValueError as e: return failure(e)
|
||||
|
||||
proc fromCborHook*(v: var Time; n: CborNode): ?!void =
|
||||
proc fromCbor*(_: type Time; n: CborNode): ?!Time =
|
||||
## Parse a `Time` from the tagged string representation
|
||||
## defined in RCF7049 section 2.4.1.
|
||||
var v: Time
|
||||
if n.tag.isSome:
|
||||
try:
|
||||
if n.tag.get == 0 and n.kind == cborText:
|
||||
v = parseDateText(n).toTime
|
||||
return success()
|
||||
return success(v)
|
||||
elif n.tag.get == 1 and n.kind in {cborUnsigned, cborNegative, cborFloat}:
|
||||
v = parseTime(n)
|
||||
return success()
|
||||
return success(v)
|
||||
except ValueError as e: return failure(e)
|
||||
|
||||
func isTagged*(n: CborNode): bool =
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import std/unittest
|
||||
import std/options
|
||||
import std/streams
|
||||
import std/times
|
||||
import std/macros
|
||||
import pkg/serde
|
||||
import pkg/questionable
|
||||
@ -59,6 +60,8 @@ type
|
||||
refNewInner: NewType # Custom reference type
|
||||
refNil: ref Inner # Nil reference
|
||||
customPoint: CustomPoint # Custom type
|
||||
time: Time # Time
|
||||
date: DateTime # DateTime
|
||||
|
||||
# Custom deserialization for CustomColor enum
|
||||
# Converts a CBOR negative integer to a CustomColor enum value
|
||||
@ -169,7 +172,9 @@ suite "CBOR deserialization":
|
||||
refInner: refInner, # reference to object
|
||||
refNewInner: refNewObj, # custom reference type
|
||||
refNil: nil, # nil reference
|
||||
customPoint: CustomPoint(x: 15, y: 25) # custom type
|
||||
customPoint: CustomPoint(x: 15, y: 25), # custom type
|
||||
time: getTime(), # time
|
||||
date: now().utc # date
|
||||
)
|
||||
|
||||
# Test serialization using encode helper
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user