about summary refs log tree commit diff stats
path: root/src/io/serialize.nim
blob: 3ffa40acf52dac8518baf6821fc7319ba5b179c5 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Write data to streams.

import std/options
import std/sets
import std/streams
import std/tables

import types/blob
import types/formdata
import types/url
import types/opt

proc sread*(stream: Stream, n: var SomeNumber)
func slen*(n: SomeNumber): int

proc sread*[T](stream: Stream, s: var set[T])
func slen*[T](s: set[T]): int

proc sread*[T: enum](stream: Stream, x: var T)
func slen*[T: enum](x: T): int

proc sread*(stream: Stream, s: var string)
func slen*(s: string): int

proc sread*(stream: Stream, b: var bool)
func slen*(b: bool): int

proc sread*(stream: Stream, url: var URL)
func slen*(url: URL): int

proc sread*(stream: Stream, tup: var tuple)
func slen*(tup: tuple): int

proc sread*[I, T](stream: Stream, a: var array[I, T])
func slen*[I, T](a: array[I, T]): int

proc sread*(stream: Stream, s: var seq)
func slen*(s: seq): int

proc sread*[U, V](stream: Stream, t: var Table[U, V])
func slen*[U, V](t: Table[U, V]): int

proc sread*(stream: Stream, obj: var object)
func slen*(obj: object): int

proc sread*(stream: Stream, obj: var ref object)
func slen*(obj: ref object): int

proc sread*(stream: Stream, part: var FormDataEntry)
func slen*(part: FormDataEntry): int

proc sread*(stream: Stream, blob: var Blob)
func slen*(blob: Blob): int

proc sread*[T](stream: Stream, o: var Option[T])
func slen*[T](o: Option[T]): int

proc sread*[T, E](stream: Stream, o: var Result[T, E])
func slen*[T, E](o: Result[T, E]): int

proc sread*(stream: Stream, n: var SomeNumber) =
  if stream.readData(addr n, sizeof(n)) < sizeof(n):
    raise newException(EOFError, "eof")

func slen*(n: SomeNumber): int =
  return sizeof(n)

proc sread*[T: enum](stream: Stream, x: var T) =
  var i: int
  stream.sread(i)
  x = cast[T](i)

func slen*[T: enum](x: T): int =
  return sizeof(int)

proc sread*[T](stream: Stream, s: var set[T]) =
  var len: int
  stream.sread(len)
  for i in 0 ..< len:
    var x: T
    stream.sread(x)
    s.incl(x)

func slen*[T](s: set[T]): int =
  result = slen(s.card)
  for x in s:
    result += slen(x)

proc sread*(stream: Stream, s: var string) =
  var len: int
  stream.sread(len)
  if len > 0:
    s = newString(len)
    prepareMutation(s)
    if stream.readData(addr s[0], len) < len:
      raise newException(EOFError, "eof")
  else:
    s = ""

func slen*(s: string): int =
  slen(s.len) + s.len

proc sread*(stream: Stream, b: var bool) =
  var n: uint8
  stream.sread(n)
  if n == 1u8:
    b = true
  else:
    assert n == 0u8
    b = false

func slen*(b: bool): int =
  return sizeof(uint8)

proc sread*(stream: Stream, url: var URL) =
  var s: string
  stream.sread(s)
  if s == "":
    url = nil
  else:
    let x = newURL(s)
    if x.isSome:
      url = x.get
    else:
      url = nil

func slen*(url: URL): int =
  if url == nil:
    return slen("")
  return slen(url.serialize())

proc sread*(stream: Stream, tup: var tuple) =
  for f in tup.fields:
    stream.sread(f)

func slen*(tup: tuple): int =
  for f in tup.fields:
    result += slen(f)

proc sread*[I, T](stream: Stream; a: var array[I, T]) =
  for x in a.mitems:
    stream.sread(x)

func slen*[I, T](a: array[I, T]): int =
  for x in a:
    result += slen(x)

proc sread*(stream: Stream, s: var seq) =
  var len: int
  stream.sread(len)
  s.setLen(len)
  for x in s.mitems:
    stream.sread(x)

func slen*(s: seq): int =
  result = slen(s.len)
  for x in s:
    result += slen(x)

proc sread*[U, V](stream: Stream, t: var Table[U, V]) =
  var len: int
  stream.sread(len)
  for i in 0..<len:
    var k: U
    stream.sread(k)
    var v: V
    stream.sread(v)
    t[k] = v

func slen*[U, V](t: Table[U, V]): int =
  result = slen(t.len)
  for k, v in t:
    result += slen(k)
    result += slen(v)

proc sread*(stream: Stream, obj: var object) =
  for f in obj.fields:
    stream.sread(f)

func slen*(obj: object): int =
  for f in obj.fields:
    result += slen(f)

proc sread*(stream: Stream, obj: var ref object) =
  var n: bool
  stream.sread(n)
  if n:
    new(obj)
    stream.sread(obj[])

func slen*(obj: ref object): int =
  result = slen(obj != nil)
  if obj != nil:
    result += slen(obj[])

proc sread*(stream: Stream, part: var FormDataEntry) =
  var isstr: bool
  stream.sread(isstr)
  if isstr:
    part = FormDataEntry(isstr: true)
  else:
    part = FormDataEntry(isstr: false)
  stream.sread(part.name)
  stream.sread(part.filename)
  if part.isstr:
    stream.sread(part.svalue)
  else:
    stream.sread(part.value)

func slen*(part: FormDataEntry): int =
  result += slen(part.isstr)
  result += slen(part.name)
  result += slen(part.filename)
  if part.isstr:
    result += slen(part.svalue)
  else:
    result += slen(part.value)

proc sread*(stream: Stream, blob: var Blob) =
  var isfile: bool
  stream.sread(isfile)
  if isfile:
    var file = new WebFile
    file.isfile = true
    stream.sread(file.path)
    blob = file
  else:
    blob = Blob()
    stream.sread(blob.ctype)
    stream.sread(blob.size)
    let buffer = alloc(blob.size)
    blob.buffer = buffer
    blob.deallocFun = proc() = dealloc(buffer)
    if blob.size > 0:
      let n = stream.readData(blob.buffer, int(blob.size))
      assert n == int(blob.size)

func slen*(blob: Blob): int =
  result += slen(blob.isfile)
  if blob.isfile:
    result = slen(WebFile(blob).path)
  else:
    result += slen(blob.ctype)
    result += slen(blob.size)
    result += int(blob.size) #TODO ??

proc sread*[T](stream: Stream, o: var Option[T]) =
  var x: bool
  stream.sread(x)
  if x:
    var m: T
    stream.sread(m)
    o = some(m)
  else:
    o = none(T)

func slen*[T](o: Option[T]): int =
  result = slen(o.isSome)
  if o.isSome:
    result += slen(o.get)

proc sread*[T, E](stream: Stream, o: var Result[T, E]) =
  var x: bool
  stream.sread(x)
  if x:
    when not (T is void):
      var m: T
      stream.sread(m)
      o.ok(m)
    else:
      o.ok()
  else:
    when not (E is void):
      var e: E
      stream.sread(e)
      o.err(e)
    else:
      o.err()

func slen*[T, E](o: Result[T, E]): int =
  result = slen(o.isSome)
  if o.isSome:
    when not (T is void):
      result += slen(o.get)
  else:
    when not (E is void):
      result += slen(o.error)