Revert Preserve to a concrete type

A generic type with an embedded type is too much hassle with the
type system. Add an "embedded" flag on "Preserve" to mark if a
value should be considered as embedded.
This commit is contained in:
Emery Hemingway 2021-09-08 11:00:38 +02:00
parent 9ed18c279c
commit e6d07ba924
5 changed files with 315 additions and 358 deletions

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: Unlicense # SPDX-License-Identifier: Unlicense
import bigints import bigints
import std/[algorithm, base64, endians, hashes, options, sets, sequtils, streams, strutils, tables, typetraits] import std/[base64, endians, hashes, options, sets, sequtils, streams, tables, typetraits]
from std/json import escapeJson, escapeJsonUnquoted from std/json import escapeJson, escapeJsonUnquoted
from std/macros import hasCustomPragma, getCustomPragmaVal from std/macros import hasCustomPragma, getCustomPragmaVal
@ -10,18 +10,16 @@ from std/macros import hasCustomPragma, getCustomPragmaVal
type type
PreserveKind* = enum PreserveKind* = enum
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol, pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol,
pkRecord, pkSequence, pkSet, pkDictionary, pkRecord, pkSequence, pkSet, pkDictionary
pkEmbedded
const const
atomKinds* = {pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol} atomKinds* = {pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol}
compoundKinds* = {pkRecord, pkSequence, pkSet, pkDictionary} compoundKinds* = {pkRecord, pkSequence, pkSet, pkDictionary}
type type
DictEntry[EmbededType] = tuple[key: PreserveGen[EmbededType], val: PreserveGen[EmbededType]] DictEntry = tuple[key: Preserve, val: Preserve]
PreserveGen*[EmbeddedType] {.acyclic.} = ref object Preserve* {.acyclic.} = object
## Generic ``Preserve`` type before embedding.
case kind*: PreserveKind case kind*: PreserveKind
of pkBoolean: of pkBoolean:
bool*: bool bool*: bool
@ -40,31 +38,47 @@ type
of pkSymbol: of pkSymbol:
symbol*: string symbol*: string
of pkRecord: of pkRecord:
record*: seq[PreserveGen[EmbeddedType]] # label is last record*: seq[Preserve] # label is last
of pkSequence: of pkSequence:
sequence*: seq[PreserveGen[EmbeddedType]] sequence*: seq[Preserve]
of pkSet: of pkSet:
set*: seq[PreserveGen[EmbeddedType]] set*: seq[Preserve]
# HashSet templates not hygenic enough for this type # TODO: HashSet
of pkDictionary: of pkDictionary:
dict*: seq[DictEntry[EmbeddedType]] dict*: seq[DictEntry]
# Tables templates not hygenic enough for this type # TODO: Tables
of pkEmbedded: embedded*: bool
when EmbeddedType is void:
embedded*: PreserveGen[EmbeddedType]
else:
embedded*: EmbeddedType
template PreserveOf*(T: typedesc): untyped = PreserveGen[T] proc `==`*(x, y: Preserve): bool =
## Customize ``PreserveGen`` with an embedded type. ## Check `x` and `y` for equivalence.
## ``` if x.kind == y.kind and x.embedded == y.embedded:
## type MyPreserve = PreserveOf(MyEmbbededType) case x.kind
## ``` of pkBoolean:
result = x.bool == y.bool
type of pkFloat:
Preserve* = PreserveOf(void) result = x.float == y.float
## Type of Preserves with all embedded values of pkDouble:
## converted to an unembedded representation. result = x.double == y.double
of pkSignedInteger:
result = x.int == y.int
of pkBigInteger:
result = x.bigint == y.bigint
of pkString:
result = x.string == y.string
of pkByteString:
result = x.bytes == y.bytes
of pkSymbol:
result = x.symbol == y.symbol
of pkRecord:
result = x.record == y.record
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
result = true
of pkSet:
result = x.set == y.set
of pkDictionary:
result = x.dict == y.dict
proc `<`(x, y: string | seq[byte]): bool = proc `<`(x, y: string | seq[byte]): bool =
for i in 0 .. min(x.high, y.high): for i in 0 .. min(x.high, y.high):
@ -72,9 +86,11 @@ proc `<`(x, y: string | seq[byte]): bool =
if x[i] != y[i]: return false if x[i] != y[i]: return false
x.len < y.len x.len < y.len
proc `<`*[E](x, y: PreserveGen[E]): bool = proc `<`*(x, y: Preserve): bool =
## Preserves have a total order over Values. Check if `x` is ordered before `y`. ## Preserves have a total order over Values. Check if `x` is ordered before `y`.
if x.kind != y.kind: if x.embedded != y.embedded:
result = y.embedded
elif x.kind != y.kind:
if x.kind == pkSignedInteger and y.kind == pkBigInteger: if x.kind == pkSignedInteger and y.kind == pkBigInteger:
result = x.int.initBigInt < y.bigint result = x.int.initBigInt < y.bigint
elif x.kind == pkBigInteger and y.kind == pkSignedInteger: elif x.kind == pkBigInteger and y.kind == pkSignedInteger:
@ -103,7 +119,7 @@ proc `<`*[E](x, y: PreserveGen[E]): bool =
if x.record[x.record.high] < y.record[y.record.high]: return true if x.record[x.record.high] < y.record[y.record.high]: return true
for i in 0..<min(x.record.high, y.record.high): for i in 0..<min(x.record.high, y.record.high):
if x.record[i] < y.record[i]: return true if x.record[i] < y.record[i]: return true
if x.record[i] != y.record[i]: return false if x.record[i] == y.record[i]: return false
result = x.record.len < y.record.len result = x.record.len < y.record.len
of pkSequence: of pkSequence:
for i in 0..min(x.sequence.high, y.sequence.high): for i in 0..min(x.sequence.high, y.sequence.high):
@ -122,131 +138,88 @@ proc `<`*[E](x, y: PreserveGen[E]): bool =
if x.dict[i].val < y.dict[i].val: return true if x.dict[i].val < y.dict[i].val: return true
if x.dict[i].val != y.dict[i].val: return false if x.dict[i].val != y.dict[i].val: return false
result = x.dict.len < y.dict.len result = x.dict.len < y.dict.len
of pkEmbedded:
when not E is void:
result = x.embedded < y.embedded
proc `==`*[E](x, y: PreserveGen[E]): bool = proc hash*(pr: Preserve): Hash =
## Check `x` and `y` for equivalence. ## Produce a `Hash` of `pr` for use with a `HashSet` or `Table`.
# TODO: is this necessary to define? var h = hash(pr.kind.int) !& hash(pr.embedded)
if x.isNil or y.isNil: case pr.kind
result = x.isNil and y.isNil
elif x.kind == y.kind:
case x.kind
of pkBoolean:
result = x.bool == y.bool
of pkFloat:
result = x.float == y.float
of pkDouble:
result = x.double == y.double
of pkSignedInteger:
result = x.int == y.int
of pkBigInteger:
result = x.bigint == y.bigint
of pkString:
result = x.string == y.string
of pkByteString:
result = x.bytes == y.bytes
of pkSymbol:
result = x.symbol == y.symbol
of pkRecord:
result = x.record == y.record
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
result = true
of pkSet:
result = x.set == y.set
of pkDictionary:
result = x.dict == y.dict
of pkEmbedded:
result = x.embedded == y.embedded
proc hash*[E](prs: PreserveGen[E]): Hash =
## Produce a `Hash` of `prs` for use with a `HashSet` or `Table`.
type Value = PreserveGen[E]
var h = hash(prs.kind.int)
case prs.kind
of pkBoolean: of pkBoolean:
h = h !& hash(prs.bool) h = h !& hash(pr.bool)
of pkFloat: of pkFloat:
h = h !& hash(prs.float) h = h !& hash(pr.float)
of pkDouble: of pkDouble:
h = h !& hash(prs.double) h = h !& hash(pr.double)
of pkSignedInteger: of pkSignedInteger:
h = h !& hash(prs.int) h = h !& hash(pr.int)
of pkBigInteger: of pkBigInteger:
h = h !& hash(prs.bigint.flags) h = h !& hash(pr.bigint.flags)
h = h !& hash(prs.bigint) h = h !& hash(pr.bigint)
of pkString: of pkString:
h = h !& hash(prs.string) h = h !& hash(pr.string)
of pkByteString: of pkByteString:
h = h !& hash(prs.bytes) h = h !& hash(pr.bytes)
of pkSymbol: of pkSymbol:
h = h !& hash(prs.symbol) h = h !& hash(pr.symbol)
of pkRecord: of pkRecord:
for val in prs.record: for val in pr.record:
h = h !& hash(val) h = h !& hash(val)
of pkSequence: of pkSequence:
for val in prs.sequence: for val in pr.sequence:
h = h !& hash(val) h = h !& hash(val)
of pkSet: of pkSet:
for val in prs.set.items: for val in pr.set.items:
h = h !& hash(val) h = h !& hash(val)
of pkDictionary: of pkDictionary:
for (key, val) in prs.dict.items: for (key, val) in pr.dict.items:
h = h !& hash(key) !& hash(val) h = h !& hash(key) !& hash(val)
of pkEmbedded:
when not E is void:
h = h !& hash(prs.embedded)
!$h !$h
proc `[]`*(prs: Preserve; i: int): Preserve = proc `[]`*(pr: Preserve; i: int): Preserve =
## Select an indexed value from `prs`. ## Select an indexed value from `pr`.
## Only valid for records and sequences. ## Only valid for records and sequences.
case prs.kind case pr.kind
of pkRecord: prs.record[i] of pkRecord: pr.record[i]
of pkSequence: prs.sequence[i] of pkSequence: pr.sequence[i]
else: else:
raise newException(ValueError, "`[]` is not valid for " & $prs.kind) raise newException(ValueError, "`[]` is not valid for " & $pr.kind)
proc incl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) = proc incl*(pr: var Preserve; key: Preserve) =
## Include `key` in the Preserves set `prs`. ## Include `key` in the Preserves set `pr`.
for i in 0..prs.set.high: for i in 0..pr.set.high:
if key < prs.set[i]: if key < pr.set[i]:
insert(prs.set, [key], i) insert(pr.set, [key], i)
return return
prs.set.add(key) pr.set.add(key)
proc excl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) = proc excl*(pr: var Preserve; key: Preserve) =
## Exclude `key` from the Preserves set `prs`. ## Exclude `key` from the Preserves set `pr`.
for i in 0..prs.set.high: for i in 0..pr.set.high:
if prs.set[i] == key: if pr.set[i] == key:
delete(prs.set, i, i) delete(pr.set, i, i)
break break
proc `[]`*[E](prs: var PreserveGen[E]; key: PreserveGen[E]): PreserveGen[E] = proc `[]`*(pr: var Preserve; key: Preserve): Preserve =
## Select a value by `key` from the Preserves dictionary `prs`. ## Select a value by `key` from the Preserves dictionary `pr`.
for (k, v) in prs.dict.items: for (k, v) in pr.dict.items:
if k == key: return v if k == key: return v
raise newException(KeyError, "value not in Preserves dictionary") raise newException(KeyError, "value not in Preserves dictionary")
proc `[]=`*[E](prs: var PreserveGen[E]; key, val: PreserveGen[E]) = proc `[]=`*(pr: var Preserve; key, val: Preserve) =
## Insert `val` by `key` in the Preserves dictionary `prs`. ## Insert `val` by `key` in the Preserves dictionary `pr`.
for i in 0..prs.dict.high: for i in 0..pr.dict.high:
if key < prs.dict[i].key: if key < pr.dict[i].key:
insert(prs.dict, [(key, val, )], i) insert(pr.dict, [(key, val, )], i)
return return
elif key == prs.dict[i].key: elif key == pr.dict[i].key:
prs.dict[i].val = val pr.dict[i].val = val
return return
prs.dict.add((key, val, )) pr.dict.add((key, val, ))
proc symbol*(s: string; E = void): PreserveGen[E] {.inline.} = proc symbol*(s: string; E = void): Preserve {.inline.} =
## Create a Preserves symbol value. ## Create a Preserves symbol value.
PreserveGen[E](kind: pkSymbol, symbol: s) Preserve(kind: pkSymbol, symbol: s)
proc initRecord*[E](label: PreserveGen[E]; args: varargs[PreserveGen[E]]): PreserveGen[E] = proc initRecord*(label: Preserve; args: varargs[Preserve]): Preserve =
## Create a Preserves record value. ## Create a Preserves record value.
result = Preserve(kind: pkRecord, result = Preserve(kind: pkRecord,
record: newSeqOfCap[Preserve](1+args.len)) record: newSeqOfCap[Preserve](1+args.len))
@ -260,74 +233,74 @@ proc initRecord*(label: string; args: varargs[Preserve, toPreserve]): Preserve =
assert($initRecord("foo", 1, 2.0) == "<foo 1 2.0f>") assert($initRecord("foo", 1, 2.0) == "<foo 1 2.0f>")
initRecord(symbol(label), args) initRecord(symbol(label), args)
proc initSet*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkSet) proc initSet*(): Preserve = Preserve(kind: pkSet)
## Create a Preserves set value. ## Create a Preserves set value.
proc initDictionary*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkDictionary) proc initDictionary*(): Preserve = Preserve(kind: pkDictionary)
## Create a Preserves dictionary value. ## Create a Preserves dictionary value.
proc len*[E](prs: PreserveGen[E]): int = proc len*(pr: Preserve): int =
## Return the shallow count of values in ``prs``, that is the number of ## Return the shallow count of values in ``pr``, that is the number of
## fields in a record, items in a sequence, items in a set, or key-value pairs ## fields in a record, items in a sequence, items in a set, or key-value pairs
## in a dictionary. ## in a dictionary.
case prs.kind case pr.kind
of pkRecord: prs.record.len.pred of pkRecord: pr.record.len.pred
of pkSequence: prs.sequence.len of pkSequence: pr.sequence.len
of pkSet: prs.set.len of pkSet: pr.set.len
of pkDictionary: prs.dict.len of pkDictionary: pr.dict.len
else: 0 else: 0
iterator items*[E](prs: PreserveGen[E]): PreserveGen[E] = iterator items*(pr: Preserve): Preserve =
## Shallow iterator over `prs`, yield the fields in a record, ## Shallow iterator over `pr`, yield the fields in a record,
## the items of a sequence, the items of a set, or the pairs ## the items of a sequence, the items of a set, or the pairs
## of a dictionary. ## of a dictionary.
case prs.kind case pr.kind
of pkRecord: of pkRecord:
for i in 0..prs.record.high.pred: for i in 0..pr.record.high.pred:
yield prs.record[i] yield pr.record[i]
of pkSequence: of pkSequence:
for e in prs.sequence.items: yield e for e in pr.sequence.items: yield e
of pkSet: of pkSet:
for e in prs.set.items: yield e for e in pr.set.items: yield e
of pkDictionary: of pkDictionary:
for (k, v) in prs.dict.items: for (k, v) in pr.dict.items:
yield k # key can be an arbitrary Preserve yield k # key can be an arbitrary Preserve
yield v yield v
else: discard else: discard
proc isFalse*[E](prs: PreserveGen[E]): bool = proc isFalse*(pr: Preserve): bool =
## Check if ``prs`` is equivalent to the zero-initialized ``Preserve``. ## Check if ``pr`` is equivalent to the zero-initialized ``Preserve``.
prs.kind == pkBoolean and prs.bool == false pr.kind == pkBoolean and pr.bool == false
proc isSymbol*[E](prs: PreserveGen[E]; sym: string): bool = proc isSymbol*(pr: Preserve; sym: string): bool =
## Check if `prs` is a Preserves symbol. ## Check if `pr` is a Preserves symbol.
(prs.kind == pkSymbol) and (prs.symbol == sym) (pr.kind == pkSymbol) and (pr.symbol == sym)
proc isRecord*[E](prs: PreserveGen[E]): bool = proc isRecord*(pr: Preserve): bool =
## Check if `prs` is a Preserves record. ## Check if `pr` is a Preserves record.
if prs.kind == pkRecord: if pr.kind == pkRecord:
result = true result = true
assert(prs.record.len > 0) assert(pr.record.len > 0)
proc isDictionary*[E](prs: PreserveGen[E]): bool = proc isDictionary*(pr: Preserve): bool =
## Check if `prs` is a Preserves dictionary. ## Check if `pr` is a Preserves dictionary.
prs.kind == pkDictionary pr.kind == pkDictionary
proc label*[E](prs: PreserveGen[E]): PreserveGen[E] {.inline.} = proc label*(pr: Preserve): Preserve {.inline.} =
## Return the label of record value. ## Return the label of record value.
prs.record[prs.record.high] pr.record[pr.record.high]
proc arity*[E](prs: PreserveGen[E]): int {.inline.} = proc arity*(pr: Preserve): int {.inline.} =
## Return the number of fields in record `prs`. ## Return the number of fields in record `pr`.
pred(prs.record.len) pred(pr.record.len)
proc fields*[E](prs: PreserveGen[E]): seq[PreserveGen[E]] {.inline.} = proc fields*(pr: Preserve): seq[Preserve] {.inline.} =
## Return the fields of a record value. ## Return the fields of a record value.
prs.record[0..prs.record.high.pred] pr.record[0..pr.record.high.pred]
iterator fields*[E](prs: PreserveGen[E]): PreserveGen[E] = iterator fields*(pr: Preserve): Preserve =
## Iterate the fields of a record value. ## Iterate the fields of a record value.
for i in 0..<prs.record.high: yield prs.record[i] for i in 0..<pr.record.high: yield pr.record[i]
proc writeVarint(s: Stream; n: int) = proc writeVarint(s: Stream; n: int) =
var n = n var n = n
@ -349,39 +322,40 @@ proc readVarint(s: Stream): int =
break break
shift.inc 7 shift.inc 7
proc write*[E](str: Stream; prs: PreserveGen[E]) = proc write*(str: Stream; pr: Preserve) =
## Write the binary-encoding of a Preserves value to a stream. ## Write the binary-encoding of a Preserves value to a stream.
case prs.kind: if pr.embedded: str.write(0x86'u8)
case pr.kind:
of pkBoolean: of pkBoolean:
case prs.bool case pr.bool
of false: str.write(0x80'u8) of false: str.write(0x80'u8)
of true: str.write(0x81'u8) of true: str.write(0x81'u8)
of pkFloat: of pkFloat:
str.write(0x82'u8) str.write(0x82'u8)
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
str.write(prs.float) str.write(pr.float)
else: else:
var be: float32 var be: float32
swapEndian32(be.addr, prs.float.unsafeAddr) swapEndian32(be.addr, pr.float.unsafeAddr)
str.write(be) str.write(be)
of pkDouble: of pkDouble:
str.write(0x83'u8) str.write(0x83'u8)
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
str.write(prs.double) str.write(pr.double)
else: else:
var be: float64 var be: float64
swapEndian64(be.addr, prs.double.unsafeAddr) swapEndian64(be.addr, pr.double.unsafeAddr)
str.write(be) str.write(be)
of pkSignedInteger: of pkSignedInteger:
if (-3 <= prs.int) and (prs.int <= 12): if (-3 <= pr.int) and (pr.int <= 12):
str.write(0x90'i8 or int8(if prs.int < 0: prs.int + 16 else: prs.int)) str.write(0x90'i8 or int8(if pr.int < 0: pr.int + 16 else: pr.int))
else: else:
var bitCount = 1'u8 var bitCount = 1'u8
if prs.int < 0: if pr.int < 0:
while ((not prs.int) shr bitCount) != 0: while ((not pr.int) shr bitCount) != 0:
inc(bitCount) inc(bitCount)
else: else:
while (prs.int shr bitCount) != 0: while (pr.int shr bitCount) != 0:
inc(bitCount) inc(bitCount)
var byteCount = (bitCount + 8) div 8 var byteCount = (bitCount + 8) div 8
str.write(0xa0'u8 or (byteCount - 1)) str.write(0xa0'u8 or (byteCount - 1))
@ -389,13 +363,13 @@ proc write*[E](str: Stream; prs: PreserveGen[E]) =
if n > 0: if n > 0:
write(n.pred, i shr 8) write(n.pred, i shr 8)
str.write(i.uint8) str.write(i.uint8)
write(byteCount, prs.int) write(byteCount, pr.int)
of pkBigInteger: of pkBigInteger:
doAssert(Negative notin prs.bigint.flags, "negative big integers not implemented") doAssert(Negative notin pr.bigint.flags, "negative big integers not implemented")
var bytes = newSeqOfCap[uint8](prs.bigint.limbs.len * 4) var bytes = newSeqOfCap[uint8](pr.bigint.limbs.len * 4)
var begun = false var begun = false
for i in countdown(prs.bigint.limbs.high, 0): for i in countdown(pr.bigint.limbs.high, 0):
let limb = prs.bigint.limbs[i] let limb = pr.bigint.limbs[i]
for j in countdown(24, 0, 8): for j in countdown(24, 0, 8):
let b = uint8(limb shr j) let b = uint8(limb shr j)
begun = begun or (b != 0) begun = begun or (b != 0)
@ -409,118 +383,110 @@ proc write*[E](str: Stream; prs: PreserveGen[E]) =
str.write(cast[string](bytes)) str.write(cast[string](bytes))
of pkString: of pkString:
str.write(0xb1'u8) str.write(0xb1'u8)
str.writeVarint(prs.string.len) str.writeVarint(pr.string.len)
str.write(prs.string) str.write(pr.string)
of pkByteString: of pkByteString:
str.write(0xb2'u8) str.write(0xb2'u8)
str.writeVarint(prs.bytes.len) str.writeVarint(pr.bytes.len)
str.write(cast[string](prs.bytes)) str.write(cast[string](pr.bytes))
of pkSymbol: of pkSymbol:
str.write(0xb3'u8) str.write(0xb3'u8)
str.writeVarint(prs.symbol.len) str.writeVarint(pr.symbol.len)
str.write(prs.symbol) str.write(pr.symbol)
of pkRecord: of pkRecord:
assert(prs.record.len > 0) assert(pr.record.len > 0)
str.write(0xb4'u8) str.write(0xb4'u8)
str.write(prs.record[prs.record.high]) str.write(pr.record[pr.record.high])
for i in 0..<prs.record.high: for i in 0..<pr.record.high:
str.write(prs.record[i]) str.write(pr.record[i])
str.write(0x84'u8) str.write(0x84'u8)
of pkSequence: of pkSequence:
str.write(0xb5'u8) str.write(0xb5'u8)
for e in prs.sequence: for e in pr.sequence:
str.write(e) str.write(e)
str.write(0x84'u8) str.write(0x84'u8)
of pkSet: of pkSet:
str.write(0xb6'u8) str.write(0xb6'u8)
for val in prs.set.items: for val in pr.set.items:
str.write(val) str.write(val)
str.write(0x84'u8) str.write(0x84'u8)
of pkDictionary: of pkDictionary:
str.write(0xb7'u8) str.write(0xb7'u8)
for (key, value) in prs.dict.items: for (key, value) in pr.dict.items:
str.write(key) str.write(key)
str.write(value) str.write(value)
str.write(0x84'u8) str.write(0x84'u8)
of pkEmbedded:
str.write(0x86'u8)
when E is void:
str.write(0x80'u8)
else:
str.write(prs.embedded.toPreserve)
proc encode*[E](prs: PreserveGen[E]): string = proc encode*(pr: Preserve): seq[byte] =
## Return the binary-encoding of a Preserves value. ## Return the binary-encoding of a Preserves value.
let s = newStringStream() let s = newStringStream()
s.write prs s.write pr
s.setPosition 0 s.setPosition 0
result = s.readAll result = cast[seq[byte]](s.readAll)
proc decodePreserves*(s: Stream; E = void): PreserveGen[E] = proc decodePreserves*(s: Stream): Preserve =
## Decode a Preserves value from a binary-encoded stream. ## Decode a Preserves value from a binary-encoded stream.
## ``E`` is the Nim type of embedded values where ``void``
## selects a ``Preserve`` representation.
type Value = PreserveGen[E]
proc assertStream(check: bool) = proc assertStream(check: bool) =
if not check: if not check:
raise newException(ValueError, "invalid Preserves stream") raise newException(ValueError, "invalid Preserves stream")
const endMarker = 0x84 const endMarker = 0x84
let tag = s.readUint8() let tag = s.readUint8()
case tag case tag
of 0x80: result = Value(kind: pkBoolean, bool: false) of 0x80: result = Preserve(kind: pkBoolean, bool: false)
of 0x81: result = Value(kind: pkBoolean, bool: true) of 0x81: result = Preserve(kind: pkBoolean, bool: true)
of 0x82: of 0x82:
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
result = Value(kind: pkFloat, float: s.readFloat32()) result = Preserve(kind: pkFloat, float: s.readFloat32())
else: else:
result = Value(kind: pkFloat) result = Preserve(kind: pkFloat)
var be = s.readFloat32() var be = s.readFloat32()
swapEndian32(result.float.addr, be.addr) swapEndian32(result.float.addr, be.addr)
of 0x83: of 0x83:
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
result = Value(kind: pkDouble, double: s.readFloat64()) result = Preserve(kind: pkDouble, double: s.readFloat64())
else: else:
result = Value(kind: pkDouble) result = Preserve(kind: pkDouble)
var be = s.readFloat64() var be = s.readFloat64()
swapEndian64(result.double.addr, be.addr) swapEndian64(result.double.addr, be.addr)
of 0x86: of 0x86:
result = Value(kind: pkEmbedded, embedded: decodePreserves(s, E)) result = decodePreserves(s)
result.embedded = true
of 0xb1: of 0xb1:
result = Value(kind: pkString) result = Preserve(kind: pkString)
let len = s.readVarint() let len = s.readVarint()
result.string = s.readStr(len) result.string = s.readStr(len)
of 0xb2: of 0xb2:
result = Value(kind: pkByteString) result = Preserve(kind: pkByteString)
let len = s.readVarint() let len = s.readVarint()
result.bytes = cast[seq[byte]](s.readStr(len)) result.bytes = cast[seq[byte]](s.readStr(len))
of 0xb3: of 0xb3:
let len = s.readVarint() let len = s.readVarint()
result = Value(kind: pkSymbol, symbol: s.readStr(len)) result = Preserve(kind: pkSymbol, symbol: s.readStr(len))
of 0xb4: of 0xb4:
result = Value(kind: pkRecord) result = Preserve(kind: pkRecord)
var label = decodePreserves(s, E) var label = decodePreserves(s)
while s.peekUint8() != endMarker: while s.peekUint8() != endMarker:
result.record.add decodePreserves(s, E) result.record.add decodePreserves(s)
result.record.add(move label) result.record.add(move label)
discard s.readUint8() discard s.readUint8()
of 0xb5: of 0xb5:
result = Value(kind: pkSequence) result = Preserve(kind: pkSequence)
while s.peekUint8() != endMarker: while s.peekUint8() != endMarker:
result.sequence.add decodePreserves(s, E) result.sequence.add decodePreserves(s)
discard s.readUint8() discard s.readUint8()
of 0xb6: of 0xb6:
result = Value(kind: pkSet) result = Preserve(kind: pkSet)
while s.peekUint8() != endMarker: while s.peekUint8() != endMarker:
incl(result, decodePreserves(s, E)) incl(result, decodePreserves(s))
discard s.readUint8() discard s.readUint8()
of 0xb7: of 0xb7:
result = Value(kind: pkDictionary) result = Preserve(kind: pkDictionary)
while s.peekUint8() != endMarker: while s.peekUint8() != endMarker:
result[decodePreserves(s, E)] = decodePreserves(s, E) result[decodePreserves(s)] = decodePreserves(s)
discard s.readUint8() discard s.readUint8()
of 0xb0: of 0xb0:
let len = s.readVarint() let len = s.readVarint()
result = Value(kind: pkBigInteger, bigint: initBigint 0) result = Preserve(kind: pkBigInteger, bigint: initBigint 0)
for _ in 1..len: for _ in 1..len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32 result.bigint = (result.bigint shl 8) + s.readUint8().int32
of endMarker: of endMarker:
@ -529,29 +495,29 @@ proc decodePreserves*(s: Stream; E = void): PreserveGen[E] =
case 0xf0 and tag case 0xf0 and tag
of 0x90: of 0x90:
var n = tag.BiggestInt var n = tag.BiggestInt
result = Value(kind: pkSignedInteger, result = Preserve(kind: pkSignedInteger,
int: n - (if n > 0x9c: 0xa0 else: 0x90)) int: n - (if n > 0x9c: 0xa0 else: 0x90))
of 0xa0: of 0xa0:
let len = (tag.int and 0x0f) + 1 let len = (tag.int and 0x0f) + 1
if len <= 8: if len <= 8:
result = Value(kind: pkSignedInteger, int: s.readUint8().BiggestInt) result = Preserve(kind: pkSignedInteger, int: s.readUint8().BiggestInt)
if (result.int and 0x80) != 0: result.int.dec(0x100) if (result.int and 0x80) != 0: result.int.dec(0x100)
for i in 1..<len: for i in 1..<len:
result.int = (result.int shl 8) or s.readUint8().BiggestInt result.int = (result.int shl 8) or s.readUint8().BiggestInt
else: else:
result = Value(kind: pkBigInteger) result = Preserve(kind: pkBigInteger)
for i in 0..<len: for i in 0..<len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32 result.bigint = (result.bigint shl 8) + s.readUint8().int32
else: else:
assertStream(false) assertStream(false)
proc decodePreserves*(s: string, E = void): PreserveGen[E] = proc decodePreserves*(s: string): Preserve =
## Decode a string of binary-encoded Preserves. ## Decode a string of binary-encoded Preserves.
s.newStringStream.decodePreserves E s.newStringStream.decodePreserves
proc decodePreserves*(s: seq[byte], E = void): PreserveGen[E] = proc decodePreserves*(s: seq[byte]): Preserve =
## Decode a byte-string of binary-encoded Preserves. ## Decode a byte-string of binary-encoded Preserves.
cast[string](s).decodePreserves E cast[string](s).decodePreserves
template record*(label: string) {.pragma.} template record*(label: string) {.pragma.}
## Serialize this object or tuple as a record. See ``toPreserve``. ## Serialize this object or tuple as a record. See ``toPreserve``.
@ -559,50 +525,46 @@ template record*(label: string) {.pragma.}
template unpreservable*() {.pragma.} template unpreservable*() {.pragma.}
## Pragma to forbid a type from being converted by ``toPreserve``. ## Pragma to forbid a type from being converted by ``toPreserve``.
proc toPreserve*[T](x: T; E = void): PreserveGen[E] = proc toPreserve*[T](x: T): Preserve =
## Serializes ``x`` to Preserves. Can be customized by defining ## Serializes ``x`` to Preserves. Can be customized by defining
## ``toPreserveHook(x: T)`` in the calling scope. ## ``toPreserveHook(x: T)`` in the calling scope.
## ``E`` is the embedded type where ``void`` returns embedded when (T is Preserve): result = x
## values as Preserves.
type Value = PreserveGen[E]
when (T is Value): result = x
elif T is E: result = Value(kind: pkEmbedded, embedded: x)
elif compiles(toPreserveHook(x)): elif compiles(toPreserveHook(x)):
result = toPreserveHook(x) result = toPreserveHook(x)
elif T is Bigint: elif T is Bigint:
result = Value(kind: pkBigInteger, bigint: x) result = Preserve(kind: pkBigInteger, bigint: x)
elif T is seq[byte]: elif T is seq[byte]:
result = Value(kind: pkByteString, bytes: x) result = Preserve(kind: pkByteString, bytes: x)
elif T is array | seq: elif T is array | seq:
result = Value(kind: pkSequence) result = Preserve(kind: pkSequence)
for v in x.items: result.sequence.add(toPreserve(v, E)) for v in x.items: result.sequence.add(toPreserve(v))
elif T is bool: elif T is bool:
result = Value(kind: pkBoolean, bool: x) result = Preserve(kind: pkBoolean, bool: x)
elif T is distinct: elif T is distinct:
result = toPreserve(x.distinctBase) result = toPreserve(x.distinctBase)
elif T is float: elif T is float:
result = Value(kind: pkFloat, float: x) result = Preserve(kind: pkFloat, float: x)
elif T is float64: elif T is float64:
result = Value(kind: pkDouble, double: x) result = Preserve(kind: pkDouble, double: x)
elif T is object | tuple: elif T is object | tuple:
when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".} when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".}
elif T.hasCustomPragma(record): elif T.hasCustomPragma(record):
result = Value(kind: pkRecord) result = Preserve(kind: pkRecord)
for _, f in x.fieldPairs: result.record.add(toPreserve(f)) for _, f in x.fieldPairs: result.record.add(toPreserve(f))
result.record.add(symbol(T.getCustomPragmaVal(record))) result.record.add(symbol(T.getCustomPragmaVal(record)))
else: else:
result = Value(kind: pkDictionary) result = Preserve(kind: pkDictionary)
for k, v in x.fieldPairs: for k, v in x.fieldPairs:
result[symbol(k, E)] = toPreserve(v, E) result[symbol(k)] = toPreserve(v)
elif T is Ordinal: elif T is Ordinal:
result = Value(kind: pkSignedInteger, int: x.ord.BiggestInt) result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
elif T is ptr | ref: elif T is ptr | ref:
if system.`==`(x, nil): result = symbol("null", E) if system.`==`(x, nil): result = symbol("null")
else: result = toPreserve(x[]) else: result = toPreserve(x[])
elif T is string: elif T is string:
result = Value(kind: pkString, string: x) result = Preserve(kind: pkString, string: x)
elif T is SomeInteger: elif T is SomeInteger:
result = Value(kind: pkSignedInteger, int: x.BiggestInt) result = Preserve(kind: pkSignedInteger, int: x.BiggestInt)
else: else:
raiseAssert("unpreservable type" & $T) raiseAssert("unpreservable type" & $T)
@ -613,9 +575,9 @@ proc toPreserveHook*[T](set: HashSet[T]): Preserve =
proc toPreserveHook*[A, B](table: Table[A, B]|TableRef[A, B]): Preserve = proc toPreserveHook*[A, B](table: Table[A, B]|TableRef[A, B]): Preserve =
## Hook for preserving ``Table``. ## Hook for preserving ``Table``.
result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len)) result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len))
for k, v in table.pairs: result.dict.add((toPreserve(k), toPreserve(v), )) for k, v in table.pairs: result.dict.add((k.toPreserve, v.toPreserve, ))
proc fromPreserve*[E, T](v: var T; prs: PreserveGen[E]): bool = proc fromPreserve*[T](v: var T; pr: Preserve): bool =
## Inplace version of `preserveTo`. Returns ``true`` on ## Inplace version of `preserveTo`. Returns ``true`` on
## a complete match, otherwise returns ``false``. ## a complete match, otherwise returns ``false``.
# TODO: {.raises: [].} # TODO: {.raises: [].}
@ -627,93 +589,93 @@ proc fromPreserve*[E, T](v: var T; prs: PreserveGen[E]): bool =
assert(fromPreserve(foo, parsePreserves("""<foo 1 2>"""))) assert(fromPreserve(foo, parsePreserves("""<foo 1 2>""")))
assert(foo.x == 1) assert(foo.x == 1)
assert(foo.y == 2) assert(foo.y == 2)
type Value = PreserveGen[E] type Value = Preserve
when T is Value: when T is Value:
v = prs v = pr
result = true result = true
elif compiles(fromPreserveHook(v, prs)): elif compiles(fromPreserveHook(v, pr)):
result = fromPreserveHook(v, prs) result = fromPreserveHook(v, pr)
elif T is Bigint: elif T is Bigint:
case prs.kind case pr.kind
of pkSignedInteger: of pkSignedInteger:
v = initBigint(prs.int) v = initBigint(pr.int)
result = true result = true
of pkBigInteger: of pkBigInteger:
v = prs.bigint v = pr.bigint
result = true result = true
else: disard else: disard
elif T is bool: elif T is bool:
if prs.kind == pkBoolean: if pr.kind == pkBoolean:
v = prs.bool v = pr.bool
result = true result = true
elif T is SomeInteger: elif T is SomeInteger:
if prs.kind == pkSignedInteger: if pr.kind == pkSignedInteger:
v = T(prs.int) v = T(pr.int)
result = true result = true
elif T is float: elif T is float:
if prs.kind == pkFloat: if pr.kind == pkFloat:
v = prs.float v = pr.float
result = true result = true
elif T is seq: elif T is seq:
if T is seq[byte] and prs.kind == pkByteString: if T is seq[byte] and pr.kind == pkByteString:
v = prs.bytes v = pr.bytes
result = true result = true
elif prs.kind == pkSequence: elif pr.kind == pkSequence:
v.setLen(prs.len) v.setLen(pr.len)
result = true result = true
for i, e in prs.sequence: for i, e in pr.sequence:
result = result and fromPreserve(v[i], e) result = result and fromPreserve(v[i], e)
elif T is float64: elif T is float64:
case prs.kind case pr.kind
of pkFloat: of pkFloat:
v = prs.float v = pr.float
result = true result = true
of pkDouble: of pkDouble:
v = prs.double v = pr.double
result = true result = true
elif T is object | tuple: elif T is object | tuple:
case prs.kind case pr.kind
of pkRecord: of pkRecord:
when T.hasCustomPragma(record): when T.hasCustomPragma(record):
if prs.record[prs.record.high].isSymbol T.getCustomPragmaVal(record): if pr.record[pr.record.high].isSymbol T.getCustomPragmaVal(record):
result = true result = true
var i = 0 var i = 0
for fname, field in v.fieldPairs: for fname, field in v.fieldPairs:
if not result or (i == prs.record.high): break if not result or (i == pr.record.high): break
result = result and fromPreserve(field, prs.record[i]) result = result and fromPreserve(field, pr.record[i])
inc(i) inc(i)
result = result and (i == prs.record.high) # arity equivalence check= result = result and (i == pr.record.high) # arity equivalence check=
of pkDictionary: of pkDictionary:
result = true result = true
var fieldCount = 0 var fieldCount = 0
for key, val in v.fieldPairs: for key, val in v.fieldPairs:
inc fieldCount inc fieldCount
for (pk, pv) in prs.dict.items: for (pk, pv) in pr.dict.items:
var sym = symbol(key, E) var sym = symbol(key)
if sym == pk: if sym == pk:
result = result and fromPreserve(val, pv) result = result and fromPreserve(val, pv)
break break
result = result and prs.dict.len == fieldCount result = result and pr.dict.len == fieldCount
else: discard else: discard
elif T is Ordinal | SomeInteger: elif T is Ordinal | SomeInteger:
if prs.kind == pkSignedInteger: if pr.kind == pkSignedInteger:
v = (T)prs.int v = (T)pr.int
result = true result = true
elif T is ref: elif T is ref:
if prs != symbol("null", E): if pr != symbol("null"):
new v new v
result = fromPreserve(v[], prs) result = fromPreserve(v[], pr)
elif T is string: elif T is string:
if prs.kind == pkString: if pr.kind == pkString:
v = prs.string v = pr.string
result = true result = true
elif T is distinct: elif T is distinct:
result = fromPreserve(result.distinctBase, prs) result = fromPreserve(result.distinctBase, pr)
else: else:
raiseAssert("no conversion of type Preserve to " & $T) raiseAssert("no conversion of type Preserve to " & $T)
if not result: reset v if not result: reset v
proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] = proc preserveTo*(pr: Preserve; T: typedesc): Option[T] =
## Reverse of `toPreserve`. ## Reverse of `toPreserve`.
## ##
# TODO: {.raises: [].} # TODO: {.raises: [].}
@ -726,70 +688,71 @@ proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] =
assert(parsePreserves("""<bar 1 2>""").preserveTo(Foo).isNone) assert(parsePreserves("""<bar 1 2>""").preserveTo(Foo).isNone)
assert(parsePreserves("""<foo 1 2>""").preserveTo(Foo).isSome) assert(parsePreserves("""<foo 1 2>""").preserveTo(Foo).isSome)
var v: T var v: T
if fromPreserve(v, prs): if fromPreserve(v, pr):
result = some(move v) result = some(move v)
proc fromPreserveHook*[A,B](t: var Table[A,B]|TableRef[A,B]; prs: Preserve): bool = proc fromPreserveHook*[A,B,E](t: var Table[A,B]|TableRef[A,B]; pr: Preserve): bool =
if prs.isDictionary: if pr.isDictionary:
for k, v in prs.pairs: for k, v in pr.pairs:
t[preserveTo(k, A)] = preserveTo(k, B) t[preserveTo(k, A)] = preserveTo(v, B)
result = true result = true
proc concat[E](result: var string; prs: PreserveGen[E]) = proc concat(result: var string; pr: Preserve) =
case prs.kind: if pr.embedded: result.add("#!")
case pr.kind:
of pkBoolean: of pkBoolean:
case prs.bool case pr.bool
of false: result.add "#f" of false: result.add "#f"
of true: result.add "#t" of true: result.add "#t"
of pkFloat: of pkFloat:
result.add($prs.float & "f") result.add($pr.float & "f")
of pkDouble: of pkDouble:
result.add $prs.double result.add $pr.double
of pkSignedInteger: of pkSignedInteger:
result.add $prs.int result.add $pr.int
of pkBigInteger: of pkBigInteger:
result.add $prs.bigint result.add $pr.bigint
of pkString: of pkString:
result.add escapeJson(prs.string) result.add escapeJson(pr.string)
of pkByteString: of pkByteString:
for b in prs.bytes: for b in pr.bytes:
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}: if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
result.add("#[") #]# result.add("#[") #]#
result.add(base64.encode(prs.bytes)) result.add(base64.encode(pr.bytes))
result.add(']') result.add(']')
return return
result.add("#\"") result.add("#\"")
result.add(cast[string](prs.bytes)) result.add(cast[string](pr.bytes))
result.add('"') result.add('"')
of pkSymbol: of pkSymbol:
result.add(escapeJsonUnquoted(prs.symbol)) result.add(escapeJsonUnquoted(pr.symbol))
of pkRecord: of pkRecord:
assert(prs.record.len > 0) assert(pr.record.len > 0)
result.add('<') result.add('<')
result.concat(prs.record[prs.record.high]) result.concat(pr.record[pr.record.high])
for i in 0..<prs.record.high: for i in 0..<pr.record.high:
result.add(' ') result.add(' ')
result.concat(prs.record[i]) result.concat(pr.record[i])
result.add('>') result.add('>')
of pkSequence: of pkSequence:
result.add('[') result.add('[')
for i, val in prs.sequence: for i, val in pr.sequence:
if i > 0: if i > 0:
result.add(' ') result.add(' ')
result.concat(val) result.concat(val)
result.add(']') result.add(']')
of pkSet: of pkSet:
result.add("#{") result.add("#{")
for val in prs.set.items: for val in pr.set.items:
result.concat(val) result.concat(val)
result.add(' ') result.add(' ')
if prs.set.len > 1: if pr.set.len > 1:
result.setLen(result.high) result.setLen(result.high)
result.add('}') result.add('}')
of pkDictionary: of pkDictionary:
result.add('{') result.add('{')
var i = 0 var i = 0
for (key, value) in prs.dict.items: for (key, value) in pr.dict.items:
if i > 0: if i > 0:
result.add(' ') result.add(' ')
result.concat(key) result.concat(key)
@ -797,12 +760,6 @@ proc concat[E](result: var string; prs: PreserveGen[E]) =
result.concat(value) result.concat(value)
inc i inc i
result.add('}') result.add('}')
of pkEmbedded:
result.add("#!")
when E is void:
result.add("#f")
else:
result.add($prs.embedded)
proc `$`*[E](prs: PreserveGen[E]): string = concat(result, prs) proc `$`*(pr: Preserve): string = concat(result, pr)
## Generate the textual representation of ``prs``. ## Generate the textual representation of ``pr``.

