-
Notifications
You must be signed in to change notification settings - Fork 7
/
toml_serialization.nim
179 lines (152 loc) · 6.65 KB
/
toml_serialization.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# toml-serialization
# Copyright (c) 2020 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
# * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
serialization, toml_serialization/[reader, writer, types],
toml_serialization/private/utils
export
serialization, reader, writer, types
serializationFormat Toml,
mimeType = "application/toml"
Toml.setReader TomlReader
Toml.setWriter TomlWriter, PreferredOutput = string
template supports*(_: type Toml, T: type): bool =
# The TOML format should support every type
# when not at top level ... :)
true
import
typetraits
type
# only objects | tuple | TomlValueRef allowed at top level
TomlSpecial* = TomlDateTime or TomlDate or TomlTime
TomlNotTopLevel* = SomeInteger or
seq or SomeFloat or string or
array or enum or bool or TomlSpecial
template tomlFatalImpl(fn, R: untyped) =
const typeName = typetraits.name(type R)
{.fatal: "Toml." & astToStr(fn) & ": \'" & typeName &
"\' not allowed at top level Toml, called from" &
$instantiationInfo().}
template decode*(_: type Toml,
input: string,
RecordType: type TomlNotTopLevel,
params: varargs[untyped]): auto =
# TODO, this is duplicated only due to a Nim bug:
# If `input` was `string|openArray[byte]`, it won't match `seq[byte]`
tomlFatalImpl(decode, RecordType)
template decode*(_: type Toml,
input: openArray[byte],
RecordType: type TomlNotTopLevel,
params: varargs[untyped]): auto =
# TODO, this is duplicated only due to a Nim bug:
# If `input` was `string|openArray[byte]`, it won't match `seq[byte]`
tomlFatalImpl(decode, RecordType)
template loadFile*(_: type Toml,
fileName: string,
RecordType: type TomlNotTopLevel,
params: varargs[untyped]): auto =
tomlFatalImpl(loadFile, RecordType)
template encode*(_: type Toml,
value: TomlNotTopLevel,
params: varargs[untyped]): auto =
type RecordType = type value
tomlFatalImpl(encode, RecordType)
template saveFile*(_: type Toml,
fileName: string,
value: TomlNotTopLevel,
params: varargs[untyped]) =
type RecordType = type value
tomlFatalImpl(saveFile, RecordType)
# override default behaviour when in keyed mode
import
stew/shims/macros
template tomlDecodeImpl*(input: untyped,
RecordType: distinct type,
key: string,
tomlCase: TomlCase,
params: varargs[untyped]): auto =
mixin init, ReaderType
{.noSideEffect.}:
# We assume that there are no side-effects here, because we are
# using a `memoryInput`. The computed side-effects are coming
# from the fact that the dynamic dispatch mechanisms used in
# faststreams may be reading from a file or a network device.
try:
let stream = memInputStream(input)
var reader = unpackArgs(init, [TomlReader, stream, tomlCase, params])
when RecordType is (seq or array) and uTypeIsRecord(RecordType):
reader.readTableArray(RecordType, key, tomlCase)
else:
reader.moveToKey(key, tomlCase)
reader.readValue(RecordType)
except IOError:
raise (ref Defect)() # memory inputs cannot raise an IOError
template decode*(_: type Toml, input: string,
RecordType: distinct type,
key: string, tomlCase: TomlCase,
params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, key, tomlCase, params)
template decode*(_: type Toml, input: string,
RecordType: distinct type,
key: string, params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, key, TomlCaseSensitive, params)
template decode*(_: type Toml, input: openArray[byte],
RecordType: distinct type,
key: string, tomlCase: TomlCase,
params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, key, tomlCase, params)
template decode*(_: type Toml, input: openArray[byte],
RecordType: distinct type,
key: string, params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, key, TomlCaseSensitive, params)
template decode*(_: type Toml,
input: string,
RecordType: distinct type,
params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, "", TomlCaseSensitive, params)
template decode*(_: type Toml,
input: openArray[byte],
RecordType: distinct type,
params: varargs[untyped]): auto =
tomlDecodeImpl(input, RecordType, "", TomlCaseSensitive, params)
template tomlLoadImpl*(filename: string,
RecordType: distinct type,
key: string, tomlCase: TomlCase,
params: varargs[untyped]): auto =
mixin init, ReaderType, readValue
var stream: InputStream
when nimvm:
let input = staticRead(filename)
stream = VMInputStream(pos: 0, data: toVMString(input))
else:
stream = memFileInput(filename)
try:
var reader = unpackArgs(init, [TomlReader, stream, params])
when RecordType is (seq or array) and uTypeIsRecord(RecordType):
reader.readTableArray(RecordType, key, tomlCase)
else:
reader.moveToKey(key, tomlCase)
reader.readValue(RecordType)
finally:
close stream
template loadFile*(_: type Toml, filename: string,
RecordType: distinct type,
key: string, tomlCase: TomlCase,
params: varargs[untyped]): auto =
tomlLoadImpl(filename, RecordType, key, tomlCase, params)
template loadFile*(_: type Toml, filename: string,
RecordType: distinct type,
key: string, params: varargs[untyped]): auto =
tomlLoadImpl(filename, RecordType, key, TomlCaseSensitive, params)
template loadFile*(_: type Toml, filename: string,
RecordType: distinct type,
tomlCase: TomlCase,
params: varargs[untyped]): auto =
tomlLoadImpl(filename, RecordType, "", tomlCase, params)
template loadFile*(_: type Toml, filename: string,
RecordType: distinct type,
params: varargs[untyped]): auto =
tomlLoadImpl(filename, RecordType, "", TomlCaseSensitive, params)