# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway # SPDX-License-Identifier: ISC import bigints 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 type PreserveKind* = enum pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol, pkRecord, pkSequence, pkSet, pkDictionary, pkEmbedded DictEntry[EmbededType] = tuple[key: PreserveGen[EmbededType], val: PreserveGen[EmbededType]] PreserveGen*[EmbeddedType] {.acyclic.} = ref object ## Generic ``Preserve`` type before embedding. 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: record*: seq[PreserveGen[EmbeddedType]] # label is last of pkSequence: sequence*: seq[PreserveGen[EmbeddedType]] of pkSet: set*: seq[PreserveGen[EmbeddedType]] # HashSet templates not hygenic enough for this type of pkDictionary: dict*: seq[DictEntry[EmbeddedType]] # Tables templates not hygenic enough for this type of pkEmbedded: when EmbeddedType is void: embedded*: PreserveGen[EmbeddedType] else: embedded*: EmbeddedType template PreserveOf*(T: typedesc): untyped = PreserveGen[T] ## Customize ``PreserveGen`` with an embedded type. ## ``` ## type MyPreserve = PreserveOf(MyEmbbededType) ## ``` type Preserve* = PreserveOf(void) ## Type of Preserves with all embedded values ## converted to an unembedded representation. proc `<`(x, y: string | seq[byte]): bool = for i in 0 .. min(x.high, y.high): if x[i] < y[i]: return true if x[i] != y[i]: return false x.len < y.len proc `<`*[E](x, y: PreserveGen[E]): bool = 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 else: case x.kind of pkBoolean: result = (not x.bool) and 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: if x.record[x.record.high] < y.record[y.record.high]: return true for i in 0.. 0) func isDictionary*(prs: Preserve): bool = prs.kind == pkDictionary proc label*(prs: Preserve): Preserve {.inline.} = ## Return the label of a record value. 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. prs.record[0..prs.record.high.pred] iterator fields*(prs: Preserve): Preserve = ## Iterate the fields of a record value. for i in 0.. 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") 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) str.write(cast[string](prs.bytes)) of pkSymbol: str.write(0xb3'u8) str.writeVarint(prs.symbol.len) str.write(prs.symbol) of pkRecord: assert(prs.record.len > 0) str.write(0xb4'u8) str.write(prs.record[prs.record.high]) for i in 0.. 0x9c: 0xa0 else: 0x90)) of 0xa0: let len = (tag.int and 0x0f) + 1 if len <= 8: result = Value(kind: pkSignedInteger, int: s.readUint8().BiggestInt) if (result.int and 0x80) != 0: result.int.dec(0x100) for i in 1..") ## ``` template unpreservable*() {.pragma.} ## Pragma to forbid a type from being converted by `toPreserve`. 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. 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: result = Value(kind: pkBigInteger, bigint: x) elif T is seq[byte]: result = Value(kind: pkByteString, bytes: x) elif T is array | seq: result = Value(kind: pkSequence) for v in x.items: result.sequence.add(toPreserve(v, E)) elif T is bool: result = Value(kind: pkBoolean, bool: x) elif T is distinct: result = toPreserve(x.distinctBase) elif T is float: result = Value(kind: pkFloat, float: x) elif T is float64: result = Value(kind: pkDouble, double: x) elif T is object | tuple: when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".} elif T.hasCustomPragma(record): result = Value(kind: pkRecord) for _, f in x.fieldPairs: result.record.add(toPreserve(f)) result.record.add(symbol(T.getCustomPragmaVal(record))) else: result = Value(kind: pkDictionary) for k, v in x.fieldPairs: result[symbol(k, E)] = toPreserve(v, E) elif T is Ordinal: result = Value(kind: pkSignedInteger, int: x.ord.BiggestInt) elif T is ptr | ref: if system.`==`(x, nil): result = symbol("null", E) else: result = toPreserve(x[]) elif T is string: result = Value(kind: pkString, string: x) elif T is SomeInteger: result = Value(kind: pkSignedInteger, int: x.BiggestInt) else: raiseAssert("unpreservable type" & $T) proc toPreserveHook*[T](set: HashSet[T]): Preserve = Preserve(kind: pkSet, set: set.map(toPreserve)) proc toPreserveHook*[A,B](table: Table[A,B]|TableRef[A,B]): Preserve = result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len)) for k, v in table.pairs: result.dict.add((toPreserve(k), toPreserve(v),)) 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(""""""))) assert(foo.x == 1) assert(foo.y == 2) assert(foo.z == 3) type Value = PreserveGen[E] when T is Value: v = prs result = true elif compiles(fromPreserveHook(v, prs)): result = fromPreserveHook(v, prs) elif T is Bigint: case prs.kind of pkSignedInteger: v = initBigint(prs.int) result = true of pkBigInteger: v = prs.bigint result = true else: disard elif T is bool: if prs.kind == pkBoolean: v = prs.bool result = true elif T is SomeInteger: if prs.kind == pkSignedInteger: v = T(prs.int) result = true elif T is float: if prs.kind == pkFloat: v = prs.float result = true 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) 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): 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 var fieldCount = 0 for key, val in v.fieldPairs: 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 elif T is ref: if prs != symbol("null", E): new v result = fromPreserve(v[], prs) elif T is string: if prs.kind == pkString: v = prs.string result = true elif T is distinct: result = fromPreserve(result.distinctBase, prs) else: raiseAssert("no conversion of type Preserve to " & $T) proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] = ## Reverse of `toPreserve`. # TODO: {.raises: [].} runnableExamples: import std/options, preserves, preserves/parse type Foo {.record: "foo".} = object x, y, z: int assert(parsePreserves("""""").preserveTo(Foo).isNone) assert(parsePreserves("""""").preserveTo(Foo).isNone) assert(parsePreserves("""""").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 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..') 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) proc `$`*[E](prs: PreserveGen[E]): string = concat(result, prs)