This module implements initializers, comparision, getters, setters, operatores and macros to manipilate KdlVal, KdlNode and KdlDoc.
Procs
proc `$`(doc: KdlDoc): string {....raises: [KdlError, Exception], tags: [RootEffect], forbids: [].}
- Source Edit
func `==`(node1, node2: KdlNode): bool {....raises: [KdlError, Exception], tags: [RootEffect], forbids: [].}
- Source Edit
proc contains(doc: KdlDoc; name: string): bool {....raises: [], tags: [], forbids: [].}
- Checks if doc has a node called name Source Edit
proc contains(node: KdlNode; child: KdlNode): bool {. ...raises: [KdlError, Exception], tags: [RootEffect], forbids: [].}
- Checks if node has the child children. Source Edit
proc get[T: Value](val: KdlVal; x: typedesc[T]): T
-
When x is string, stringifies val using $. when x is SomeNumber, converts val to x.
Example:
let val = initKFloat(3.14) assert val.get(int) == 3 assert val.get(uint) == 3u assert val.get(float) == 3.14 assert val.get(float32) == 3.14f assert val.get(range[0f .. 4f]) == 3.14f assert val.get(string) == "3.14"
Source Edit proc initKFloat(val: SomeFloat = float.default; tag = string.none): KdlVal
- Source Edit
proc initKString(val = string.default; tag = string.none): KdlVal {....raises: [], tags: [], forbids: [].}
- Source Edit
proc initKVal(val: bool; tag = string.none): KdlVal {....raises: [], tags: [], forbids: [].}
- Source Edit
Macros
macro toKdlArgs(args: varargs[untyped]): untyped
-
Creates an array of KdlVals by calling initKVal through args.
Example:
assert toKdlArgs(1, 2, "a"[tag]) == [1.initKVal, 2.initKVal, "a".initKVal("tag".some)] assert initKNode("name", args = toKdlArgs(nil, true, "b")) == initKNode("name", args = [initKNull(), true.initKVal, "b".initKVal])
Source Edit macro toKdlDoc(body: untyped): KdlDoc
-
Generate a KdlDoc from Nim's AST that is somewhat similar to KDL's syntax. body has to be an statement list
See also toKdlNode.
Example:
let doc = toKdlDoc: node numbers(10[u8], 20[i32], myfloat = 1.5[f32]): strings( "123e4567-e89b-12d3-a456-426614174000"[uuid], "2021-02-03"[date], filter = r"$\d+"[regex], ) person[author](name = "Alex") "i am also a node" color[RGB](r = 200, b = 100, g = 100)
Source Edit macro toKdlNode(body: untyped): KdlNode
-
Generate a KdlNode from Nim's AST that is somewhat similar to KDL's syntax.
- For nodes use call syntax: node(args, props).
- For properties use an equal expression: key=val.
- For children pass a block to a node: node(args, props): ...
Example:
let node = toKdlNode: numbers(10[u8], 20[i32], myfloat = 1.5[f32]): strings( "123e4567-e89b-12d3-a456-426614174000"[uuid], "2021-02-03"[date], filter = r"$\d+"[regex], ) person[author](name = "Alex") # It is the same as: # numbers (u8)10 (i32)20 myfloat=(f32)1.5 { # strings (uuid)"123e4567-e89b-12d3-a456-426614174000" (date)"2021-02-03" filter=(regex)r"$\d+" # (author)person name="Alex" # }
Source Edit macro toKdlProps(props: untyped): Table[string, KdlVal]
-
Creates a Table[string, KdlVal] from a array-of-tuples/table-constructor by calling initKVal through the values.
Example:
assert toKdlProps({"a": 1[i8], "b": 2}) == {"a": 1.initKVal("i8".some), "b": 2.initKVal}.toTable assert initKNode("name", props = toKdlProps({"c": nil, "d": true})) == initKNode("name", props = {"c": initKNull(), "d": true.initKVal}.toTable)
Source Edit