Address some TODO items in loadFile and saveFile

This commit is contained in:
Zahary Karadjov 2020-04-14 19:34:44 +03:00
parent 8f6e86350e
commit 67bb6a77bb
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 12 additions and 23 deletions

View File

@ -40,7 +40,7 @@ proc readValue*(reader: var auto, T: type): T =
reader.readValue(result) reader.readValue(result)
template decode*(Format: distinct type, template decode*(Format: distinct type,
input: openarray[byte], input: string,
RecordType: distinct type, RecordType: distinct type,
params: varargs[untyped]): auto = params: varargs[untyped]): auto =
# TODO, this is dusplicated only due to a Nim bug: # TODO, this is dusplicated only due to a Nim bug:
@ -75,22 +75,11 @@ template loadFile*(Format: distinct type,
filename: string, filename: string,
RecordType: distinct type, RecordType: distinct type,
params: varargs[untyped]): auto = params: varargs[untyped]): auto =
mixin init, ReaderType mixin init, ReaderType, readValue
var stream = openFile(filename)
try:
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var reader = init(ReaderType(Format), stream, params)
else:
var reader = init(ReaderType(Format), stream)
var stream = fileInput(filename)
var reader = unpackArgs(init, [ReaderType(Format), stream, params])
reader.readValue(RecordType) reader.readValue(RecordType)
finally:
# TODO: destructors
if not stream.isNil:
stream[].close()
template loadFile*[RecordType](Format: type, template loadFile*[RecordType](Format: type,
filename: string, filename: string,
@ -98,13 +87,13 @@ template loadFile*[RecordType](Format: type,
params: varargs[untyped]) = params: varargs[untyped]) =
record = loadFile(Format, filename, RecordType, params) record = loadFile(Format, filename, RecordType, params)
template saveFile*(Format: type, filename: string, args: varargs[untyped]) = template saveFile*(Format: type, filename: string, value: auto, params: varargs[untyped]) =
when false: mixin init, WriterType, writeValue
# TODO use faststreams output stream
discard var stream = fileOutput(filename)
else: var writer = unpackArgs(init, [WriterType(Format), stream, params])
let bytes = Format.encode(args) writer.writeValue(value)
writeFile(filename, cast[string](bytes)) flush stream
template borrowSerialization*(Alias: distinct type, template borrowSerialization*(Alias: distinct type,
OriginalType: distinct type) {.dirty.} = OriginalType: distinct type) {.dirty.} =