preserves-nim/src/preserves.nim

778 lines
23 KiB
Nim
Raw Normal View History

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
import bigints
2021-08-28 10:48:50 +00:00
import std/[algorithm, base64, endians, hashes, options, sets, sequtils, streams, strutils, tables, typetraits]
from std/json import escapeJson, escapeJsonUnquoted
from std/macros import hasCustomPragma, getCustomPragmaVal
2021-06-02 13:51:36 +00:00
type
2021-06-08 10:14:56 +00:00
PreserveKind* = enum
2021-06-24 15:31:30 +00:00
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString,
pkByteString, pkSymbol, pkRecord, pkSequence, pkSet, pkDictionary, pkEmbedded
2021-06-02 13:51:36 +00:00
2021-08-28 10:48:50 +00:00
DictEntry[EmbededType] = tuple[key: PreserveGen[EmbededType], val: PreserveGen[EmbededType]]
PreserveGen*[EmbeddedType] {.acyclic.} = ref object
## Generic ``Preserve`` type before embedding.
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 pkBigInteger:
bigint*: BigInt
of pkString:
string*: string
of pkByteString:
bytes*: seq[byte]
of pkSymbol:
symbol*: string
of pkRecord:
2021-08-28 10:48:50 +00:00
record*: seq[PreserveGen[EmbeddedType]] # label is last
2021-06-02 13:51:36 +00:00
of pkSequence:
2021-08-28 10:48:50 +00:00
sequence*: seq[PreserveGen[EmbeddedType]]
2021-06-02 13:51:36 +00:00
of pkSet:
2021-08-28 10:48:50 +00:00
set*: seq[PreserveGen[EmbeddedType]]
# HashSet templates not hygenic enough for this type
2021-06-02 13:51:36 +00:00
of pkDictionary:
2021-08-28 10:48:50 +00:00
dict*: seq[DictEntry[EmbeddedType]]
# Tables templates not hygenic enough for this type
2021-06-02 13:51:36 +00:00
of pkEmbedded:
2021-08-28 10:48:50 +00:00
when EmbeddedType is void:
embedded*: PreserveGen[EmbeddedType]
else:
embedded*: EmbeddedType
2021-06-02 13:51:36 +00:00
2021-08-28 10:48:50 +00:00
template PreserveOf*(T: typedesc): untyped = PreserveGen[T]
## Customize ``PreserveGen`` with an embedded type.
## ```
## type MyPreserve = PreserveOf(MyEmbbededType)
## ```
2021-06-24 15:31:30 +00:00
2021-08-28 10:48:50 +00:00
type
Preserve* = PreserveOf(void)
## Type of Preserves with all embedded values
## converted to an unembedded representation.
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-08-28 10:48:50 +00:00
proc `<`*[E](x, y: PreserveGen[E]): bool =
2021-06-02 13:51:36 +00:00
if x.kind != y.kind:
if x.kind == pkSignedInteger and y.kind == pkBigInteger:
result = x.int.initBigInt < y.bigint
elif x.kind == pkBigInteger and y.kind == pkSignedInteger:
result = x.bigint < y.int.initBigInt
else:
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 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
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
if x.record[i] != y.record[i]: return false
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
of pkEmbedded:
when not E is void:
result = x.embedded < y.embedded
proc `==`*[E](x, y: PreserveGen[E]): bool =
# TODO: is this necessary to define?
if x.isNil or y.isNil:
result = x.isNil and y.isNil
elif x.kind == y.kind:
2021-06-02 13:51:36 +00:00
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:
2021-07-16 17:11:19 +00:00
result = x.record == y.record
2021-06-02 13:51:36 +00:00
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
2021-06-02 13:51:36 +00:00
result = true
of pkSet:
2021-08-28 10:48:50 +00:00
result = x.set == y.set
2021-06-02 13:51:36 +00:00
of pkDictionary:
2021-08-28 10:48:50 +00:00
result = x.dict == y.dict
2021-06-02 13:51:36 +00:00
of pkEmbedded:
result = x.embedded == y.embedded
2021-06-02 13:51:36 +00:00
2021-08-28 10:48:50 +00:00
proc hash*[E](prs: PreserveGen[E]): Hash =
type Value = PreserveGen[E]
var h = hash(prs.kind.int)
case prs.kind
2021-06-02 13:51:36 +00:00
of pkBoolean:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.bool)
2021-06-02 13:51:36 +00:00
of pkFloat:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.float)
2021-06-02 13:51:36 +00:00
of pkDouble:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.double)
2021-06-02 13:51:36 +00:00
of pkSignedInteger:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.int)
2021-06-02 13:51:36 +00:00
of pkBigInteger:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.bigint.flags)
h = h !& hash(prs.bigint)
2021-06-02 13:51:36 +00:00
of pkString:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.string)
2021-06-02 13:51:36 +00:00
of pkByteString:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.bytes)
2021-06-02 13:51:36 +00:00
of pkSymbol:
2021-08-28 10:48:50 +00:00
h = h !& hash(prs.symbol)
2021-06-02 13:51:36 +00:00
of pkRecord:
2021-08-28 10:48:50 +00:00
for val in prs.record:
h = h !& hash(val)
2021-06-02 13:51:36 +00:00
of pkSequence:
2021-08-28 10:48:50 +00:00
for val in prs.sequence:
h = h !& hash(val)
2021-06-02 13:51:36 +00:00
of pkSet:
for val in prs.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-08-28 10:48:50 +00:00
for (key, val) in prs.dict.items:
h = h !& hash(key) !& hash(val)
2021-06-02 13:51:36 +00:00
of pkEmbedded:
2021-08-28 10:48:50 +00:00
when not E is void:
h = h !& hash(prs.embedded)
!$h
2021-08-28 10:48:50 +00:00
proc `[]`*(prs: Preserve; i: int): Preserve =
case prs.kind
of pkRecord: prs.record[i]
of pkSequence: prs.sequence[i]
else:
raise newException(ValueError, "`[]` is not valid for " & $prs.kind)
2021-07-16 17:11:19 +00:00
2021-08-28 10:48:50 +00:00
proc incl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) =
for i in 0..prs.set.high:
if key < prs.set[i]:
insert(prs.set, [key], i)
return
prs.set.add(key)
proc excl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) =
for i in 0..prs.set.high:
if prs.set[i] == key:
delete(prs.set, i, i)
break
proc `[]`*[E](prs: var PreserveGen[E]; key: PreserveGen[E]): PreserveGen[E] =
for (k, v) in prs.dict.items:
if k == key: return v
raise newException(KeyError, "value not in Preserves dictionary")
proc `[]=`*[E](prs: var PreserveGen[E]; key, val: PreserveGen[E]) =
for i in 0..prs.dict.high:
if key < prs.dict[i].key:
insert(prs.dict, [(key, val,)], i)
return
elif key == prs.dict[i].key:
prs.dict[i].val = val
return
prs.dict.add((key, val,))
proc initRecord*(label: Preserve; args: varargs[Preserve, toPreserve]): Preserve =
## Record constructor.
result = Preserve(kind: pkRecord,
record: newSeqOfCap[Preserve](1+args.len))
for arg in args:
#assertValid(arg)
result.record.add(arg)
result.record.add(label)
proc len*[E](prs: PreserveGen[E]): int =
## Return the number of values one level below ``prs``.
case prs.kind
of pkRecord: prs.record.len.pred
of pkSequence: prs.sequence.len
of pkSet: prs.set.len
of pkDictionary: prs.dict.len
else: 0
proc symbol*(s: string; E = void): PreserveGen[E] {.inline.} =
## Symbol constructor.
PreserveGen[E](kind: pkSymbol, symbol: s)
proc initRecord*(label: string; args: varargs[Preserve, toPreserve]): Preserve {.inline.} =
## Record constructor that converts ``label`` to a symbol.
initRecord(symbol(label), args)
proc initSet*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkSet)
proc initDictionary*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkDictionary)
iterator items*[E](prs: PreserveGen[E]): PreserveGen[E] =
case prs.kind
of pkRecord:
for i in 0..prs.record.high.pred:
yield prs.record[i]
of pkSequence:
for e in prs.sequence.items: yield e
of pkSet:
for e in prs.set.items: yield e
of pkDictionary:
2021-08-28 10:48:50 +00:00
for (k, v) in prs.dict.items:
yield k # key can be an arbitrary Preserve
yield v
else: discard
2021-08-28 10:48:50 +00:00
proc isFalse*[E](prs: PreserveGen[E]): bool =
## Check if ``prs`` is equivalent to the zero-initialized ``Preserve``.
prs.kind == pkBoolean and prs.bool == false
proc isSymbol*(prs: Preserve; sym: string): bool =
(prs.kind == pkSymbol) and (prs.symbol == sym)
2021-06-24 15:31:30 +00:00
func isRecord*(prs: Preserve): bool =
if prs.kind == pkRecord:
result = true
assert(prs.record.len > 0)
func isDictionary*(prs: Preserve): bool =
prs.kind == pkDictionary
proc label*(prs: Preserve): Preserve {.inline.} =
## Return the label of a record value.
2021-06-24 15:31:30 +00:00
prs.record[prs.record.high]
proc arity*(prs: Preserve): int {.inline.} =
## Return the number of fields in a record value.
pred(prs.record.len)
proc fields*(prs: Preserve): seq[Preserve] {.inline.} =
## Return the fields of a record value.
2021-06-24 15:31:30 +00:00
prs.record[0..prs.record.high.pred]
iterator fields*(prs: Preserve): Preserve =
## Iterate the fields of a record value.
2021-06-24 15:31:30 +00:00
for i in 0..<prs.record.high: yield prs.record[i]
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-08-28 10:48:50 +00:00
proc write*[E](str: Stream; prs: PreserveGen[E]) =
2021-06-02 13:51:36 +00:00
case prs.kind:
of pkBoolean:
case prs.bool
of false: str.write(0x80'u8)
of true: str.write(0x81'u8)
of pkFloat:
str.write(0x82'u8)
when system.cpuEndian == bigEndian:
str.write(prs.float)
else:
var be: float32
swapEndian32(be.addr, prs.float.unsafeAddr)
str.write(be)
of pkDouble:
str.write(0x83'u8)
when system.cpuEndian == bigEndian:
str.write(prs.double)
else:
var be: float64
swapEndian64(be.addr, prs.double.unsafeAddr)
str.write(be)
of pkSignedInteger:
if (-3 <= prs.int) and (prs.int <= 12):
str.write(0x90'i8 or int8(if prs.int < 0: prs.int + 16 else: prs.int))
else:
var bitCount = 1'u8
if prs.int < 0:
while ((not prs.int) shr bitCount) != 0:
inc(bitCount)
else:
while (prs.int shr bitCount) != 0:
inc(bitCount)
var byteCount = (bitCount + 8) div 8
str.write(0xa0'u8 or (byteCount - 1))
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)
write(byteCount, prs.int)
of pkBigInteger:
doAssert(Negative notin prs.bigint.flags, "negative big integers not implemented")
2021-06-02 13:51:36 +00:00
var bytes = newSeqOfCap[uint8](prs.bigint.limbs.len * 4)
var begun = false
for i in countdown(prs.bigint.limbs.high, 0):
let limb = prs.bigint.limbs[i]
for j in countdown(24, 0, 8):
let b = uint8(limb shr j)
begun = begun or (b != 0)
if begun:
bytes.add(b)
if bytes.len <= 16:
str.write(0xa0'u8 or bytes.high.uint8)
else:
str.write(0xb0'u8)
str.writeVarint(bytes.len)
str.write(cast[string](bytes))
of pkString:
str.write(0xb1'u8)
str.writeVarint(prs.string.len)
str.write(prs.string)
of pkByteString:
str.write(0xb2'u8)
str.writeVarint(prs.bytes.len)
2021-07-16 17:11:19 +00:00
str.write(cast[string](prs.bytes))
2021-06-02 13:51:36 +00:00
of pkSymbol:
str.write(0xb3'u8)
str.writeVarint(prs.symbol.len)
str.write(prs.symbol)
of pkRecord:
2021-06-24 15:31:30 +00:00
assert(prs.record.len > 0)
2021-06-02 13:51:36 +00:00
str.write(0xb4'u8)
2021-06-24 15:31:30 +00:00
str.write(prs.record[prs.record.high])
for i in 0..<prs.record.high:
str.write(prs.record[i])
2021-06-02 13:51:36 +00:00
str.write(0x84'u8)
of pkSequence:
str.write(0xb5'u8)
for e in prs.sequence:
2021-06-02 13:51:36 +00:00
str.write(e)
str.write(0x84'u8)
of pkSet:
str.write(0xb6'u8)
for val in prs.set.items:
str.write(val)
2021-06-02 13:51:36 +00:00
str.write(0x84'u8)
of pkDictionary:
str.write(0xb7'u8)
2021-08-28 10:48:50 +00:00
for (key, value) in prs.dict.items:
2021-06-02 13:51:36 +00:00
str.write(key)
str.write(value)
str.write(0x84'u8)
of pkEmbedded:
str.write(0x86'u8)
2021-08-28 10:48:50 +00:00
when E is void:
str.write(0x80'u8)
else:
str.write(prs.embedded.toPreserve)
2021-06-02 13:51:36 +00:00
2021-08-28 10:48:50 +00:00
proc encode*[E](prs: PreserveGen[E]): string =
2021-07-16 17:11:19 +00:00
let s = newStringStream()
s.write prs
s.setPosition 0
result = s.readAll
2021-08-28 10:48:50 +00:00
proc decodePreserves*(s: Stream; E = void): PreserveGen[E] =
type Value = PreserveGen[E]
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-08-28 10:48:50 +00:00
of 0x80: result = Value(kind: pkBoolean, bool: false)
of 0x81: result = Value(kind: pkBoolean, bool: true)
2021-06-02 13:51:36 +00:00
of 0x82:
when system.cpuEndian == bigEndian:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkFloat, float: s.readFloat32())
2021-06-02 13:51:36 +00:00
else:
2021-08-28 10:48:50 +00:00
result = Value(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-08-28 10:48:50 +00:00
result = Value(kind: pkDouble, double: s.readFloat64())
2021-06-02 13:51:36 +00:00
else:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkDouble)
2021-06-02 13:51:36 +00:00
var be = s.readFloat64()
swapEndian64(result.double.addr, be.addr)
2021-08-28 10:48:50 +00:00
of 0x86:
result = Value(kind: pkEmbedded, embedded: decodePreserves(s, E))
2021-06-02 13:51:36 +00:00
of 0xb1:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkString)
2021-06-02 13:51:36 +00:00
let len = s.readVarint()
result.string = s.readStr(len)
of 0xb2:
2021-08-28 10:48:50 +00:00
result = Value(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()
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSymbol, symbol: s.readStr(len))
2021-06-02 13:51:36 +00:00
of 0xb4:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkRecord)
var label = decodePreserves(s, E)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-08-28 10:48:50 +00:00
result.record.add decodePreserves(s, E)
result.record.add(move label)
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb5:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSequence)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-08-28 10:48:50 +00:00
result.sequence.add decodePreserves(s, E)
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb6:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSet)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-08-28 10:48:50 +00:00
incl(result, decodePreserves(s, E))
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb7:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkDictionary)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-08-28 10:48:50 +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-08-28 10:48:50 +00:00
result = Value(kind: pkBigInteger, bigint: initBigint 0)
for _ in 1..len:
2021-06-02 13:51:36 +00:00
result.bigint = (result.bigint shl 8) + s.readUint8().int32
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-08-28 10:48:50 +00:00
result = Value(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
if len <= 8:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSignedInteger, int: s.readUint8().BiggestInt)
2021-06-02 13:51:36 +00:00
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
else:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkBigInteger)
2021-06-02 13:51:36 +00:00
for i in 0..<len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32
else:
assertStream(false)
2021-08-28 10:48:50 +00:00
proc decodePreserves*(s: string, E = void): PreserveGen[E] =
s.newStringStream.decodePreserves E
2021-07-16 17:11:19 +00:00
2021-08-28 10:48:50 +00:00
proc decodePreserves*(s: seq[byte], E = void): PreserveGen[E] =
cast[string](s).decodePreserves E
2021-07-15 12:24:30 +00:00
template record*(label: string) {.pragma.}
## Serialize this object or tuple as a record.
##
## ```
## type Foo {.record: "foobar".} = tuple
## a, b: int
## let r: Foo = (1, 2)
## assert($r.toPreserve == "<foobar 1 2>")
## ```
2021-07-01 10:47:30 +00:00
template unpreservable*() {.pragma.}
## Pragma to forbid a type from being converted by `toPreserve`.
2021-08-28 10:48:50 +00:00
proc toPreserve*[T](x: T; E = void): PreserveGen[E] =
## Serializes `x` to Preserves; uses `toPreserveHook(x: T)` if it's in scope to
## customize serialization.
2021-08-28 10:48:50 +00:00
type Value = PreserveGen[E]
when (T is Value): result = x
elif T is E: result = Value(kind: pkEmbedded, embedded: x)
elif compiles(toPreserveHook(x)):
result = toPreserveHook(x)
elif T is Bigint:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkBigInteger, bigint: x)
elif T is seq[byte]:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkByteString, bytes: x)
elif T is array | seq:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSequence)
for v in x.items: result.sequence.add(toPreserve(v, E))
elif T is bool:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkBoolean, bool: x)
elif T is distinct:
result = toPreserve(x.distinctBase)
elif T is float:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkFloat, float: x)
elif T is float64:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkDouble, double: x)
2021-06-24 15:31:30 +00:00
elif T is object | tuple:
when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".}
2021-07-01 10:47:30 +00:00
elif T.hasCustomPragma(record):
2021-08-28 10:48:50 +00:00
result = Value(kind: pkRecord)
for _, f in x.fieldPairs: result.record.add(toPreserve(f))
result.record.add(symbol(T.getCustomPragmaVal(record)))
else:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkDictionary)
for k, v in x.fieldPairs:
result[symbol(k, E)] = toPreserve(v, E)
elif T is Ordinal:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSignedInteger, int: x.ord.BiggestInt)
elif T is ptr | ref:
2021-08-28 10:48:50 +00:00
if system.`==`(x, nil): result = symbol("null", E)
else: result = toPreserve(x[])
elif T is string:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkString, string: x)
elif T is SomeInteger:
2021-08-28 10:48:50 +00:00
result = Value(kind: pkSignedInteger, int: x.BiggestInt)
else:
raiseAssert("unpreservable type" & $T)
2021-06-02 13:51:36 +00:00
2021-06-24 15:31:30 +00:00
proc toPreserveHook*[T](set: HashSet[T]): Preserve =
Preserve(kind: pkSet, set: set.map(toPreserve))
2021-08-11 11:00:32 +00:00
proc toPreserveHook*[A,B](table: Table[A,B]|TableRef[A,B]): Preserve =
result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len))
2021-08-28 10:48:50 +00:00
for k, v in table.pairs: result.dict.add((toPreserve(k), toPreserve(v),))
2021-08-11 11:00:32 +00:00
2021-08-28 10:48:50 +00:00
proc fromPreserve*[E,T](v: var T; prs: PreserveGen[E]): bool =
## Inplace version of `preserveTo`.
## Partial matches on compond values may leave artifacts in ``v``.
# TODO: {.raises: [].}
runnableExamples:
import std/options, preserves, preserves/parse
type Foo {.record: "foo".} = object
x, y, z: int
var foo: Foo
assert(fromPreserve(foo, parsePreserves("""<foo 1 2 3>""")))
assert(foo.x == 1)
assert(foo.y == 2)
assert(foo.z == 3)
2021-08-28 10:48:50 +00:00
type Value = PreserveGen[E]
when T is Value:
v = prs
result = true
2021-08-28 10:48:50 +00:00
elif compiles(fromPreserveHook(v, prs)):
result = fromPreserveHook(v, prs)
2021-06-24 15:31:30 +00:00
elif T is Bigint:
case prs.kind
of pkSignedInteger:
v = initBigint(prs.int)
result = true
of pkBigInteger:
v = prs.bigint
result = true
else: disard
2021-06-24 15:31:30 +00:00
elif T is bool:
if prs.kind == pkBoolean:
v = prs.bool
result = true
2021-06-24 15:31:30 +00:00
elif T is SomeInteger:
if prs.kind == pkSignedInteger:
v = T(prs.int)
result = true
2021-06-24 15:31:30 +00:00
elif T is float:
if prs.kind == pkFloat:
v = prs.float
result = true
2021-06-24 15:31:30 +00:00
elif T is seq:
if T is seq[byte] and prs.kind == pkByteString:
v = prs.bytes
result = true
elif prs.kind == pkSequence:
v.setLen(prs.len)
result = true
for i, e in prs.sequence:
result = result and fromPreserve(v[i], e)
2021-06-24 15:31:30 +00:00
elif T is float64:
case prs.kind
of pkFloat:
v = prs.float
result = true
of pkDouble:
v = prs.double
result = true
elif T is object | tuple:
case prs.kind
of pkRecord:
when T.hasCustomPragma(record):
2021-08-28 10:48:50 +00:00
if prs.record[prs.record.high].isSymbol T.getCustomPragmaVal(record):
result = true
var i = 0
for fname, field in v.fieldPairs:
if not result or (i == prs.record.high): break
result = result and fromPreserve(field, prs.record[i])
inc(i)
result = result and (i == prs.record.high) # arity equivalence check=
of pkDictionary:
result = true
2021-08-28 10:48:50 +00:00
var fieldCount = 0
for key, val in v.fieldPairs:
2021-08-28 10:48:50 +00:00
inc fieldCount
for (pk, pv) in prs.dict.items:
var sym = symbol(key, E)
if sym == pk:
result = result and fromPreserve(val, pv)
break
result = result and prs.dict.len == fieldCount
else: discard
elif T is Ordinal | SomeInteger:
if prs.kind == pkSignedInteger:
v = (T)prs.int
result = true
2021-08-11 11:00:32 +00:00
elif T is ref:
2021-08-28 10:48:50 +00:00
if prs != symbol("null", E):
new v
result = fromPreserve(v[], prs)
2021-06-24 15:31:30 +00:00
elif T is string:
if prs.kind == pkString:
v = prs.string
result = true
elif T is distinct:
result = fromPreserve(result.distinctBase, prs)
2021-06-24 15:31:30 +00:00
else:
raiseAssert("no conversion of type Preserve to " & $T)
2021-06-24 15:31:30 +00:00
2021-08-28 10:48:50 +00:00
proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] =
2021-06-24 15:31:30 +00:00
## Reverse of `toPreserve`.
# TODO: {.raises: [].}
runnableExamples:
import std/options, preserves, preserves/parse
type Foo {.record: "foo".} = object
x, y, z: int
assert(parsePreserves("""<foo "abc">""").preserveTo(Foo).isNone)
assert(parsePreserves("""<bar 1 2 3>""").preserveTo(Foo).isNone)
assert(parsePreserves("""<foo 1 2 3>""").preserveTo(Foo).isSome)
var v: T
if fromPreserve(v, prs):
result = some(move v)
proc fromPreserveHook*[A,B](t: var Table[A,B]|TableRef[A,B]; prs: Preserve): bool =
if prs.isDictionary:
for k, v in prs.pairs:
t[preserveTo(k,A)] = preserveTo(k,B)
result = true
2021-08-11 11:00:32 +00:00
2021-08-28 10:48:50 +00:00
proc concat[E](result: var string; prs: PreserveGen[E]) =
case prs.kind:
of pkBoolean:
case prs.bool
of false: result.add "#f"
of true: result.add "#t"
of pkFloat:
result.add($prs.float & "f")
of pkDouble:
result.add $prs.double
of pkSignedInteger:
result.add $prs.int
of pkBigInteger:
result.add $prs.bigint
of pkString:
result.add escapeJson(prs.string)
of pkByteString:
for b in prs.bytes:
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
result.add("#[") #]#
result.add(base64.encode(prs.bytes))
result.add(']')
return
result.add("#\"")
result.add(cast[string](prs.bytes))
result.add('"')
of pkSymbol:
result.add(escapeJsonUnquoted(prs.symbol))
of pkRecord:
assert(prs.record.len > 0)
result.add('<')
result.concat(prs.record[prs.record.high])
for i in 0..<prs.record.high:
result.add(' ')
result.concat(prs.record[i])
result.add('>')
of pkSequence:
result.add('[')
for i, val in prs.sequence:
if i > 0:
result.add(' ')
result.concat(val)
result.add(']')
of pkSet:
result.add("#{")
for val in prs.set.items:
result.concat(val)
result.add(' ')
if prs.set.len > 1:
result.setLen(result.high)
result.add('}')
of pkDictionary:
result.add('{')
var i = 0
for (key, value) in prs.dict.items:
if i > 0:
result.add(' ')
result.concat(key)
result.add(": ")
result.concat(value)
inc i
result.add('}')
of pkEmbedded:
result.add("#!")
when E is void:
result.add("#f")
else:
result.add($prs.embedded)
2021-08-11 11:00:32 +00:00
2021-08-28 10:48:50 +00:00
proc `$`*[E](prs: PreserveGen[E]): string = concat(result, prs)