Add support for Format flavors
This commit is contained in:
parent
261de741b7
commit
84ffa54554
|
@ -14,8 +14,8 @@ template serializationFormatImpl(Name: untyped,
|
||||||
# mechanism of Nim will try to expand the `mimeType` param in the position
|
# mechanism of Nim will try to expand the `mimeType` param in the position
|
||||||
# of the `mimeType` template name which will result in error.
|
# of the `mimeType` template name which will result in error.
|
||||||
type Name* = object
|
type Name* = object
|
||||||
template ReaderType*(T: type Name): type = Reader
|
template ReaderType*(T: type Name, F: distinct type = DefaultFlavor): type = Reader[F]
|
||||||
template WriterType*(T: type Name): type = Writer
|
template WriterType*(T: type Name, F: distinct type = DefaultFlavor): type = Writer[F]
|
||||||
template PreferedOutputType*(T: type Name): type = PreferedOutput
|
template PreferedOutputType*(T: type Name): type = PreferedOutput
|
||||||
template mimeType*(T: type Name): string = mimeTypeName
|
template mimeType*(T: type Name): string = mimeTypeName
|
||||||
|
|
||||||
|
@ -24,6 +24,13 @@ template serializationFormat*(Name: untyped,
|
||||||
mimeType: static string = "") =
|
mimeType: static string = "") =
|
||||||
serializationFormatImpl(Name, Reader, Writer, PreferedOutput, mimeType)
|
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 =
|
template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
|
||||||
mixin init, WriterType, writeValue, PreferedOutputType
|
mixin init, WriterType, writeValue, PreferedOutputType
|
||||||
{.noSideEffect.}:
|
{.noSideEffect.}:
|
||||||
|
@ -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.
|
# faststreams may be writing to a file or a network device.
|
||||||
try:
|
try:
|
||||||
var s = memoryOutput()
|
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
|
writeValue writer, value
|
||||||
s.getOutput PreferedOutputType(Format)
|
s.getOutput PreferedOutputType(Format)
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -108,7 +116,8 @@ template saveFile*(Format: type, filename: string, value: auto, params: varargs[
|
||||||
|
|
||||||
var stream = fileOutput(filename)
|
var stream = fileOutput(filename)
|
||||||
try:
|
try:
|
||||||
var writer = unpackArgs(init, [WriterType(Format), stream, params])
|
type Writer = WriterType(Format)
|
||||||
|
var writer = unpackArgs(init, [Writer, stream, params])
|
||||||
writer.writeValue(value)
|
writer.writeValue(value)
|
||||||
finally:
|
finally:
|
||||||
close stream
|
close stream
|
||||||
|
@ -169,6 +178,7 @@ template writeValue*(stream: OutputStream,
|
||||||
value: auto,
|
value: auto,
|
||||||
params: varargs[untyped]) =
|
params: varargs[untyped]) =
|
||||||
mixin WriterType, init, writeValue
|
mixin WriterType, init, writeValue
|
||||||
var writer = unpackArgs(init, [WriterType(Format), stream])
|
type Writer = WriterType(Format)
|
||||||
|
var writer = unpackArgs(init, [Writer, stream])
|
||||||
writeValue writer, value
|
writeValue writer, value
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import
|
||||||
errors
|
errors
|
||||||
|
|
||||||
type
|
type
|
||||||
|
DefaultFlavor* = object
|
||||||
FieldTag*[RecordType; fieldName: static string; FieldType] = distinct void
|
FieldTag*[RecordType; fieldName: static string; FieldType] = distinct void
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
Loading…
Reference in New Issue