2016-09-20 19:53:38 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2016-09-21 19:20:57 +00:00
|
|
|
# (c) Copyright 2016 Felix Krause
|
2016-09-20 19:53:38 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## ==================
|
2021-05-17 22:31:47 +00:00
|
|
|
## Module yaml/taglib
|
2016-09-21 19:20:57 +00:00
|
|
|
## ==================
|
|
|
|
##
|
2021-03-23 17:51:05 +00:00
|
|
|
## The taglib API enables you to define custom tags for the types you are
|
|
|
|
## using with the serialization API.
|
2016-09-21 19:20:57 +00:00
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
import macros
|
2020-11-03 20:17:31 +00:00
|
|
|
import data
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
template n(suffix: string): Tag = Tag(nimyamlTagRepositoryPrefix & suffix)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
registeredUris {.compileTime.} = newSeq[string]() ## \
|
|
|
|
## Since Table doesn't really work at compile time, we also store
|
|
|
|
## registered URIs here to be able to generate a static compiler error
|
|
|
|
## when the user tries to register an URI more than once.
|
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
template setTag*(t: typedesc, tag: Tag) =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Associate the given uri with a certain type. This uri is used as YAML tag
|
|
|
|
## when loading and dumping values of this type.
|
2021-03-23 17:51:05 +00:00
|
|
|
when $tag in registeredUris:
|
2016-09-20 19:53:38 +00:00
|
|
|
{. fatal: "[NimYAML] URI \"" & uri & "\" registered twice!" .}
|
2021-03-23 17:51:05 +00:00
|
|
|
const tconst {.genSym.} = tag
|
2016-09-20 19:53:38 +00:00
|
|
|
static:
|
2021-03-23 17:51:05 +00:00
|
|
|
registeredUris.add($tag)
|
|
|
|
proc yamlTag*(T: typedesc[t]): Tag {.inline, raises: [].} = tconst
|
2016-09-20 19:53:38 +00:00
|
|
|
## autogenerated
|
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
template setTag*(t: typedesc, tag: Tag, idName: untyped) =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Like `setTagUri <#setTagUri.t,typedesc,string>`_, but lets
|
|
|
|
## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only
|
|
|
|
## necessary if you want to implement serialization / construction yourself.
|
2021-03-23 17:51:05 +00:00
|
|
|
when $tag in registeredUris:
|
2016-09-20 19:53:38 +00:00
|
|
|
{. fatal: "[NimYAML] URI \"" & uri & "\" registered twice!" .}
|
2021-03-23 17:51:05 +00:00
|
|
|
const idName* = tag
|
2016-09-20 19:53:38 +00:00
|
|
|
static:
|
2021-03-23 17:51:05 +00:00
|
|
|
registeredUris.add($tag)
|
2020-11-10 12:55:22 +00:00
|
|
|
proc yamlTag*(T: typedesc[t]): Tag {.inline, raises: [].} = idName
|
2016-09-20 19:53:38 +00:00
|
|
|
## autogenerated
|
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
template setTagUri*(t: typedesc; uri: string) {.deprecated: "use setTag".} =
|
|
|
|
setTag(t, Tag(uri))
|
|
|
|
template setTagUri*(t: typedesc; uri: string; idName: untyped)
|
|
|
|
{.deprecated: "use setTag".} = setTag(t, Tag(uri), idName)
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
static:
|
|
|
|
# standard YAML tags used by serialization
|
2021-03-23 17:51:05 +00:00
|
|
|
registeredUris.add($yTagExclamationMark)
|
|
|
|
registeredUris.add($yTagQuestionMark)
|
|
|
|
registeredUris.add($yTagString)
|
|
|
|
registeredUris.add($yTagNull)
|
|
|
|
registeredUris.add($yTagBoolean)
|
|
|
|
registeredUris.add($yTagFloat)
|
|
|
|
registeredUris.add($yTagTimestamp)
|
|
|
|
registeredUris.add($yTagValue)
|
|
|
|
registeredUris.add($yTagBinary)
|
2016-09-20 19:53:38 +00:00
|
|
|
# special tags used by serialization
|
2021-03-23 17:51:05 +00:00
|
|
|
registeredUris.add($yTagNimField)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
# tags for Nim's standard types
|
2021-03-23 17:51:05 +00:00
|
|
|
setTag(char, n"system:char", yTagNimChar)
|
|
|
|
setTag(int8, n"system:int8", yTagNimInt8)
|
|
|
|
setTag(int16, n"system:int16", yTagNimInt16)
|
|
|
|
setTag(int32, n"system:int32", yTagNimInt32)
|
|
|
|
setTag(int64, n"system:int64", yTagNimInt64)
|
|
|
|
setTag(uint8, n"system:uint8", yTagNimUInt8)
|
|
|
|
setTag(uint16, n"system:uint16", yTagNimUInt16)
|
|
|
|
setTag(uint32, n"system:uint32", yTagNimUInt32)
|
|
|
|
setTag(uint64, n"system:uint64", yTagNimUInt64)
|
|
|
|
setTag(float32, n"system:float32", yTagNimFloat32)
|
|
|
|
setTag(float64, n"system:float64", yTagNimFloat64)
|
|
|
|
|
|
|
|
proc nimTag*(suffix: string): Tag =
|
2016-10-10 18:16:54 +00:00
|
|
|
## prepends NimYAML's tag repository prefix to the given suffix. For example,
|
|
|
|
## ``nimTag("system:char")`` yields ``"tag:nimyaml.org,2016:system:char"``.
|
2021-03-23 17:51:05 +00:00
|
|
|
Tag(nimyamlTagRepositoryPrefix & suffix)
|