mirror of
https://github.com/status-im/nim-serialization.git
synced 2025-01-30 13:45:12 +00:00
f9a1121b87
Other changes: * The format declarations have been broken down in multiple parts. This makes it possible to structure the implementation libraries in a way such that the format name is accessible even when importing only reader or writer modules. * Flavors lead to more complicated Reader and Writer types. Instead of requiring the user to write down long type names such as `JsonReader[DefaultFlavor]`, it's now possible to refer to the reader and writer type of each format using expressions such as `Json.Reader` and `Json.Writer`. * Fixed a typo (PreferredOutput)
41 lines
1.9 KiB
Nim
41 lines
1.9 KiB
Nim
import
|
|
std/typetraits
|
|
|
|
template serializationFormatImpl(Name: untyped,
|
|
mimeTypeName: static string = "") {.dirty.} =
|
|
# This indirection is required in order to be able to generate the
|
|
# `mimeType` accessor template. Without the indirection, the template
|
|
# 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 mimeType*(T: type Name): string = mimeTypeName
|
|
|
|
template serializationFormat*(Name: untyped, mimeType: static string = "") =
|
|
serializationFormatImpl(Name, mimeType)
|
|
|
|
template setReader*(Format, FormatReader: distinct type) =
|
|
when arity(FormatReader) > 1:
|
|
template ReaderType*(T: type Format, F: distinct type = DefaultFlavor): type = FormatReader[F]
|
|
template Reader*(T: type Format, F: distinct type = DefaultFlavor): type = FormatReader[F]
|
|
else:
|
|
template ReaderType*(T: type Format): type = FormatReader
|
|
template Reader*(T: type Format): type = FormatReader
|
|
|
|
template setWriter*(Format, FormatWriter, PreferredOutput: distinct type) =
|
|
when arity(FormatWriter) > 1:
|
|
template WriterType*(T: type Format, F: distinct type = DefaultFlavor): type = FormatWriter[F]
|
|
template Writer*(T: type Format, F: distinct type = DefaultFlavor): type = FormatWriter[F]
|
|
else:
|
|
template WriterType*(T: type Format): type = FormatWriter
|
|
template Writer*(T: type Format): type = FormatWriter
|
|
|
|
template PreferredOutputType*(T: type Format): type = PreferredOutput
|
|
|
|
template createFlavor*(ModifiedFormat, FlavorName: untyped) =
|
|
type FlavorName* = object
|
|
template Reader*(T: type FlavorName): type = Reader(ModifiedFormat, FlavorName)
|
|
template Writer*(T: type FlavorName): type = Writer(ModifiedFormat, FlavorName)
|
|
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(ModifiedFormat)
|
|
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat)
|
|
|