2020-10-21 05:01:48 +00:00
|
|
|
import
|
2020-10-22 05:09:34 +00:00
|
|
|
typetraits, options, tables,
|
2020-10-21 05:01:48 +00:00
|
|
|
serialization,
|
2020-10-22 05:09:34 +00:00
|
|
|
./utils, ./types
|
2020-10-21 05:01:48 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
WinregWriter* = object
|
|
|
|
hKey: HKEY
|
|
|
|
path: string
|
2020-10-21 09:07:25 +00:00
|
|
|
key: seq[string]
|
2020-10-21 05:01:48 +00:00
|
|
|
|
|
|
|
proc init*(T: type WinregWriter,
|
|
|
|
hKey: HKEY, path: string): T =
|
|
|
|
result.hKey = hKey
|
|
|
|
result.path = path
|
|
|
|
|
|
|
|
proc writeValue*(w: var WinregWriter, value: auto) =
|
|
|
|
mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL
|
2020-10-21 09:07:25 +00:00
|
|
|
# TODO: reduce allocation
|
2020-10-21 05:01:48 +00:00
|
|
|
|
2020-10-21 09:07:25 +00:00
|
|
|
when value is (SomePrimitives or range or string):
|
|
|
|
let path = constructPath(w.path, w.key)
|
|
|
|
discard setValue(w.hKey, path, w.key[^1], value)
|
|
|
|
elif value is Option:
|
|
|
|
if value.isSome:
|
|
|
|
w.writeValue value.get
|
2020-10-21 05:01:48 +00:00
|
|
|
elif value is (seq or array or openArray):
|
|
|
|
when uTypeIsPrimitives(type value):
|
2020-10-21 09:07:25 +00:00
|
|
|
let path = constructPath(w.path, w.key)
|
|
|
|
discard setValue(w.hKey, path, w.key[^1], value)
|
2020-10-21 05:01:48 +00:00
|
|
|
elif uTypeIsRecord(type value):
|
2020-10-21 09:07:25 +00:00
|
|
|
let key = w.key[^1]
|
|
|
|
for i in 0..<value.len:
|
|
|
|
w.key[^1] = key & $i
|
|
|
|
w.writeValue(value[i])
|
2020-10-21 05:01:48 +00:00
|
|
|
else:
|
|
|
|
const typeName = typetraits.name(value.type)
|
|
|
|
{.fatal: "Failed to convert to Winreg array an unsupported type: " & typeName.}
|
|
|
|
elif value is (object or tuple):
|
2020-10-21 09:07:25 +00:00
|
|
|
type RecordType = type value
|
|
|
|
w.key.add ""
|
|
|
|
value.enumInstanceSerializedFields(fieldName, field):
|
|
|
|
type FieldType = type field
|
|
|
|
w.key[^1] = fieldName
|
|
|
|
w.writeFieldIMPL(FieldTag[RecordType, fieldName, FieldType], field, value)
|
|
|
|
discard w.key.pop()
|
2020-10-21 05:01:48 +00:00
|
|
|
else:
|
|
|
|
const typeName = typetraits.name(value.type)
|
|
|
|
{.fatal: "Failed to convert to Winreg an unsupported type: " & typeName.}
|