View File

@ -28,45 +28,45 @@ proc toPreserveHook*(js: JsonNode): Preserve =
for i, e in js.elems: for i, e in js.elems:
result.sequence[i] = toPreserveHook(e) result.sequence[i] = toPreserveHook(e)
proc toJsonHook*(prs: Preserve): JsonNode = proc fromPreserveHook*(js: var JsonNode; prs: Preserve): bool =
case prs.kind: case prs.kind:
of pkBoolean: of pkBoolean:
result = newJBool(prs.bool) js = newJBool(prs.bool)
of pkFloat: of pkFloat:
result = newJFloat(prs.float) js = newJFloat(prs.float)
of pkDouble: of pkDouble:
result = newJFloat(prs.double) js = newJFloat(prs.double)
of pkSignedInteger: of pkSignedInteger:
result = newJInt(prs.int) js = newJInt(prs.int)
of pkBigInteger:
raise newException(ValueError, "cannot convert big integer to JSON")
of pkString: of pkString:
result = newJString(prs.string) js = newJString(prs.string)
of pkByteString:
raise newException(ValueError, "cannot convert bytes to JSON")
of pkSymbol: of pkSymbol:
case prs.symbol case prs.symbol
of "false": of "false":
result = newJBool(false) js = newJBool(false)
of "true": of "true":
result = newJBool(true) js = newJBool(true)
of "null": of "null":
result = newJNull() js = newJNull()
else: else:
raise newException(ValueError, "cannot convert symbol to JSON") return false
of pkRecord:
raise newException(ValueError, "cannot convert record to JSON")
of pkSequence: of pkSequence:
result = newJArray() js = newJArray()
for val in prs.sequence: js.elems.setLen(prs.sequence.len)
result.add(toJsonHook(val)) for i, val in prs.sequence:
of pkSet: if not fromPreserve(js.elems[i], val):
raise newException(ValueError, "cannot convert set to JSON") return false
of pkDictionary: of pkDictionary:
result = newJObject() js = newJObject()
for (key, val) in prs.dict.items: for (key, val) in prs.dict.items:
if key.kind != pkString: if key.kind != pkString:
raise newException(ValueError, "cannot convert non-string dictionary key to JSON") return false
result[key.string] = toJsonHook(val) var jsVal: JsonNode
of pkEmbedded: if not fromPreserve(jsVal, val): return false
raise newException(ValueError, "cannot convert embedded value to JSON") js[key.string] = jsVal
else: return false
true
proc toJsonHook*(pr: Preserve): JsonNode =
if not fromPreserve(result, pr):
raise newException(ValueError, "cannot convert Preserves value to JSON")

