Use the latest OutputStream; Needed helpers for the SSZ support

This commit is contained in:
Zahary Karadjov 2019-03-11 11:34:04 +02:00
parent b4216704f3
commit 030c8e2cbe
4 changed files with 45 additions and 7 deletions

View File

@ -1,8 +1,8 @@
import
faststreams, serialization/object_serialization
faststreams, serialization/[object_serialization, errors]
export
faststreams, object_serialization
faststreams, object_serialization, errors
template serializationFormatImpl(Name: untyped,
Reader, Writer, PreferedOutput: distinct type,
@ -28,27 +28,48 @@ proc encodeImpl(writer: var auto, value: auto) =
template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
mixin init, WriterType, PreferedOutputType
var s = init MemoryOutputStream[PreferedOutputType(Format)]
var s = init OutputStream
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var writer = init(WriterType(Format), addr s, params)
var writer = init(WriterType(Format), s, params)
else:
var writer = init(WriterType(Format), addr s)
var writer = init(WriterType(Format), s)
encodeImpl(writer, value)
s.getOutput
s.getOutput PreferedOutputType(Format)
proc readValue*(reader: var auto, T: type): T =
mixin readValue
reader.readValue(result)
template decode*(Format: distinct type,
input: openarray[byte] | string,
input: string,
RecordType: distinct type,
params: varargs[untyped]): auto =
# TODO, this is dusplicated only due to a Nim bug:
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
mixin init, ReaderType
var stream = memoryStream(input)
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var reader = init(ReaderType(Format), stream, params)
else:
var reader = init(ReaderType(Format), stream)
reader.readValue(RecordType)
template decode*(Format: distinct type,
input: openarray[byte],
RecordType: distinct type,
params: varargs[untyped]): auto =
# TODO, this is dusplicated only due to a Nim bug:
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
mixin init, ReaderType
var stream = memoryStream(input)

4
serialization/errors.nim Normal file
View File

@ -0,0 +1,4 @@
type
SerializationError* = object of CatchableError
UnexpectedEofError* = object of SerializationError

View File

@ -57,6 +57,13 @@ template serializeFields*(value: auto, fieldName, fieldValue, body: untyped) =
template op(fieldName, fieldValue: untyped) = body
eachSerializedFieldImpl(value, op)
template deserializeFields*(value: auto, fieldName, fieldValue, body: untyped) =
# TODO: this would be nicer as a for loop macro
mixin eachSerializedFieldImpl
template op(fieldName, fieldValue: untyped) = body
eachSerializedFieldImpl(value, op)
macro customSerialization*(field: untyped, definition): untyped =
discard

View File

@ -29,6 +29,12 @@ type
proc default(T: typedesc): T = discard
template roundripTest*(Format: type, val: auto) =
test Format.name & " " & val.type.name & " roundtrip":
let v = val
let serialized = Format.encode(v)
check: Format.decode(serialized, v.type) == v
proc executeReaderWriterTests*(Format: type) =
mixin init, ReaderType, WriterType