diff --git a/src/preserves/private/preserves_schema_nim.nim b/src/preserves/private/preserves_schema_nim.nim index b86cb02..6bee656 100644 --- a/src/preserves/private/preserves_schema_nim.nim +++ b/src/preserves/private/preserves_schema_nim.nim @@ -349,14 +349,15 @@ proc generateNimFile*(scm: Schema; path: string) = knownTypes: TypeTable typeSection = newNode nkTypeSection procs: seq[PNode] + megaType: PNode if scm.embeddedType == "": typeSection.add nn(nkTypeDef, - ident"EmbeddedType", + exportIdent"EmbeddedType", newEmpty(), ident"void") else: typeSection.add nn(nkTypeDef, - ident"EmbeddedType", + exportIdent"EmbeddedType", newEmpty(), scm.embeddedType.ident) typeSection.add nn(nkTypeDef, @@ -371,6 +372,11 @@ proc generateNimFile*(scm: Schema; path: string) = else: var name = name name[0] = name[0].toUpperAscii + if megaType.isNil: + megaType = ident(name) + else: + megaType = nn(nkInfix, + ident"|", megaType, ident(name)) let t = nimTypeOf(scm, knownTypes, def, name) t.comment = "``" & $def & "``" case def.kind @@ -417,6 +423,34 @@ proc generateNimFile*(scm: Schema; path: string) = ident"std/typetraits", ident"preserves") collectRefImports(imports, scm) + procs.add nn(nkProcDef, + "$".ident.accQuote.toExport, + newEmpty(), + newEmpty(), + nn(nkFormalParams, + ident"string", + nn(nkIdentDefs, + ident"x", + megaType, + newEmpty())), + newEmpty(), + newEmpty(), + nn(nkStmtList, + nn(nkCall, ident"$", nn(nkCall, ident"toPreserve", ident"x", ident"EmbeddedType")))) + procs.add nn(nkProcDef, + "encode".ident.accQuote.toExport, + newEmpty(), + newEmpty(), + nn(nkFormalParams, + nn(nkBracketExpr, ident"seq", ident"byte"), + nn(nkIdentDefs, + ident"x", + megaType, + newEmpty())), + newEmpty(), + newEmpty(), + nn(nkStmtList, + nn(nkCall, ident"encode", nn(nkCall, ident"toPreserve", ident"x", ident"EmbeddedType")))) var module = newNode(nkStmtList).add( imports, typeSection