View File

@ -89,9 +89,9 @@ proc parsePreserves*(text: string): Preserve {.gcsafe.} =
pushStack Preserve(kind: pkSymbol, symbol: $0) pushStack Preserve(kind: pkSymbol, symbol: $0)
Preserves.Embedded <- Preserves.Embedded: Preserves.Embedded <- Preserves.Embedded:
pushStack Preserve( var v = stack.pop.value
kind: pkEmbedded, v.embedded = true
embedded: stack.pop.value) pushStack v
Preserves.Compact <- Preserves.Compact: Preserves.Compact <- Preserves.Compact:
pushStack decodePreserves(stack.pop.value.bytes) pushStack decodePreserves(stack.pop.value.bytes)

View File

@ -4,9 +4,9 @@
import std/[macros, typetraits] import std/[macros, typetraits]
import ../preserves import ../preserves
type RecordClass*[EmbeddedType] = object type RecordClass* = object
## Type of a preserves record. ## Type of a preserves record.
label*: PreserveGen[EmbeddedType] label*: Preserve
arity*: Natural arity*: Natural
proc `$`*(rec: RecordClass): string = proc `$`*(rec: RecordClass): string =
@ -18,17 +18,17 @@ proc isClassOf*(rec: RecordClass; val: Preserve): bool =
assert(val.record.len > 0) assert(val.record.len > 0)
result = val.label == rec.label and rec.arity == val.arity result = val.label == rec.label and rec.arity == val.arity
proc classOf*[E](val: PreserveGen[E]): RecordClass[E] = proc classOf*(val: Preserve): RecordClass =
## Derive the ``RecordClass`` of ``val``. ## Derive the ``RecordClass`` of ``val``.
if val.kind != pkRecord: if val.kind != pkRecord:
raise newException(Defect, "cannot derive class of non-record value " & $val) raise newException(Defect, "cannot derive class of non-record value " & $val)
assert(val.record.len > 0) assert(val.record.len > 0)
RecordClass[E](label: val.label, arity: val.arity) RecordClass(label: val.label, arity: val.arity)
proc classOf*[T](x: T; E = void): RecordClass[E] = proc classOf*[T](x: T): RecordClass =
## Derive the ``RecordClass`` of ``x``. ## Derive the ``RecordClass`` of ``x``.
when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.} when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.}
result.label = preserves.symbol(T.getCustomPragmaVal(record), E) result.label = preserves.symbol(T.getCustomPragmaVal(record))
for k, v in x.fieldPairs: inc(result.arity) for k, v in x.fieldPairs: inc(result.arity)
proc classOf*(T: typedesc[tuple]): RecordClass = proc classOf*(T: typedesc[tuple]): RecordClass =

View File

@ -37,4 +37,4 @@ suite "parse":
let let
a = encode test a = encode test
b = bin b = bin
check(a.toHex == b.toHex) check(cast[string](a).toHex == b.toHex)