2021-08-24 15:29:29 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
2021-08-31 15:00:00 +00:00
|
|
|
# SPDX-License-Identifier: Unlicense
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
import std/[base64, endians, hashes, options, sets, sequtils, streams, tables, typetraits]
|
2022-03-11 05:39:29 +00:00
|
|
|
import ./preserves/private/macros
|
2021-06-28 10:30:16 +00:00
|
|
|
|
2021-08-28 09:31:33 +00:00
|
|
|
from std/json import escapeJson, escapeJsonUnquoted
|
2021-10-17 09:20:17 +00:00
|
|
|
from std/strutils import parseEnum
|
2021-11-03 18:17:33 +00:00
|
|
|
import ./preserves/private/dot
|
2021-10-17 09:20:17 +00:00
|
|
|
|
|
|
|
when defined(tracePreserves):
|
2022-12-08 04:03:58 +00:00
|
|
|
when defined(posix):
|
|
|
|
template trace(args: varargs[untyped]) = {.cast(noSideEffect).}: stderr.writeLine(args)
|
|
|
|
else:
|
|
|
|
template trace(args: varargs[untyped]) = {.cast(noSideEffect).}: echo(args)
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
|
|
|
template trace(args: varargs[untyped]) = discard
|
2021-08-28 09:17:23 +00:00
|
|
|
|
2021-06-02 13:51:36 +00:00
|
|
|
type
|
2021-06-08 10:14:56 +00:00
|
|
|
PreserveKind* = enum
|
2021-11-12 11:25:28 +00:00
|
|
|
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol,
|
2021-09-23 13:20:06 +00:00
|
|
|
pkRecord, pkSequence, pkSet, pkDictionary, pkEmbedded
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2021-09-01 09:37:49 +00:00
|
|
|
const
|
2021-11-12 11:25:28 +00:00
|
|
|
atomKinds* = {pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol}
|
2021-09-01 09:37:49 +00:00
|
|
|
compoundKinds* = {pkRecord, pkSequence, pkSet, pkDictionary}
|
|
|
|
|
2022-02-20 04:33:15 +00:00
|
|
|
type Symbol* = distinct string
|
|
|
|
proc `$`*(s: Symbol): string {.borrow.}
|
|
|
|
proc `<`*(x, y: Symbol): bool {.borrow.}
|
|
|
|
proc `==`*(x, y: Symbol): bool {.borrow.}
|
|
|
|
proc len*(s: Symbol): int {.borrow.}
|
|
|
|
proc hash*(s: Symbol): Hash {.borrow.}
|
|
|
|
|
2021-09-01 09:37:49 +00:00
|
|
|
type
|
2021-10-25 17:07:16 +00:00
|
|
|
Preserve*[E = void] = object
|
2022-12-07 19:10:14 +00:00
|
|
|
embedded*: bool
|
|
|
|
## Flag to mark embedded Preserves
|
2021-06-02 13:51:36 +00:00
|
|
|
case kind*: PreserveKind
|
|
|
|
of pkBoolean:
|
|
|
|
bool*: bool
|
|
|
|
of pkFloat:
|
|
|
|
float*: float32
|
|
|
|
of pkDouble:
|
|
|
|
double*: float64
|
|
|
|
of pkSignedInteger:
|
|
|
|
int*: BiggestInt
|
|
|
|
of pkString:
|
|
|
|
string*: string
|
|
|
|
of pkByteString:
|
|
|
|
bytes*: seq[byte]
|
|
|
|
of pkSymbol:
|
2022-02-20 04:33:15 +00:00
|
|
|
symbol*: Symbol
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkRecord:
|
2021-09-23 13:20:06 +00:00
|
|
|
record*: seq[Preserve[E]] # label is last
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSequence:
|
2021-09-23 13:20:06 +00:00
|
|
|
sequence*: seq[Preserve[E]]
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSet:
|
2021-09-23 13:20:06 +00:00
|
|
|
set*: seq[Preserve[E]]
|
2021-09-08 09:00:38 +00:00
|
|
|
# TODO: HashSet
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkDictionary:
|
2021-09-23 13:20:06 +00:00
|
|
|
dict*: seq[DictEntry[E]]
|
2021-09-08 09:00:38 +00:00
|
|
|
# TODO: Tables
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
|
|
|
embed*: E
|
|
|
|
|
|
|
|
DictEntry[E] = tuple[key: Preserve[E], val: Preserve[E]]
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc `==`*[A, B](x: Preserve[A]; y: Preserve[B]): bool =
|
2021-09-08 09:00:38 +00:00
|
|
|
## Check `x` and `y` for equivalence.
|
|
|
|
if x.kind == y.kind and x.embedded == y.embedded:
|
|
|
|
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 pkString:
|
|
|
|
result = x.string == y.string
|
|
|
|
of pkByteString:
|
|
|
|
result = x.bytes == y.bytes
|
|
|
|
of pkSymbol:
|
|
|
|
result = x.symbol == y.symbol
|
|
|
|
of pkRecord:
|
2021-10-25 19:30:55 +00:00
|
|
|
result = x.record.len == y.record.len
|
|
|
|
for i in 0..x.record.high:
|
|
|
|
if not result: break
|
|
|
|
result = result and (x.record[i] == y.record[i])
|
2021-09-08 09:00:38 +00:00
|
|
|
of pkSequence:
|
|
|
|
for i, val in x.sequence:
|
|
|
|
if y.sequence[i] != val: return false
|
|
|
|
result = true
|
|
|
|
of pkSet:
|
2021-10-25 19:30:55 +00:00
|
|
|
result = x.set.len == y.set.len
|
|
|
|
for i in 0..x.set.high:
|
|
|
|
if not result: break
|
|
|
|
result = result and (x.set[i] == y.set[i])
|
2021-09-08 09:00:38 +00:00
|
|
|
of pkDictionary:
|
2021-10-25 19:30:55 +00:00
|
|
|
result = x.dict.len == y.dict.len
|
|
|
|
for i in 0..x.dict.high:
|
|
|
|
if not result: break
|
|
|
|
result = result and
|
|
|
|
(x.dict[i].key == y.dict[i].key) and
|
|
|
|
(x.dict[i].val == y.dict[i].val)
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
|
|
|
when A is B:
|
|
|
|
when A is void:
|
|
|
|
result = true
|
|
|
|
else:
|
|
|
|
result = x.embed == y.embed
|
2021-07-09 10:24:51 +00:00
|
|
|
|
2021-06-02 13:51:36 +00:00
|
|
|
proc `<`(x, y: string | seq[byte]): bool =
|
|
|
|
for i in 0 .. min(x.high, y.high):
|
2021-08-28 10:48:50 +00:00
|
|
|
if x[i] < y[i]: return true
|
|
|
|
if x[i] != y[i]: return false
|
2021-06-02 13:51:36 +00:00
|
|
|
x.len < y.len
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc `<`*[A, B](x: Preserve[A]; y: Preserve[B]): bool =
|
2021-10-25 17:07:16 +00:00
|
|
|
## Preserves have a total order over values. Check if `x` is ordered before `y`.
|
2021-09-08 09:00:38 +00:00
|
|
|
if x.embedded != y.embedded:
|
|
|
|
result = y.embedded
|
|
|
|
elif x.kind != y.kind:
|
2021-11-12 11:25:28 +00:00
|
|
|
result = x.kind < y.kind
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
case x.kind
|
|
|
|
of pkBoolean:
|
|
|
|
result = (not x.bool) and y.bool
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkFloat:
|
|
|
|
result = x.float < y.float
|
|
|
|
of pkDouble:
|
|
|
|
result = x.double < y.double
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSignedInteger:
|
|
|
|
result = x.int < y.int
|
|
|
|
of pkString:
|
|
|
|
result = x.string < y.string
|
|
|
|
of pkByteString:
|
|
|
|
result = x.bytes < y.bytes
|
|
|
|
of pkSymbol:
|
|
|
|
result = x.symbol < y.symbol
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkRecord:
|
|
|
|
if x.record[x.record.high] < y.record[y.record.high]: return true
|
|
|
|
for i in 0..<min(x.record.high, y.record.high):
|
|
|
|
if x.record[i] < y.record[i]: return true
|
2021-09-08 09:00:38 +00:00
|
|
|
if x.record[i] == y.record[i]: return false
|
2021-08-28 10:48:50 +00:00
|
|
|
result = x.record.len < y.record.len
|
|
|
|
of pkSequence:
|
|
|
|
for i in 0..min(x.sequence.high, y.sequence.high):
|
|
|
|
if x.sequence[i] < y.sequence[i]: return true
|
|
|
|
if x.sequence[i] != y.sequence[i]: return false
|
|
|
|
result = x.sequence.len < y.sequence.len
|
|
|
|
of pkSet:
|
|
|
|
for i in 0..min(x.set.high, y.set.high):
|
|
|
|
if x.set[i] < y.set[i]: return true
|
|
|
|
if x.set[i] != y.set[i]: return false
|
|
|
|
result = x.set.len < y.set.len
|
|
|
|
of pkDictionary:
|
|
|
|
for i in 0..min(x.dict.high, y.dict.high):
|
|
|
|
if x.dict[i].key < y.dict[i].key: return true
|
|
|
|
if x.dict[i].key == y.dict[i].key:
|
|
|
|
if x.dict[i].val < y.dict[i].val: return true
|
|
|
|
if x.dict[i].val != y.dict[i].val: return false
|
|
|
|
result = x.dict.len < y.dict.len
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
|
|
|
when (not A is void) and (A is B):
|
|
|
|
result = x.embed < y.embed
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc hash*(pr: Preserve): Hash =
|
|
|
|
## Produce a `Hash` of `pr` for use with a `HashSet` or `Table`.
|
|
|
|
var h = hash(pr.kind.int) !& hash(pr.embedded)
|
|
|
|
case pr.kind
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkBoolean:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.bool)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkFloat:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.float)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkDouble:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.double)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSignedInteger:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.int)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkString:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.string)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkByteString:
|
2021-09-08 09:00:38 +00:00
|
|
|
h = h !& hash(pr.bytes)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSymbol:
|
2022-02-20 04:33:15 +00:00
|
|
|
h = h !& hash(string pr.symbol)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkRecord:
|
2021-09-08 09:00:38 +00:00
|
|
|
for val in pr.record:
|
2021-08-28 10:48:50 +00:00
|
|
|
h = h !& hash(val)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSequence:
|
2021-09-08 09:00:38 +00:00
|
|
|
for val in pr.sequence:
|
2021-08-28 10:48:50 +00:00
|
|
|
h = h !& hash(val)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSet:
|
2021-09-08 09:00:38 +00:00
|
|
|
for val in pr.set.items:
|
2021-08-28 10:48:50 +00:00
|
|
|
h = h !& hash(val)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkDictionary:
|
2021-09-08 09:00:38 +00:00
|
|
|
for (key, val) in pr.dict.items:
|
2021-08-28 10:48:50 +00:00
|
|
|
h = h !& hash(key) !& hash(val)
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
|
|
|
h = h !& hash(pr.embed)
|
2021-08-28 10:48:50 +00:00
|
|
|
!$h
|
2021-06-15 10:48:20 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc `[]`*(pr: Preserve; i: int): Preserve =
|
2021-09-23 12:29:16 +00:00
|
|
|
## Select an indexed value from ``pr``.
|
2021-09-01 09:37:49 +00:00
|
|
|
## Only valid for records and sequences.
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkRecord: pr.record[i]
|
|
|
|
of pkSequence: pr.sequence[i]
|
2021-09-23 12:29:16 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "`Preserves value is not indexable")
|
|
|
|
|
2021-11-11 10:11:40 +00:00
|
|
|
proc `[]=`*(pr: var Preserve; i: Natural; val: Preserve) =
|
|
|
|
## Assign an indexed value into ``pr``.
|
|
|
|
## Only valid for records and sequences.
|
|
|
|
case pr.kind
|
|
|
|
of pkRecord: pr.record[i] = val
|
|
|
|
of pkSequence: pr.sequence[i] = val
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "`Preserves value is not indexable")
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
proc getOrDefault(pr: Preserve; key: Preserve): Preserve =
|
|
|
|
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
|
|
|
|
## or returns the `#f` Preserves value.
|
|
|
|
if pr.kind == pkDictionary:
|
|
|
|
for (k, v) in pr.dict:
|
|
|
|
if key == k:
|
|
|
|
result = v
|
|
|
|
break
|
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc incl*(pr: var Preserve; key: Preserve) =
|
|
|
|
## Include `key` in the Preserves set `pr`.
|
|
|
|
for i in 0..pr.set.high:
|
|
|
|
if key < pr.set[i]:
|
|
|
|
insert(pr.set, [key], i)
|
2021-08-28 10:48:50 +00:00
|
|
|
return
|
2021-09-08 09:00:38 +00:00
|
|
|
pr.set.add(key)
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc excl*(pr: var Preserve; key: Preserve) =
|
|
|
|
## Exclude `key` from the Preserves set `pr`.
|
|
|
|
for i in 0..pr.set.high:
|
|
|
|
if pr.set[i] == key:
|
|
|
|
delete(pr.set, i, i)
|
2021-08-28 10:48:50 +00:00
|
|
|
break
|
|
|
|
|
2022-10-25 22:42:04 +00:00
|
|
|
proc `[]`*(pr, key: Preserve): Preserve {.deprecated: "use step instead".} =
|
2022-03-18 14:44:20 +00:00
|
|
|
## Select a value by `key` from `pr`.
|
|
|
|
## Works for sequences, records, and dictionaries.
|
2022-03-08 21:24:14 +00:00
|
|
|
if pr.isDictionary:
|
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k == key: return v
|
2022-03-14 19:16:38 +00:00
|
|
|
raise newException(KeyError, "value not in Preserves dictionary")
|
2022-03-08 21:24:14 +00:00
|
|
|
elif (pr.isRecord or pr.isSequence) and key.isInteger:
|
|
|
|
result = pr[int key.int]
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "invalid Preserves indexing")
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2022-03-18 14:44:20 +00:00
|
|
|
func step*(pr, idx: Preserve): Option[Preserve] =
|
|
|
|
## Step into `pr` by index `idx`.
|
|
|
|
## Works for sequences, records, and dictionaries.
|
|
|
|
runnableExamples:
|
2022-03-18 16:26:52 +00:00
|
|
|
import std/options
|
2022-03-18 14:44:20 +00:00
|
|
|
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
|
|
|
|
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
|
|
|
|
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Preserve[void])
|
|
|
|
if pr.isDictionary:
|
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k == idx:
|
|
|
|
result = some(v)
|
|
|
|
break
|
|
|
|
elif (pr.isRecord or pr.isSequence) and idx.isInteger:
|
|
|
|
let i = int idx.int
|
|
|
|
if i < pr.len:
|
|
|
|
result = some(pr[i])
|
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc `[]=`*(pr: var Preserve; key, val: Preserve) =
|
|
|
|
## Insert `val` by `key` in the Preserves dictionary `pr`.
|
|
|
|
for i in 0..pr.dict.high:
|
|
|
|
if key < pr.dict[i].key:
|
|
|
|
insert(pr.dict, [(key, val, )], i)
|
2021-08-28 10:48:50 +00:00
|
|
|
return
|
2021-09-08 09:00:38 +00:00
|
|
|
elif key == pr.dict[i].key:
|
|
|
|
pr.dict[i].val = val
|
2021-08-28 10:48:50 +00:00
|
|
|
return
|
2021-09-08 09:00:38 +00:00
|
|
|
pr.dict.add((key, val, ))
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2022-03-08 21:24:14 +00:00
|
|
|
proc mget*(pr: var Preserve; key: Preserve): var Preserve =
|
|
|
|
## Select a value by `key` from the Preserves dictionary `pr`.
|
|
|
|
if pr.isDictionary:
|
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k == key: return v
|
|
|
|
raise newException(KeyError, "key not in Preserves dictionary")
|
|
|
|
raise newException(ValueError, "not a Preserves dictionary")
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
proc toSymbol*(s: sink string; E = void): Preserve[E] {.inline.} =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves symbol value.
|
2022-02-20 04:33:15 +00:00
|
|
|
Preserve[E](kind: pkSymbol, symbol: Symbol s)
|
2021-09-01 09:37:49 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc initRecord*[E](label: Preserve[E]; arity: Natural = 0): Preserve[E] =
|
2021-09-23 12:29:31 +00:00
|
|
|
## Create a Preserves record value.
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkRecord, record: newSeq[Preserve[E]](arity.succ))
|
2021-09-23 12:29:31 +00:00
|
|
|
result.record[arity] = label
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc initRecord*[E](label: Preserve[E]; args: varargs[Preserve[E]]): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves record value.
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkRecord,
|
|
|
|
record: newSeqOfCap[Preserve[E]](1+args.len))
|
2021-08-28 10:48:50 +00:00
|
|
|
for arg in args:
|
|
|
|
result.record.add(arg)
|
|
|
|
result.record.add(label)
|
|
|
|
|
2021-11-11 10:11:40 +00:00
|
|
|
proc initRecord*[E](label: string; args: varargs[Preserve[E]]): Preserve[E] {.inline.} =
|
|
|
|
## Create a Preserves record value.
|
|
|
|
initRecord(toSymbol(label, E), args)
|
|
|
|
|
2023-03-29 22:32:54 +00:00
|
|
|
proc initSequence*(len: Natural = 0; E = void): Preserve[E] =
|
2021-09-23 12:29:31 +00:00
|
|
|
## Create a Preserves sequence value.
|
2021-09-23 13:20:06 +00:00
|
|
|
Preserve[E](kind: pkSequence, sequence: newSeq[Preserve[E]](len))
|
2021-09-23 12:29:31 +00:00
|
|
|
|
2023-03-29 22:33:39 +00:00
|
|
|
proc initSequenceOfCap*(cap: Natural; E = void): Preserve[E] =
|
|
|
|
## Create a Preserves sequence value.
|
|
|
|
Preserve[E](kind: pkSequence, sequence: newSeqOfCap[Preserve[E]](cap))
|
|
|
|
|
2023-03-29 22:32:54 +00:00
|
|
|
proc initSet*(E = void): Preserve[E] = Preserve[E](kind: pkSet)
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves set value.
|
|
|
|
|
2023-03-29 22:32:54 +00:00
|
|
|
proc initDictionary*(E = void): Preserve[E] = Preserve[E](kind: pkDictionary)
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves dictionary value.
|
|
|
|
|
2023-04-03 22:03:56 +00:00
|
|
|
proc toDictionary*[E](pairs: openArray[(string, Preserve[E])]): Preserve[E] =
|
|
|
|
## Create a Preserves dictionary value.
|
|
|
|
result = Preserve[E](kind: pkDictionary)
|
|
|
|
for (key, val) in pairs: result[toSymbol(key, E)] = val
|
|
|
|
|
2021-10-20 20:15:11 +00:00
|
|
|
proc embed*[E](pr: sink Preserve[E]): Preserve[E] =
|
2021-09-23 13:20:06 +00:00
|
|
|
## Create a Preserves value that embeds ``e``.
|
2021-10-20 20:15:11 +00:00
|
|
|
result = pr
|
|
|
|
result.embedded = true
|
2021-09-23 13:20:06 +00:00
|
|
|
|
2021-10-20 20:15:11 +00:00
|
|
|
proc embed*[E](e: sink E): Preserve[E] =
|
|
|
|
## Create a Preserves value that embeds ``e``.
|
|
|
|
Preserve[E](kind: pkEmbedded, embed: e)
|
2021-10-17 09:20:17 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc len*(pr: Preserve): int =
|
|
|
|
## Return the shallow count of values in ``pr``, that is the number of
|
2021-09-01 09:37:49 +00:00
|
|
|
## fields in a record, items in a sequence, items in a set, or key-value pairs
|
|
|
|
## in a dictionary.
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkRecord: pr.record.len.pred
|
|
|
|
of pkSequence: pr.sequence.len
|
|
|
|
of pkSet: pr.set.len
|
|
|
|
of pkDictionary: pr.dict.len
|
2021-08-28 10:48:50 +00:00
|
|
|
else: 0
|
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
iterator items*(pr: Preserve): Preserve =
|
|
|
|
## Shallow iterator over `pr`, yield the fields in a record,
|
2021-09-01 09:37:49 +00:00
|
|
|
## the items of a sequence, the items of a set, or the pairs
|
|
|
|
## of a dictionary.
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkRecord:
|
2021-09-08 09:00:38 +00:00
|
|
|
for i in 0..pr.record.high.pred:
|
|
|
|
yield pr.record[i]
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkSequence:
|
2021-09-08 09:00:38 +00:00
|
|
|
for e in pr.sequence.items: yield e
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkSet:
|
2021-09-08 09:00:38 +00:00
|
|
|
for e in pr.set.items: yield e
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkDictionary:
|
2021-09-08 09:00:38 +00:00
|
|
|
for (k, v) in pr.dict.items:
|
2021-07-09 10:24:18 +00:00
|
|
|
yield k # key can be an arbitrary Preserve
|
|
|
|
yield v
|
|
|
|
else: discard
|
|
|
|
|
2021-11-11 10:11:40 +00:00
|
|
|
iterator pairs*[E](pr: Preserve[E]): DictEntry[E] =
|
|
|
|
assert(pr.kind == pkDictionary, "not a dictionary")
|
|
|
|
for i in 0..pr.dict.high: yield pr.dict[i]
|
|
|
|
|
2021-09-23 11:32:04 +00:00
|
|
|
func isBoolean*(pr: Preserve): bool {.inline.} = pr.kind == pkBoolean
|
|
|
|
## Check if ``pr`` is a Preserve boolean.
|
|
|
|
|
2021-10-25 17:07:16 +00:00
|
|
|
func isFalse*(pr: Preserve): bool {.inline.} =
|
2021-09-08 09:00:38 +00:00
|
|
|
## Check if ``pr`` is equivalent to the zero-initialized ``Preserve``.
|
|
|
|
pr.kind == pkBoolean and pr.bool == false
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2021-09-23 11:32:04 +00:00
|
|
|
func isFloat*(pr: Preserve): bool {.inline.} = pr.kind == pkFloat
|
|
|
|
## Check if ``pr`` is a Preserve float.
|
|
|
|
|
|
|
|
func isDouble*(pr: Preserve): bool {.inline.} = pr.kind == pkDouble
|
|
|
|
## Check if ``pr`` is a Preserve double.
|
|
|
|
|
2021-11-12 11:25:28 +00:00
|
|
|
func isInteger*(pr: Preserve): bool {.inline.} = pr.kind == pkSignedInteger
|
2021-09-23 11:32:04 +00:00
|
|
|
## Check if ``pr`` is a Preserve double.
|
|
|
|
|
|
|
|
func isString*(pr: Preserve): bool {.inline.} = pr.kind == pkString
|
|
|
|
## Check if ``pr`` is a Preserve text string.
|
|
|
|
|
|
|
|
func isByteString*(pr: Preserve): bool {.inline.} = pr.kind == pkByteString
|
|
|
|
## Check if ``pr`` is a Preserves byte string.
|
|
|
|
|
|
|
|
func isSymbol*(pr: Preserve): bool {.inline.} = pr.kind == pkSymbol
|
2021-09-08 09:00:38 +00:00
|
|
|
## Check if `pr` is a Preserves symbol.
|
2021-09-23 11:32:04 +00:00
|
|
|
|
2022-02-20 04:33:15 +00:00
|
|
|
func isSymbol*(pr: Preserve; sym: string|Symbol): bool {.inline.} =
|
2021-09-23 11:32:04 +00:00
|
|
|
## Check if ``pr`` is a Preserves symbol of ``sym``.
|
2022-02-20 04:33:15 +00:00
|
|
|
(pr.kind == pkSymbol) and (pr.symbol == Symbol(sym))
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2022-10-27 20:29:45 +00:00
|
|
|
proc label*(pr: Preserve): Preserve {.inline.} =
|
|
|
|
## Return the label of record value.
|
|
|
|
pr.record[pr.record.high]
|
|
|
|
|
|
|
|
proc arity*(pr: Preserve): int {.inline.} =
|
|
|
|
## Return the number of fields in record `pr`.
|
|
|
|
pred(pr.record.len)
|
|
|
|
|
2021-09-23 11:32:04 +00:00
|
|
|
func isRecord*(pr: Preserve): bool {.inline.} = (pr.kind == pkRecord) and (pr.record.len > 0)
|
|
|
|
## Check if ``pr`` is a Preserves record.
|
|
|
|
|
2022-10-27 20:29:45 +00:00
|
|
|
func isRecord*(pr: Preserve; label: string): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserves record with the given label symbol.
|
|
|
|
pr.kind == pkRecord and
|
|
|
|
pr.record.len > 0 and
|
|
|
|
pr.label.isSymbol(label)
|
|
|
|
|
2023-03-29 21:47:54 +00:00
|
|
|
func isRecord*(pr: Preserve; label: string; arity: Natural): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserves record with the given label symbol and field arity.
|
|
|
|
pr.kind == pkRecord and
|
|
|
|
pr.record.len == succ(arity) and
|
|
|
|
pr.label.isSymbol(label)
|
|
|
|
|
2021-09-23 11:32:04 +00:00
|
|
|
proc isSequence*(pr: Preserve): bool {.inline.} = pr.kind == pkSequence
|
|
|
|
## Check if ``pr`` is a Preserves sequence.
|
|
|
|
|
|
|
|
proc isSet*(pr: Preserve): bool {.inline.} = pr.kind == pkSet
|
|
|
|
## Check if ``pr`` is a Preserves set.
|
|
|
|
|
|
|
|
proc isDictionary*(pr: Preserve): bool {.inline.} = pr.kind == pkDictionary
|
|
|
|
## Check if ``pr`` is a Preserves dictionary.
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
func isEmbedded*[E](pr: Preserve[E]): bool {.inline.} =
|
2021-09-23 11:32:04 +00:00
|
|
|
## Check if ``pr`` is an embedded value.
|
2021-09-23 13:20:06 +00:00
|
|
|
when E is void: pr.embedded # embedded Preserves
|
|
|
|
else: pr.kind == pkEmbedded # embedded Nim
|
2021-08-25 11:49:35 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc fields*(pr: Preserve): seq[Preserve] {.inline.} =
|
2021-06-15 10:48:20 +00:00
|
|
|
## Return the fields of a record value.
|
2021-09-08 09:00:38 +00:00
|
|
|
pr.record[0..pr.record.high.pred]
|
2021-06-15 10:48:20 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
iterator fields*(pr: Preserve): Preserve =
|
2021-06-15 10:48:20 +00:00
|
|
|
## Iterate the fields of a record value.
|
2021-09-08 09:00:38 +00:00
|
|
|
for i in 0..<pr.record.high: yield pr.record[i]
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2021-10-24 23:53:12 +00:00
|
|
|
proc unembed*[E](pr: Preserve[E]): E =
|
|
|
|
## Unembed an `E` value from a `Preserve[E]` value.
|
|
|
|
if pr.kind != pkEmbedded:
|
|
|
|
raise newException(ValueError, "not an embedded value")
|
|
|
|
pr.embed
|
|
|
|
|
2021-06-02 13:51:36 +00:00
|
|
|
proc writeVarint(s: Stream; n: int) =
|
|
|
|
var n = n
|
|
|
|
while true:
|
|
|
|
let c = int8(n and 0x7f)
|
|
|
|
n = n shr 7
|
|
|
|
if n == 0:
|
|
|
|
s.write((char)c.char)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
s.write((char)c or 0x80)
|
|
|
|
|
|
|
|
proc readVarint(s: Stream): int =
|
|
|
|
var shift: int
|
|
|
|
while shift < (9*8):
|
|
|
|
let c = s.readChar.int
|
|
|
|
result = result or ((c and 0x7f) shl shift)
|
|
|
|
if (c and 0x80) == 0:
|
|
|
|
break
|
|
|
|
shift.inc 7
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc write*[E](str: Stream; pr: Preserve[E]) =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Write the binary-encoding of a Preserves value to a stream.
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.embedded: str.write(0x86'u8)
|
|
|
|
case pr.kind:
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkBoolean:
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.bool
|
2021-06-02 13:51:36 +00:00
|
|
|
of false: str.write(0x80'u8)
|
|
|
|
of true: str.write(0x81'u8)
|
|
|
|
of pkFloat:
|
|
|
|
str.write(0x82'u8)
|
|
|
|
when system.cpuEndian == bigEndian:
|
2021-09-08 09:00:38 +00:00
|
|
|
str.write(pr.float)
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
var be: float32
|
2021-09-08 09:00:38 +00:00
|
|
|
swapEndian32(be.addr, pr.float.unsafeAddr)
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(be)
|
|
|
|
of pkDouble:
|
|
|
|
str.write(0x83'u8)
|
|
|
|
when system.cpuEndian == bigEndian:
|
2021-09-08 09:00:38 +00:00
|
|
|
str.write(pr.double)
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
var be: float64
|
2021-09-08 09:00:38 +00:00
|
|
|
swapEndian64(be.addr, pr.double.unsafeAddr)
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(be)
|
|
|
|
of pkSignedInteger:
|
2021-09-08 09:00:38 +00:00
|
|
|
if (-3 <= pr.int) and (pr.int <= 12):
|
|
|
|
str.write(0x90'i8 or int8(if pr.int < 0: pr.int + 16 else: pr.int))
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
var bitCount = 1'u8
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.int < 0:
|
|
|
|
while ((not pr.int) shr bitCount) != 0:
|
2021-06-02 13:51:36 +00:00
|
|
|
inc(bitCount)
|
|
|
|
else:
|
2021-09-08 09:00:38 +00:00
|
|
|
while (pr.int shr bitCount) != 0:
|
2021-06-02 13:51:36 +00:00
|
|
|
inc(bitCount)
|
|
|
|
var byteCount = (bitCount + 8) div 8
|
|
|
|
str.write(0xa0'u8 or (byteCount - 1))
|
2021-06-04 09:39:52 +00:00
|
|
|
proc write(n: uint8; i: BiggestInt) =
|
2021-06-02 13:51:36 +00:00
|
|
|
if n > 0:
|
|
|
|
write(n.pred, i shr 8)
|
|
|
|
str.write(i.uint8)
|
2021-09-08 09:00:38 +00:00
|
|
|
write(byteCount, pr.int)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkString:
|
|
|
|
str.write(0xb1'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
str.writeVarint(pr.string.len)
|
|
|
|
str.write(pr.string)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkByteString:
|
|
|
|
str.write(0xb2'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
str.writeVarint(pr.bytes.len)
|
|
|
|
str.write(cast[string](pr.bytes))
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkSymbol:
|
|
|
|
str.write(0xb3'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
str.writeVarint(pr.symbol.len)
|
2022-02-20 04:33:15 +00:00
|
|
|
str.write(string pr.symbol)
|
2021-06-02 13:51:36 +00:00
|
|
|
of pkRecord:
|
2021-09-08 09:00:38 +00:00
|
|
|
assert(pr.record.len > 0)
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(0xb4'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
str.write(pr.record[pr.record.high])
|
|
|
|
for i in 0..<pr.record.high:
|
|
|
|
str.write(pr.record[i])
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(0x84'u8)
|
|
|
|
of pkSequence:
|
|
|
|
str.write(0xb5'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
for e in pr.sequence:
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(e)
|
|
|
|
str.write(0x84'u8)
|
|
|
|
of pkSet:
|
|
|
|
str.write(0xb6'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
for val in pr.set.items:
|
2021-06-15 14:02:14 +00:00
|
|
|
str.write(val)
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(0x84'u8)
|
|
|
|
of pkDictionary:
|
|
|
|
str.write(0xb7'u8)
|
2021-09-08 09:00:38 +00:00
|
|
|
for (key, value) in pr.dict.items:
|
2021-06-02 13:51:36 +00:00
|
|
|
str.write(key)
|
|
|
|
str.write(value)
|
|
|
|
str.write(0x84'u8)
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
2022-10-16 20:29:27 +00:00
|
|
|
str.write(0x86'u8)
|
|
|
|
str.write(pr.embed.toPreserve)
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc encode*[E](pr: Preserve[E]): seq[byte] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Return the binary-encoding of a Preserves value.
|
2021-07-16 17:11:19 +00:00
|
|
|
let s = newStringStream()
|
2021-09-08 09:00:38 +00:00
|
|
|
s.write pr
|
2022-10-16 20:29:27 +00:00
|
|
|
result = cast[seq[byte]](move s.data)
|
2021-07-16 17:11:19 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Decode a Preserves value from a binary-encoded stream.
|
2022-10-29 23:32:38 +00:00
|
|
|
if s.atEnd: raise newException(ValueError, "End of Preserves stream")
|
2021-06-02 13:51:36 +00:00
|
|
|
proc assertStream(check: bool) =
|
|
|
|
if not check:
|
|
|
|
raise newException(ValueError, "invalid Preserves stream")
|
|
|
|
const endMarker = 0x84
|
|
|
|
let tag = s.readUint8()
|
|
|
|
case tag
|
2021-09-23 13:20:06 +00:00
|
|
|
of 0x80: result = Preserve[E](kind: pkBoolean, bool: false)
|
|
|
|
of 0x81: result = Preserve[E](kind: pkBoolean, bool: true)
|
2021-06-02 13:51:36 +00:00
|
|
|
of 0x82:
|
|
|
|
when system.cpuEndian == bigEndian:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkFloat, float: s.readFloat32())
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkFloat)
|
2021-06-02 13:51:36 +00:00
|
|
|
var be = s.readFloat32()
|
|
|
|
swapEndian32(result.float.addr, be.addr)
|
|
|
|
of 0x83:
|
|
|
|
when system.cpuEndian == bigEndian:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkDouble, double: s.readFloat64())
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkDouble)
|
2021-06-02 13:51:36 +00:00
|
|
|
var be = s.readFloat64()
|
|
|
|
swapEndian64(result.double.addr, be.addr)
|
2022-11-02 18:58:37 +00:00
|
|
|
of 0x85:
|
|
|
|
discard decodePreserves(s, E)
|
|
|
|
while s.peekUint8() == 0x85:
|
|
|
|
discard s.readUint8()
|
|
|
|
discard decodePreserves(s, E)
|
2021-08-28 10:48:50 +00:00
|
|
|
of 0x86:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = decodePreserves(s, E)
|
2021-09-08 09:00:38 +00:00
|
|
|
result.embedded = true
|
2021-06-02 13:51:36 +00:00
|
|
|
of 0xb1:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkString)
|
2021-06-02 13:51:36 +00:00
|
|
|
let len = s.readVarint()
|
|
|
|
result.string = s.readStr(len)
|
|
|
|
of 0xb2:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkByteString)
|
2021-06-02 13:51:36 +00:00
|
|
|
let len = s.readVarint()
|
|
|
|
result.bytes = cast[seq[byte]](s.readStr(len))
|
|
|
|
of 0xb3:
|
|
|
|
let len = s.readVarint()
|
2022-02-20 04:33:15 +00:00
|
|
|
result = Preserve[E](kind: pkSymbol, symbol: Symbol s.readStr(len))
|
2021-06-02 13:51:36 +00:00
|
|
|
of 0xb4:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkRecord)
|
|
|
|
var label = decodePreserves(s, E)
|
2021-06-02 13:51:36 +00:00
|
|
|
while s.peekUint8() != endMarker:
|
2021-09-23 13:20:06 +00:00
|
|
|
result.record.add decodePreserves(s, E)
|
2021-08-28 10:48:50 +00:00
|
|
|
result.record.add(move label)
|
2021-06-02 13:51:36 +00:00
|
|
|
discard s.readUint8()
|
|
|
|
of 0xb5:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSequence)
|
2021-06-02 13:51:36 +00:00
|
|
|
while s.peekUint8() != endMarker:
|
2021-09-23 13:20:06 +00:00
|
|
|
result.sequence.add decodePreserves(s, E)
|
2021-06-02 13:51:36 +00:00
|
|
|
discard s.readUint8()
|
|
|
|
of 0xb6:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSet)
|
2021-06-02 13:51:36 +00:00
|
|
|
while s.peekUint8() != endMarker:
|
2021-09-23 13:20:06 +00:00
|
|
|
incl(result, decodePreserves(s, E))
|
2021-06-02 13:51:36 +00:00
|
|
|
discard s.readUint8()
|
|
|
|
of 0xb7:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkDictionary)
|
2021-06-02 13:51:36 +00:00
|
|
|
while s.peekUint8() != endMarker:
|
2021-09-23 13:20:06 +00:00
|
|
|
result[decodePreserves(s, E)] = decodePreserves(s, E)
|
2021-06-02 13:51:36 +00:00
|
|
|
discard s.readUint8()
|
|
|
|
of 0xb0:
|
|
|
|
let len = s.readVarint()
|
2021-11-12 11:25:28 +00:00
|
|
|
result = Preserve[E](kind: pkSignedInteger)
|
2021-06-04 09:39:52 +00:00
|
|
|
for _ in 1..len:
|
2021-11-12 11:25:28 +00:00
|
|
|
result.int = (result.int shl 8) + s.readUint8().BiggestInt
|
2021-08-28 10:48:50 +00:00
|
|
|
of endMarker:
|
|
|
|
assertStream(false)
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
case 0xf0 and tag
|
|
|
|
of 0x90:
|
|
|
|
var n = tag.BiggestInt
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSignedInteger,
|
2021-06-02 13:51:36 +00:00
|
|
|
int: n - (if n > 0x9c: 0xa0 else: 0x90))
|
|
|
|
of 0xa0:
|
|
|
|
let len = (tag.int and 0x0f) + 1
|
2021-11-12 11:25:28 +00:00
|
|
|
if len > 8: raise newException(ValueError,
|
|
|
|
"numbers wider than 64-bits not supported by this Preserves implementation")
|
|
|
|
result = Preserve[E](kind: pkSignedInteger, int: s.readUint8().BiggestInt)
|
|
|
|
if (result.int and 0x80) != 0: result.int.dec(0x100)
|
|
|
|
for i in 1..<len:
|
|
|
|
result.int = (result.int shl 8) or s.readUint8().BiggestInt
|
2021-06-02 13:51:36 +00:00
|
|
|
else:
|
|
|
|
assertStream(false)
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc decodePreserves*(s: string; E = void): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Decode a string of binary-encoded Preserves.
|
2021-09-23 13:20:06 +00:00
|
|
|
decodePreserves(s.newStringStream, E)
|
2021-07-16 17:11:19 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc decodePreserves*(s: seq[byte]; E = void): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Decode a byte-string of binary-encoded Preserves.
|
2021-09-23 13:20:06 +00:00
|
|
|
decodePreserves(cast[string](s), E)
|
2021-07-15 12:24:30 +00:00
|
|
|
|
2022-10-22 23:40:24 +00:00
|
|
|
type BufferedDecoder* = object
|
|
|
|
## Type for buffering binary Preserves before decoding.
|
|
|
|
stream: StringStream
|
|
|
|
decodePosition, maxSize: int
|
|
|
|
|
|
|
|
proc newBufferedDecoder*(maxSize = 4096): BufferedDecoder =
|
|
|
|
## Create a new `newBufferedDecoder`.
|
|
|
|
runnableExamples:
|
|
|
|
var
|
|
|
|
buf = newBufferedDecoder()
|
|
|
|
bin = encode(parsePreserves("<foobar>"))
|
|
|
|
buf.feed(bin[0..2])
|
|
|
|
buf.feed(bin[3..bin.high])
|
|
|
|
var (success, pr) = decode(buf)
|
|
|
|
assert success
|
|
|
|
assert $pr == "<foobar>"
|
|
|
|
BufferedDecoder(stream: newStringStream(), maxSize: maxSize)
|
|
|
|
|
2022-10-24 04:39:37 +00:00
|
|
|
proc feed*(dec: var BufferedDecoder; buf: pointer; len: int) =
|
|
|
|
if dec.maxSize < (dec.stream.getPosition + len):
|
2022-10-22 23:40:24 +00:00
|
|
|
raise newException(IOError, "BufferedDecoder at maximum buffer size")
|
2022-10-24 04:39:37 +00:00
|
|
|
dec.stream.writeData(buf, len)
|
|
|
|
|
|
|
|
proc feed*[T: byte|char](dec: var BufferedDecoder; data: openarray[T]) =
|
|
|
|
dec.feed(unsafeAddr data[0], data.len)
|
2022-10-22 23:40:24 +00:00
|
|
|
|
|
|
|
proc decode*(dec: var BufferedDecoder; E = void): (bool, Preserve[E]) =
|
|
|
|
## Decode from `dec`. If decoding fails the internal position of the
|
|
|
|
## decode does not advance.
|
|
|
|
var appendPos = dec.stream.getPosition
|
|
|
|
dec.stream.setPosition(dec.decodePosition)
|
|
|
|
try:
|
|
|
|
result[1] = decodePreserves(dec.stream, E)
|
|
|
|
result[0] = true
|
|
|
|
if dec.stream.getPosition == appendPos:
|
|
|
|
dec.stream.setPosition(0)
|
|
|
|
dec.decodePosition = 0
|
|
|
|
else:
|
|
|
|
dec.decodePosition = dec.stream.getPosition
|
|
|
|
dec.stream.setPosition(appendPos)
|
2023-03-22 16:19:23 +00:00
|
|
|
except IOError, ValueError:
|
2022-10-22 23:40:24 +00:00
|
|
|
dec.stream.setPosition(appendPos)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesRecord*(label: string) {.pragma.}
|
|
|
|
## Serialize this object or tuple as a record.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesRecordPragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `{.preservesRecord: "…".}` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesRecord)
|
|
|
|
|
|
|
|
proc recordLabel*(T: typedesc): string =
|
|
|
|
## Get the record label set by a pragma on a type.
|
|
|
|
runnableExamples:
|
|
|
|
type Foo {.preservesRecord: "bar".} = object
|
|
|
|
n: int
|
|
|
|
assert recordLabel(Foo) == "bar"
|
|
|
|
T.getCustomPragmaVal(preservesRecord)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesTuple*() {.pragma.}
|
|
|
|
## Serialize this object or tuple as a tuple.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
|
|
|
template preservesTupleTail*() {.pragma.}
|
|
|
|
## Serialize this object field to the end of its containing tuple.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesTuplePragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `preservesTuple` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesTuple)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesDictionary*() {.pragma.}
|
|
|
|
## Serialize this object or tuple as a dictionary.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesDictionaryPragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `preservesDictionary` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesDictionary)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesOr*() {.pragma.}
|
|
|
|
## Serialize this object as an ``or`` alternative.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
|
|
|
template preservesLiteral*(value: typed) {.pragma.}
|
|
|
|
## Serialize a Preserves literal within this object.
|
|
|
|
## See ``toPreserve``.
|
2021-06-28 10:30:16 +00:00
|
|
|
|
2022-12-06 19:31:21 +00:00
|
|
|
template preservesEmbedded*() {.pragma.}
|
|
|
|
## Pragma to mark a value as embedded by `toPreserve`.
|
|
|
|
|
2021-07-01 10:47:30 +00:00
|
|
|
template unpreservable*() {.pragma.}
|
2021-09-01 09:37:49 +00:00
|
|
|
## Pragma to forbid a type from being converted by ``toPreserve``.
|
2021-10-25 19:33:31 +00:00
|
|
|
## Useful for preventing an embeded type from being encoded
|
|
|
|
## as its native type.
|
|
|
|
## Unpreservability is asserted at runtime.
|
2021-07-01 10:47:30 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc toPreserve*[T](x: T; E = void): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Serializes ``x`` to Preserves. Can be customized by defining
|
2021-09-23 13:20:06 +00:00
|
|
|
## ``toPreserveHook(x: T; E: typedesc)`` in the calling scope.
|
|
|
|
## Any ``toPreserveHook`` that does not compile will be discarded;
|
|
|
|
## *Write tests for your hooks!*
|
2021-10-25 17:07:16 +00:00
|
|
|
##
|
|
|
|
## When `tracePreserves` is defined (`-d:tracePreserves`) a diagnostic
|
|
|
|
## trace is printing during `toPreserve`.
|
2021-09-23 13:20:06 +00:00
|
|
|
when (T is Preserve[E]): result = x
|
2022-12-07 07:18:51 +00:00
|
|
|
elif T is E:
|
|
|
|
when E is void: {.error: "cannot embed void".}
|
|
|
|
result = embed(x)
|
2022-12-07 19:10:14 +00:00
|
|
|
elif T is Preserve[void]:
|
|
|
|
result = cast[Preserve[E]](x)
|
2021-09-23 13:20:06 +00:00
|
|
|
elif compiles(toPreserveHook(x, E)):
|
|
|
|
result = toPreserveHook(x, E)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is enum:
|
|
|
|
result = toSymbol($x, E)
|
2021-08-25 11:49:35 +00:00
|
|
|
elif T is seq[byte]:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkByteString, bytes: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is array | seq:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSequence, sequence: newSeqOfCap[Preserve[E]](x.len))
|
|
|
|
for v in x.items: result.sequence.add(toPreserve(v, E))
|
2022-05-18 18:56:41 +00:00
|
|
|
elif T is set:
|
|
|
|
result = Preserve[E](kind: pkSet, set: newSeqOfCap[Preserve[E]](x.len))
|
|
|
|
for v in x.items: result.incl(toPreserve(v, E))
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is bool:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkBoolean, bool: x)
|
2022-03-08 21:25:03 +00:00
|
|
|
elif T is float32:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkFloat, float: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is float64:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkDouble, double: x)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is tuple:
|
|
|
|
result = Preserve[E](kind: pkSequence,
|
|
|
|
sequence: newSeqOfCap[Preserve[E]](tupleLen(T)))
|
|
|
|
for xf in fields(x):
|
|
|
|
result.sequence.add(toPreserve(xf, E))
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is Ordinal:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSignedInteger, int: x.ord.BiggestInt)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is ptr | ref:
|
2021-10-17 09:20:17 +00:00
|
|
|
if system.`==`(x, nil): result = toSymbol("null", E)
|
2021-09-23 13:20:06 +00:00
|
|
|
else: result = toPreserve(x[], E)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is string:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkString, string: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is SomeInteger:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSignedInteger, int: x.BiggestInt)
|
2022-02-20 04:33:15 +00:00
|
|
|
elif T is Symbol:
|
|
|
|
result = Preserve[E](kind: pkSymbol, symbol: x)
|
|
|
|
elif T is distinct:
|
|
|
|
result = toPreserve(x.distinctBase, E)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is object:
|
2022-12-06 19:31:21 +00:00
|
|
|
template applyEmbed(key: string; v: var Preserve[E]) {.used.} =
|
|
|
|
when x.dot(key).hasCustomPragma(preservesEmbedded):
|
|
|
|
v.embedded = true
|
2021-10-17 15:22:44 +00:00
|
|
|
template fieldToPreserve(key: string; val: typed): Preserve {.used.} =
|
2022-02-20 04:33:15 +00:00
|
|
|
when x.dot(key).hasCustomPragma(preservesLiteral):
|
2021-10-25 19:30:55 +00:00
|
|
|
const lit = parsePreserves(x.dot(key).getCustomPragmaVal(preservesLiteral))
|
|
|
|
cast[Preserve[E]](lit)
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
|
|
|
toPreserve(val, E)
|
2021-10-25 19:33:31 +00:00
|
|
|
when T.hasCustomPragma(unpreservable):
|
2022-10-16 20:29:27 +00:00
|
|
|
raiseAssert($T & " is unpreservable")
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesOr):
|
|
|
|
var hasKind, hasVariant: bool
|
|
|
|
for k, v in x.fieldPairs:
|
|
|
|
if k == "orKind":
|
|
|
|
assert(not hasKind)
|
|
|
|
hasKind = true
|
|
|
|
else:
|
|
|
|
assert(hasKind and not hasVariant)
|
|
|
|
result = fieldToPreserve(k, v)
|
2022-12-06 19:31:21 +00:00
|
|
|
applyEmbed(k, result)
|
2021-10-17 09:20:17 +00:00
|
|
|
hasVariant = true
|
|
|
|
elif T.hasCustomPragma(preservesRecord):
|
|
|
|
result = Preserve[E](kind: pkRecord)
|
|
|
|
for k, v in x.fieldPairs:
|
2022-12-06 19:31:21 +00:00
|
|
|
var pr = fieldToPreserve(k, v)
|
|
|
|
applyEmbed(k, pr)
|
|
|
|
result.record.add(pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
result.record.add(tosymbol(T.getCustomPragmaVal(preservesRecord), E))
|
|
|
|
elif T.hasCustomPragma(preservesTuple):
|
2023-03-29 22:32:54 +00:00
|
|
|
result = initSequence(0, E)
|
2021-10-17 09:20:17 +00:00
|
|
|
for label, field in x.fieldPairs:
|
|
|
|
when x.dot(label).hasCustomPragma(preservesTupleTail):
|
|
|
|
for y in field.items:
|
|
|
|
result.sequence.add(toPreserve(y, E))
|
|
|
|
# TODO: what if there are fields after the tail?
|
|
|
|
else:
|
2022-12-06 19:31:21 +00:00
|
|
|
var pr = fieldToPreserve(label, field)
|
|
|
|
applyEmbed(label, pr)
|
|
|
|
result.sequence.add(pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesDictionary):
|
2023-03-29 22:32:54 +00:00
|
|
|
result = initDictionary(E)
|
2021-10-17 09:20:17 +00:00
|
|
|
for key, val in x.fieldPairs:
|
2022-12-06 19:31:21 +00:00
|
|
|
var pr = fieldToPreserve(key, val)
|
|
|
|
applyEmbed(key, pr)
|
|
|
|
result[toSymbol(key, E)] = pr
|
2022-12-07 02:22:01 +00:00
|
|
|
else:
|
|
|
|
{.warning: "failed to preserve " & $T .}
|
|
|
|
result = toPreserveHook(x, E)
|
|
|
|
else:
|
|
|
|
{.warning: "failed to preserve " & $T .}
|
|
|
|
result = toPreserveHook(x, E)
|
2021-10-17 09:20:17 +00:00
|
|
|
# the hook doesn't compile but produces a useful error
|
|
|
|
trace T, " -> ", result
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc toPreserveHook*[T](set: HashSet[T]; E: typedesc): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Hook for preserving ``HashSet``.
|
2021-09-23 13:20:06 +00:00
|
|
|
result = Preserve[E](kind: pkSet, set: newSeqOfCap[Preserve[E]](set.len))
|
|
|
|
for e in set: result.incl toPreserve(e, E)
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc toPreserveHook*[A, B](table: Table[A, B]|TableRef[A, B], E: typedesc): Preserve[E] =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Hook for preserving ``Table``.
|
2023-03-29 22:32:54 +00:00
|
|
|
result = initDictionary(E)
|
2021-10-17 09:20:17 +00:00
|
|
|
for k, v in table.pairs:
|
2022-02-20 04:33:15 +00:00
|
|
|
result[toPreserve(k, E)] = toPreserve(v, E)
|
2021-08-11 11:00:32 +00:00
|
|
|
|
2022-03-18 17:25:44 +00:00
|
|
|
func containsNativeEmbeds[E](pr: Preserve[E]): bool =
|
|
|
|
## Check if a `Preserve[E]` is convertible to `Preserve[void]`.
|
|
|
|
when not E is void:
|
|
|
|
if pr.kind in compoundKinds:
|
|
|
|
for e in pr.items:
|
|
|
|
if e.containsNativeEmbeds:
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
elif pr.kind == pkEmbedded:
|
|
|
|
result = true
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc fromPreserve*[T, E](v: var T; pr: Preserve[E]): bool =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Inplace version of `preserveTo`. Returns ``true`` on
|
|
|
|
## a complete match, otherwise returns ``false``.
|
2021-09-23 13:20:06 +00:00
|
|
|
## Can be customized with `fromPreserveHook[E](x: T; var pr: Preserve[E]): bool`.
|
|
|
|
## Any ``fromPreserveHook`` that does not compile will be discarded;
|
|
|
|
## *Write tests for your hooks!*
|
2021-10-25 17:07:16 +00:00
|
|
|
##
|
|
|
|
## When `tracePreserves` is defined (`-d:tracePreserves`) a diagnostic
|
|
|
|
## trace is printing during `fromPreserve`.
|
2021-08-25 11:49:35 +00:00
|
|
|
# TODO: {.raises: [].}
|
|
|
|
runnableExamples:
|
2021-10-25 17:07:16 +00:00
|
|
|
type Foo {.preservesRecord: "foo".} = object
|
2021-09-01 09:37:49 +00:00
|
|
|
x, y: int
|
2021-08-25 11:49:35 +00:00
|
|
|
var foo: Foo
|
2021-09-01 09:37:49 +00:00
|
|
|
assert(fromPreserve(foo, parsePreserves("""<foo 1 2>""")))
|
2021-08-25 11:49:35 +00:00
|
|
|
assert(foo.x == 1)
|
|
|
|
assert(foo.y == 2)
|
2022-04-22 05:17:10 +00:00
|
|
|
when T is E:
|
|
|
|
if not pr.embedded and pr.kind == pkEmbedded:
|
|
|
|
v = pr.embed
|
2022-12-08 01:40:56 +00:00
|
|
|
result = true
|
|
|
|
elif T is Preserve[E]:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2022-03-18 17:25:44 +00:00
|
|
|
elif T is Preserve[void]:
|
|
|
|
if pr.containsNativeEmbeds:
|
|
|
|
raise newException(ValueError,
|
|
|
|
"cannot convert Preserve value with embedded " & $E & " values")
|
|
|
|
v = cast[T](pr)
|
|
|
|
result = true
|
|
|
|
elif T is Preserve:
|
|
|
|
{.error: "cannot convert " & $T & " from " & $Preserve[E].}
|
2021-09-08 09:00:38 +00:00
|
|
|
elif compiles(fromPreserveHook(v, pr)):
|
|
|
|
result = fromPreserveHook(v, pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is enum:
|
|
|
|
if pr.isSymbol:
|
|
|
|
try:
|
2022-02-20 04:33:15 +00:00
|
|
|
v = parseEnum[T](string pr.symbol)
|
2021-10-17 09:20:17 +00:00
|
|
|
result = true
|
|
|
|
except ValueError: discard
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is bool:
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.kind == pkBoolean:
|
|
|
|
v = pr.bool
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is SomeInteger:
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.kind == pkSignedInteger:
|
|
|
|
v = T(pr.int)
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is seq[byte]:
|
|
|
|
if pr.kind == pkByteString:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.bytes
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is seq:
|
|
|
|
if pr.kind == pkSequence:
|
2021-09-08 09:00:38 +00:00
|
|
|
v.setLen(pr.len)
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-09-08 09:00:38 +00:00
|
|
|
for i, e in pr.sequence:
|
2021-08-25 11:49:35 +00:00
|
|
|
result = result and fromPreserve(v[i], e)
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result:
|
|
|
|
v.setLen 0
|
|
|
|
break
|
2022-03-08 21:25:03 +00:00
|
|
|
elif T is float32:
|
|
|
|
if pr.kind == pkFloat:
|
|
|
|
v = pr.float
|
|
|
|
result = true
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is float64:
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
2021-08-25 11:49:35 +00:00
|
|
|
of pkFloat:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.float
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
|
|
|
of pkDouble:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.double
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2022-03-08 21:25:03 +00:00
|
|
|
else: discard
|
2021-06-28 10:30:16 +00:00
|
|
|
elif T is Ordinal | SomeInteger:
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.kind == pkSignedInteger:
|
|
|
|
v = (T)pr.int
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is string:
|
2021-10-17 09:20:17 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkString:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.string
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
of pkSymbol:
|
2022-02-20 04:33:15 +00:00
|
|
|
# loose convertability of Preserves symbol to Nim string
|
|
|
|
v = string pr.symbol
|
2021-10-17 09:20:17 +00:00
|
|
|
result = true
|
|
|
|
else: discard
|
2022-02-20 04:33:15 +00:00
|
|
|
elif T is Symbol:
|
|
|
|
if pr.kind == pkSymbol:
|
|
|
|
v = pr.symbol
|
|
|
|
result = true
|
|
|
|
elif T is distinct:
|
|
|
|
result = fromPreserve(v.distinctBase, pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is tuple:
|
|
|
|
case pr.kind
|
|
|
|
of pkRecord, pkSequence:
|
|
|
|
if pr.len <= tupleLen(T):
|
|
|
|
result = true
|
2022-06-13 04:05:35 +00:00
|
|
|
var i {.used.}: int
|
2021-10-17 09:20:17 +00:00
|
|
|
for f in fields(v):
|
|
|
|
if result and i < pr.len:
|
|
|
|
result = result and fromPreserve(f, pr[i])
|
|
|
|
inc i
|
|
|
|
of pkDictionary:
|
|
|
|
if tupleLen(T) == pr.len:
|
|
|
|
result = true
|
|
|
|
for key, val in fieldPairs(v):
|
2022-10-25 22:42:04 +00:00
|
|
|
let pv = step(pr, toSymbol(key, E))
|
|
|
|
result =
|
|
|
|
if pv.isSome: fromPreserve(val, get pv)
|
|
|
|
else: false
|
2022-03-08 21:25:03 +00:00
|
|
|
if not result: break
|
2021-10-17 09:20:17 +00:00
|
|
|
else: discard
|
|
|
|
elif T is ref:
|
|
|
|
if isNil(v): new(v)
|
|
|
|
result = fromPreserve(v[], pr)
|
|
|
|
elif T is object:
|
|
|
|
template fieldFromPreserve(key: string; val: typed; pr: Preserve[E]): bool {.used.} =
|
2022-02-20 04:33:15 +00:00
|
|
|
when v.dot(key).hasCustomPragma(preservesLiteral):
|
2021-10-25 19:30:55 +00:00
|
|
|
const lit = parsePreserves(v.dot(key).getCustomPragmaVal(preservesLiteral))
|
2021-10-17 09:20:17 +00:00
|
|
|
pr == lit
|
|
|
|
else:
|
|
|
|
fromPreserve(val, pr)
|
2021-10-25 19:33:31 +00:00
|
|
|
when T.hasCustomPragma(unpreservable):
|
|
|
|
raiseAssert($T & " is unpreservable")
|
2021-10-24 23:52:09 +00:00
|
|
|
elif T.hasCustomPragma(preservesRecord):
|
2021-10-17 09:20:17 +00:00
|
|
|
if pr.isRecord and pr.label.isSymbol(T.getCustomPragmaVal(preservesRecord)):
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for key, val in fieldPairs(v):
|
|
|
|
if result and i <= pr.len:
|
|
|
|
result = result and fieldFromPreserve(key, val, pr.record[i])
|
|
|
|
inc i
|
|
|
|
result = result and (i == pr.len)
|
|
|
|
elif T.hasCustomPragma(preservesTuple):
|
|
|
|
if pr.isSequence:
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for name, field in fieldPairs(v):
|
|
|
|
when v.dot(name).hasCustomPragma(preservesTupleTail):
|
2021-12-23 23:38:02 +00:00
|
|
|
if pr.len >= i:
|
|
|
|
setLen(v.dot(name), pr.len - i)
|
|
|
|
var j: int
|
|
|
|
while result and i < pr.len:
|
|
|
|
result = result and fieldFromPreserve(name, v.dot(name)[j], pr.sequence[i])
|
|
|
|
inc i
|
|
|
|
inc j
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
|
|
|
if result and i < pr.len:
|
2021-10-24 23:52:09 +00:00
|
|
|
result = result and fieldFromPreserve(name, field, pr.sequence[i])
|
2021-10-17 09:20:17 +00:00
|
|
|
inc i
|
|
|
|
result = result and (i == pr.len)
|
|
|
|
elif T.hasCustomPragma(preservesDictionary):
|
|
|
|
if pr.isDictionary:
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for key, _ in fieldPairs(v):
|
|
|
|
let val = pr.getOrDefault(toSymbol(key, E))
|
|
|
|
result = result and fieldFromPreserve(
|
|
|
|
key, v.dot(key), val)
|
|
|
|
if not result: break
|
|
|
|
inc i
|
|
|
|
result = result and (i == pr.len)
|
|
|
|
elif T.hasCustomPragma(preservesOr):
|
|
|
|
for kind in typeof(T.orKind):
|
|
|
|
v = T(orKind: kind)
|
|
|
|
var fieldWasFound = false
|
|
|
|
for key, val in fieldPairs(v):
|
|
|
|
when key != "orKind": # fieldPairs unwraps early
|
|
|
|
result = fieldFromPreserve(key, v.dot(key), pr)
|
|
|
|
fieldWasFound = true
|
|
|
|
break
|
|
|
|
if not fieldWasFound:
|
|
|
|
# hopefully a `discard` of-branch, so discard `pr`
|
|
|
|
result = true
|
|
|
|
if result: break
|
2022-03-08 21:25:03 +00:00
|
|
|
else:
|
|
|
|
if pr.isDictionary:
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for key, _ in fieldPairs(v):
|
|
|
|
let val = pr.getOrDefault(toSymbol(key, E))
|
|
|
|
result = result and fieldFromPreserve(
|
|
|
|
key, v.dot(key), val)
|
|
|
|
if not result: break
|
|
|
|
inc i
|
|
|
|
result = result and (i == pr.len)
|
|
|
|
else:
|
|
|
|
result = fromPreserveHook(v, pr)
|
|
|
|
# a previous branch determined that the hook does not compile but
|
|
|
|
# calling it here explicitly produces a reasonable compiler error
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result:
|
|
|
|
trace T, " !- ", pr
|
2021-06-24 15:31:30 +00:00
|
|
|
else:
|
2021-10-17 09:20:17 +00:00
|
|
|
trace T, " <- ", pr
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2021-09-08 09:00:38 +00:00
|
|
|
proc preserveTo*(pr: Preserve; T: typedesc): Option[T] =
|
2021-06-24 15:31:30 +00:00
|
|
|
## Reverse of `toPreserve`.
|
2021-08-25 11:49:35 +00:00
|
|
|
# TODO: {.raises: [].}
|
|
|
|
runnableExamples:
|
2022-03-18 16:26:52 +00:00
|
|
|
import std/options
|
2021-10-25 17:07:16 +00:00
|
|
|
type Foo {.preservesRecord: "foo".} = object
|
2021-09-01 09:37:49 +00:00
|
|
|
x, y: int
|
2021-08-25 11:49:35 +00:00
|
|
|
|
|
|
|
assert(parsePreserves("""<foo "abc">""").preserveTo(Foo).isNone)
|
2021-09-01 09:37:49 +00:00
|
|
|
assert(parsePreserves("""<bar 1 2>""").preserveTo(Foo).isNone)
|
|
|
|
assert(parsePreserves("""<foo 1 2>""").preserveTo(Foo).isSome)
|
2021-08-25 11:49:35 +00:00
|
|
|
var v: T
|
2021-09-08 09:00:38 +00:00
|
|
|
if fromPreserve(v, pr):
|
2021-08-25 11:49:35 +00:00
|
|
|
result = some(move v)
|
|
|
|
|
2022-05-18 18:56:41 +00:00
|
|
|
proc fromPreserveHook*[T, E](v: var set[T]; pr: Preserve[E]): bool =
|
|
|
|
## Hook for unpreserving a `set`.
|
|
|
|
if pr.kind == pkSet:
|
|
|
|
reset v
|
|
|
|
result = true
|
|
|
|
var vv: T
|
|
|
|
for e in pr.set:
|
|
|
|
result = fromPreserve(vv, e)
|
|
|
|
if result:
|
|
|
|
v.incl vv
|
|
|
|
else:
|
|
|
|
reset v
|
|
|
|
break
|
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
proc fromPreserveHook*[T, E](set: var HashSet[T]; pr: Preserve[E]): bool =
|
|
|
|
## Hook for preserving ``HashSet``.
|
|
|
|
if pr.kind == pkSet:
|
|
|
|
result = true
|
|
|
|
set.init(pr.set.len)
|
|
|
|
var e: T
|
|
|
|
for pe in pr.set:
|
|
|
|
result = fromPreserve(e, pe)
|
|
|
|
if not result: break
|
|
|
|
set.incl(move e)
|
|
|
|
|
|
|
|
proc fromPreserveHook*[A,B,E](t: var (Table[A,B]|TableRef[A,B]); pr: Preserve[E]): bool =
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.isDictionary:
|
2022-10-14 04:05:37 +00:00
|
|
|
when t is TableRef[A,B]:
|
|
|
|
if t.isNil: new t
|
2021-09-23 13:20:06 +00:00
|
|
|
result = true
|
|
|
|
var a: A
|
|
|
|
var b: B
|
2021-10-17 09:20:17 +00:00
|
|
|
for (k, v) in pr.dict.items:
|
2021-09-23 13:20:06 +00:00
|
|
|
result = fromPreserve(a, k) and fromPreserve(b, v)
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result:
|
|
|
|
clear t
|
|
|
|
break
|
2021-09-23 13:20:06 +00:00
|
|
|
t[move a] = move b
|
2021-08-11 11:00:32 +00:00
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
when isMainModule:
|
|
|
|
var t: Table[int, string]
|
|
|
|
var pr = t.toPreserveHook(void)
|
|
|
|
assert fromPreserveHook(t, pr)
|
|
|
|
|
2022-06-25 20:55:14 +00:00
|
|
|
proc apply*[E](result: var Preserve[E]; op: proc(_: var Preserve[E])) =
|
|
|
|
proc recurse(result: var Preserve[E]) = apply(result, op)
|
|
|
|
op(result)
|
|
|
|
case result.kind
|
|
|
|
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol, pkEmbedded: discard
|
|
|
|
of pkRecord:
|
|
|
|
apply(result.record, recurse)
|
|
|
|
of pkSequence:
|
|
|
|
apply(result.sequence, recurse)
|
|
|
|
of pkSet:
|
|
|
|
apply(result.set, recurse)
|
|
|
|
of pkDictionary:
|
|
|
|
apply(result.dict) do (e: var DictEntry[E]):
|
|
|
|
recurse(e.key)
|
|
|
|
recurse(e.val)
|
|
|
|
|
2021-10-20 20:15:11 +00:00
|
|
|
proc mapEmbeds*(pr: sink Preserve[void]; E: typedesc): Preserve[E] =
|
|
|
|
## Convert `Preserve[void]` to `Preserve[E]` using `fromPreserve` for `E`.
|
|
|
|
when E is void: {.error: "E cannot be void".}
|
|
|
|
if pr.embedded:
|
|
|
|
pr.embedded = false
|
|
|
|
result = Preserve[E](kind: pkEmbedded)
|
|
|
|
if not fromPreserve(result.embed, pr):
|
|
|
|
raise newException(ValueError, "failed to convert " & $E & " from " & $pr)
|
|
|
|
else:
|
|
|
|
case pr.kind
|
2021-11-12 11:25:28 +00:00
|
|
|
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol:
|
2021-10-20 20:15:11 +00:00
|
|
|
result = cast[Preserve[E]](pr)
|
|
|
|
of pkRecord:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.record = map(pr.record) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
mapEmbeds(x, E)
|
|
|
|
of pkSequence:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.sequence = map(pr.sequence) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
mapEmbeds(x, E)
|
|
|
|
of pkSet:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.set = map(pr.set) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
mapEmbeds(x, E)
|
|
|
|
of pkDictionary:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.dict = map(pr.dict) do (e: DictEntry[void]) -> DictEntry[E]:
|
|
|
|
(mapEmbeds(e.key, E), mapEmbeds(e.val, E))
|
|
|
|
of pkEmbedded:
|
2022-03-18 17:25:44 +00:00
|
|
|
result = Preserve[E](kind: pkEmbedded)
|
|
|
|
if not fromPreserve(result.embed, pr):
|
|
|
|
raise newException(ValueError, "failed to convert embedded " & $E)
|
2021-10-20 20:15:11 +00:00
|
|
|
|
|
|
|
proc mapEmbeds*[A, B](pr: sink Preserve[A]; op: proc (v: A): B): Preserve[B] =
|
|
|
|
## Convert `Preserve[A]` to `Preserve[B]` using an `A → B` procedure.
|
2022-03-18 17:25:44 +00:00
|
|
|
runnableExamples:
|
|
|
|
import std/tables
|
|
|
|
type MacGuffin = ref object
|
|
|
|
stuff: void
|
|
|
|
var registry = {20: new MacGuffin}.toTable
|
|
|
|
let
|
|
|
|
a = [ 20.embed ].toPreserve(int)
|
|
|
|
b = mapEmbeds(a) do (i: int) -> MacGuffin:
|
|
|
|
registry[i]
|
|
|
|
assert typeof(b[0].unembed) is MacGuffin
|
|
|
|
when A is Preserve: {.error: "cannot mapEmbeds from Preserve[Preserve[…]]".}
|
|
|
|
when B is Preserve: {.error: "cannot mapEmbeds to Preserve[Preserve[…]]".}
|
|
|
|
if pr.embedded:
|
|
|
|
var
|
|
|
|
e: A
|
|
|
|
pr = pr # TODO: avoid copy
|
|
|
|
pr.embedded = false
|
|
|
|
if not fromPreserve(e, pr):
|
|
|
|
raise newException(ValueError, "failed to map across embedded types")
|
|
|
|
result = embed op(e)
|
|
|
|
else:
|
|
|
|
case pr.kind
|
|
|
|
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol:
|
|
|
|
result = cast[Preserve[B]](pr)
|
|
|
|
of pkRecord:
|
|
|
|
result = Preserve[B](kind: pr.kind)
|
|
|
|
result.record = map(pr.record) do (x: Preserve[A]) -> Preserve[B]:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkSequence:
|
|
|
|
result = Preserve[B](kind: pr.kind)
|
|
|
|
result.sequence = map(pr.sequence) do (x: Preserve[A]) -> Preserve[B]:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkSet:
|
|
|
|
result = Preserve[B](kind: pr.kind)
|
|
|
|
result.set = map(pr.set) do (x: Preserve[A]) -> Preserve[B]:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkDictionary:
|
|
|
|
result = Preserve[B](kind: pr.kind)
|
|
|
|
result.dict = map(pr.dict) do (e: DictEntry[A]) -> DictEntry[B]:
|
|
|
|
(mapEmbeds(e.key, op), mapEmbeds(e.val, op))
|
|
|
|
of pkEmbedded:
|
|
|
|
result = embed op(pr.embed)
|
2021-10-20 20:15:11 +00:00
|
|
|
|
2022-12-07 09:23:10 +00:00
|
|
|
proc contract*[E](pr: sink Preserve[E]; op: proc (v: E): Preserve[void]): Preserve[void] =
|
|
|
|
## Convert `Preserve[E]` to `Preserve[void]` using an `E → Preserve[void]` procedure.
|
|
|
|
if not pr.embedded:
|
|
|
|
case pr.kind
|
|
|
|
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol:
|
|
|
|
result = cast[Preserve[void]](pr)
|
|
|
|
of pkRecord:
|
|
|
|
result = Preserve[void](kind: pr.kind)
|
|
|
|
result.record = map(pr.record) do (x: Preserve[E]) -> Preserve[void]:
|
|
|
|
contract(x, op)
|
|
|
|
of pkSequence:
|
|
|
|
result = Preserve[void](kind: pr.kind)
|
|
|
|
result.sequence = map(pr.sequence) do (x: Preserve[E]) -> Preserve[void]:
|
|
|
|
contract(x, op)
|
|
|
|
of pkSet:
|
|
|
|
result = Preserve[void](kind: pr.kind)
|
|
|
|
result.set = map(pr.set) do (x: Preserve[E]) -> Preserve[void]:
|
|
|
|
contract(x, op)
|
|
|
|
of pkDictionary:
|
|
|
|
result = Preserve[void](kind: pr.kind)
|
|
|
|
result.dict = map(pr.dict) do (e: DictEntry[E]) -> DictEntry[void]:
|
|
|
|
(contract(e.key, op), contract(e.val, op))
|
|
|
|
of pkEmbedded:
|
|
|
|
result = embed op(pr.embed)
|
|
|
|
|
|
|
|
proc expand*[E](pr: sink Preserve[void]; op: proc (v: Preserve[void]): Preserve[E]): Preserve[E] =
|
|
|
|
## Convert `Preserve[void]` to `Preserve[E]` using an `Preserve[void] → Preserve[E]` procedure.
|
2022-12-14 04:05:05 +00:00
|
|
|
if pr.embedded:
|
|
|
|
result = op(pr)
|
|
|
|
else:
|
2022-12-07 09:23:10 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol:
|
|
|
|
result = cast[Preserve[E]](pr)
|
|
|
|
of pkRecord:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.record = map(pr.record) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
expand(x, op)
|
|
|
|
of pkSequence:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.sequence = map(pr.sequence) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
expand(x, op)
|
|
|
|
of pkSet:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.set = map(pr.set) do (x: Preserve[void]) -> Preserve[E]:
|
|
|
|
expand(x, op)
|
|
|
|
of pkDictionary:
|
|
|
|
result = Preserve[E](kind: pr.kind)
|
|
|
|
result.dict = map(pr.dict) do (e: DictEntry[void]) -> DictEntry[E]:
|
|
|
|
(expand(e.key, op), expand(e.val, op))
|
|
|
|
of pkEmbedded:
|
|
|
|
result = op(pr.embed)
|
|
|
|
|
2022-10-31 02:09:17 +00:00
|
|
|
proc getOrDefault*[T, V](pr: Preserve[T]; key: string; default: V): V =
|
|
|
|
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
|
|
|
|
## or returns the `default` value.
|
|
|
|
var sym = toSymbol(key, T)
|
|
|
|
if pr.kind == pkDictionary:
|
|
|
|
for (k, v) in pr.dict:
|
|
|
|
if sym == k:
|
|
|
|
if fromPreserve(result, v): return
|
|
|
|
else: break
|
|
|
|
default
|
|
|
|
|
2022-10-29 03:12:12 +00:00
|
|
|
proc writeText*[E](stream: Stream; pr: Preserve[E]) =
|
|
|
|
## Encode Preserves to a `Stream` as text.
|
|
|
|
if pr.embedded: write(stream, "#!")
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind:
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkBoolean:
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.bool
|
2022-10-29 03:12:12 +00:00
|
|
|
of false: write(stream, "#f")
|
|
|
|
of true: write(stream, "#t")
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkFloat:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, $pr.float)
|
|
|
|
write(stream, 'f')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkDouble:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, $pr.double)
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkSignedInteger:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, $pr.int)
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkString:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, escapeJson(pr.string))
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkByteString:
|
2021-12-05 16:34:12 +00:00
|
|
|
if pr.bytes.allIt(char(it) in {' '..'!', '#'..'~'}):
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, "#\"")
|
|
|
|
write(stream, cast[string](pr.bytes))
|
|
|
|
write(stream, '"')
|
2021-12-05 16:34:12 +00:00
|
|
|
else:
|
2021-11-03 18:17:33 +00:00
|
|
|
if pr.bytes.len > 64:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, "#[") #]#
|
|
|
|
write(stream, base64.encode(pr.bytes))
|
|
|
|
write(stream, ']')
|
2021-11-03 18:17:33 +00:00
|
|
|
else:
|
|
|
|
const alphabet = "0123456789abcdef"
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, "#x\"")
|
2021-11-03 18:17:33 +00:00
|
|
|
for b in pr.bytes:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, alphabet[int(b shr 4)])
|
|
|
|
write(stream, alphabet[int(b and 0xf)])
|
|
|
|
write(stream, '"')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkSymbol:
|
2022-11-01 20:18:25 +00:00
|
|
|
let sym = pr.symbol.string
|
2022-12-08 04:05:24 +00:00
|
|
|
if sym.len > 0 and sym[0] in {'A'..'z'} and not sym.anyIt(char(it) in { '\x00'..'\x19', '"', '\\', '|' }):
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, sym)
|
|
|
|
else:
|
|
|
|
write(stream, '|')
|
|
|
|
for c in sym:
|
|
|
|
case c
|
|
|
|
of '\\':
|
|
|
|
write(stream, "\\\\")
|
|
|
|
of '/':
|
|
|
|
write(stream, "\\/")
|
2022-12-08 04:05:24 +00:00
|
|
|
of '\x08':
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, "\\b")
|
2022-12-08 04:05:24 +00:00
|
|
|
of '\x0c':
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, "\\f")
|
2022-12-08 04:05:24 +00:00
|
|
|
of '\x0a':
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, "\\n")
|
2022-12-08 04:05:24 +00:00
|
|
|
of '\x0d':
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, "\\r")
|
2022-12-08 04:05:24 +00:00
|
|
|
of '\x09':
|
2022-11-01 20:18:25 +00:00
|
|
|
write(stream, "\\t")
|
|
|
|
of '|':
|
|
|
|
write(stream, "\\|")
|
|
|
|
else:
|
|
|
|
write(stream, c)
|
|
|
|
write(stream, '|')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkRecord:
|
2021-09-08 09:00:38 +00:00
|
|
|
assert(pr.record.len > 0)
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, '<')
|
|
|
|
writeText(stream, pr.record[pr.record.high])
|
2021-09-08 09:00:38 +00:00
|
|
|
for i in 0..<pr.record.high:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, ' ')
|
|
|
|
writeText(stream, pr.record[i])
|
|
|
|
write(stream, '>')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkSequence:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, '[')
|
|
|
|
var insertWhitespace: bool
|
|
|
|
for val in pr.sequence:
|
|
|
|
if insertWhitespace: write(stream, ' ')
|
|
|
|
else: insertWhitespace = true
|
|
|
|
writeText(stream, val)
|
|
|
|
write(stream, ']')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkSet:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, "#{")
|
|
|
|
var insertWhitespace: bool
|
2021-09-08 09:00:38 +00:00
|
|
|
for val in pr.set.items:
|
2022-10-29 03:12:12 +00:00
|
|
|
if insertWhitespace: write(stream, ' ')
|
|
|
|
else: insertWhitespace = true
|
|
|
|
writeText(stream, val)
|
|
|
|
write(stream, '}')
|
2021-08-28 10:48:50 +00:00
|
|
|
of pkDictionary:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, '{')
|
|
|
|
var insertWhitespace: bool
|
2021-09-08 09:00:38 +00:00
|
|
|
for (key, value) in pr.dict.items:
|
2022-10-29 03:12:12 +00:00
|
|
|
if insertWhitespace: write(stream, ' ')
|
|
|
|
else: insertWhitespace = true
|
|
|
|
writeText(stream, key)
|
|
|
|
write(stream, ": ")
|
|
|
|
writeText(stream, value)
|
|
|
|
write(stream, '}')
|
2021-09-23 13:20:06 +00:00
|
|
|
of pkEmbedded:
|
2022-10-29 03:12:12 +00:00
|
|
|
write(stream, "#!")
|
2023-03-29 21:47:35 +00:00
|
|
|
when compiles($pr.embed) and not E is void:
|
2023-03-01 19:16:02 +00:00
|
|
|
write(stream, $pr.embed)
|
|
|
|
else:
|
|
|
|
write(stream, "…")
|
2021-08-11 11:00:32 +00:00
|
|
|
|
2022-10-29 03:12:12 +00:00
|
|
|
proc `$`*[E](pr: Preserve[E]): string =
|
2021-09-08 09:00:38 +00:00
|
|
|
## Generate the textual representation of ``pr``.
|
2022-10-29 03:12:12 +00:00
|
|
|
var stream = newStringStream()
|
|
|
|
writeText(stream, pr)
|
|
|
|
result = move stream.data
|
2022-03-18 16:26:52 +00:00
|
|
|
|
|
|
|
include ./preserves/private/parse
|