Skip to content

Commit

Permalink
Add support for Format flavors
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Mar 18, 2021
1 parent 261de74 commit 84ffa54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ template serializationFormatImpl(Name: untyped,
# mechanism of Nim will try to expand the `mimeType` param in the position
# of the `mimeType` template name which will result in error.
type Name* = object
template ReaderType*(T: type Name): type = Reader
template WriterType*(T: type Name): type = Writer
template ReaderType*(T: type Name, F: distinct type = DefaultFlavor): type = Reader[F]
template WriterType*(T: type Name, F: distinct type = DefaultFlavor): type = Writer[F]
template PreferedOutputType*(T: type Name): type = PreferedOutput
template mimeType*(T: type Name): string = mimeTypeName

Expand All @@ -24,6 +24,13 @@ template serializationFormat*(Name: untyped,
mimeType: static string = "") =
serializationFormatImpl(Name, Reader, Writer, PreferedOutput, mimeType)

template createFlavor*(ModifiedFormat, FlavorName: untyped) =
type FlavorName* = object
template ReaderType*(T: type FlavorName): type = ReaderType(ModifiedFormat, FlavorName)
template WriterType*(T: type FlavorName): type = WriterType(ModifiedFormat, FlavorName)
template PreferedOutputType*(T: type FlavorName): type = PreferedOutputType(ModifiedFormat)
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat)

template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
mixin init, WriterType, writeValue, PreferedOutputType
{.noSideEffect.}:
Expand All @@ -33,7 +40,8 @@ template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
# faststreams may be writing to a file or a network device.
try:
var s = memoryOutput()
var writer = unpackArgs(init, [WriterType(Format), s, params])
type Writer = WriterType(Format)
var writer = unpackArgs(init, [Writer, s, params])
writeValue writer, value
s.getOutput PreferedOutputType(Format)
except IOError:
Expand Down Expand Up @@ -108,7 +116,8 @@ template saveFile*(Format: type, filename: string, value: auto, params: varargs[

var stream = fileOutput(filename)
try:
var writer = unpackArgs(init, [WriterType(Format), stream, params])
type Writer = WriterType(Format)
var writer = unpackArgs(init, [Writer, stream, params])
writer.writeValue(value)
finally:
close stream
Expand Down Expand Up @@ -169,6 +178,7 @@ template writeValue*(stream: OutputStream,
value: auto,
params: varargs[untyped]) =
mixin WriterType, init, writeValue
var writer = unpackArgs(init, [WriterType(Format), stream])
type Writer = WriterType(Format)
var writer = unpackArgs(init, [Writer, stream])
writeValue writer, value

1 change: 1 addition & 0 deletions serialization/object_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import
errors

type
DefaultFlavor* = object
FieldTag*[RecordType; fieldName: static string; FieldType] = distinct void

let
Expand Down

0 comments on commit 84ffa54

Please sign in to comment.