Skip to content

Commit 9a3e66b

Browse files
committed
v2.0.0: importved xik and jik's code, renamed decoder and encoder procedures and hooks, added inline procedures, improved tests and docs
1 parent 0725fc8 commit 9a3e66b

File tree

13 files changed

+1229
-893
lines changed

13 files changed

+1229
-893
lines changed

kdl.nimble

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "1.2.4"
3+
version = "2.0.0"
44
author = "Patitotective"
55
description = "KDL document language Nim implementation"
66
license = "MIT"
@@ -12,7 +12,12 @@ skipFiles = @["src/kdl/query.nim", "src/kdl/schema.nim"]
1212
requires "nim >= 1.6.0"
1313

1414
task docs, "Generate documentation":
15+
# We create the prefs module documentation separately because it is not imported in the main kdl file as it's not backed:js friendly
1516
exec "nim doc --outdir:docs/kdl --index:on src/kdl/prefs.nim"
1617
exec "echo \"<meta http-equiv=\\\"Refresh\\\" content=\\\"0; url='kdl/prefs.html'\\\" />\" >> docs/prefs.html"
18+
19+
# Here we make it so when you click 'Index' in the prefs.html file it redirects to theindex.html.
20+
exec "echo \"<meta http-equiv=\\\"Refresh\\\" content=\\\"0; url='../theindex.html'\\\" />\" >> docs/kdl/theindex.html"
21+
1722
exec "nim doc --git.url:https://github.com/Patitotective/kdl-nim --git.commit:main --outdir:docs --project src/kdl.nim"
1823
exec "echo \"<meta http-equiv=\\\"Refresh\\\" content=\\\"0; url='kdl.html'\\\" />\" >> docs/index.html"

src/kdl.nim

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## # kdl-nim
2-
## kdl-nim is an implementation of the [KDL document language](https://kdl.dev) in the Nim programming language.
2+
## kdl-nim is an implementation of the [KDL document language](https://kdl.dev) v1.0.0 in the Nim programming language.
33
##
44
## ## Installation
55
## ```
@@ -8,18 +8,19 @@
88
##
99
## ## Overview
1010
## ### Parsing KDL
11-
## kdl-nim parses strings (or files) into a `KdlDoc` which is a sequence of `KdlNode`s.
11+
## kdl-nim parses strings, files or streams into a `KdlDoc` which is a sequence of `KdlNode`s.
1212
##
1313
## Each `KdlNode` holds a name, an optional type annotation (tag), zero ore more arguments, zero or more properties and optionally children nodes.
14-
##
15-
## Arguments and properties' values are represented by an object variant `KdlVal`. `KdlVal` can be any of `KString`, `KFloat`, `KBool`, `KNull` or `KInt`.
14+
## Arguments are a sequence of values, while properties are an unordered table of string and values.
15+
## Arguments and properties' values are represented by the object variant `KdlVal`. `KdlVal` can be of any kind `KString`, `KFloat`, `KBool`, `KNull` or `KInt`.
1616
runnableExamples:
17-
let doc = parseKdl("node 1 null {child \"abc\" true}") # You can also read files using parseKdlFile("file.kdl")
18-
19-
assert doc[0].args[0].isInt() # 1
20-
assert doc[0].args[1].isNull() # null
21-
assert doc[0].children[0].args[0].isString() # "abc"
22-
assert doc[0].children[0].args[1].isBool() # true
17+
let doc = parseKdl("node (i8)1 null key=\"val\" {child \"abc\" true}") # You can also read files using parseKdlFile("file.kdl")
18+
assert doc == @[
19+
initKNode("node",
20+
args = @[initKVal(1, "i8".some), initKNull()],
21+
props = {"key": initKVal("val")}.toTable,
22+
children = @[initKNode("child", args = @[initKVal("abc"), initKVal(true)])])
23+
]
2324

2425
## ### Reading nodes
2526
runnableExamples:
@@ -51,9 +52,8 @@ runnableExamples:
5152
assert doc[0].args[0].get(float32) == 1f
5253
assert doc[0].args[1].get(int) == 3
5354
assert doc[0].args[2].get(uint8) == 255u8
55+
assert doc[0].args[0].get(string) == "1"
5456

55-
## It only converts between numbers, you can't `val.get(string)` if `val.isBool()`.
56-
##
5757
## ### Setting values
5858
runnableExamples:
5959
var doc = parseKdl("node 1 3.14 {child \"abc\" true}")
@@ -72,9 +72,9 @@ runnableExamples:
7272
assert doc[0].children[0].args[0] == "def"
7373

7474
## ### Creating KDL
75-
## To create KDL documents, nodes or values without parsing you can also use the `toKdl`, `toKdlNode` and `toKdlVal` macros which have a similar syntax to KDL:
75+
## To create KDL documents, nodes or values without parsing or object constructors you can use the `toKdlDoc`, `toKdlNode` and`toKdlVal` macros which have a similar syntax to KDL:
7676
runnableExamples:
77-
let doc = toKdl:
77+
let doc = toKdlDoc:
7878
node[tag](1, true, nil, key="val"):
7979
child(3.14[pi])
8080

@@ -85,15 +85,24 @@ runnableExamples:
8585
let node = toKdlNode: numbers(1, 2.13, 3.1e-10)
8686
assert node == parseKdl("numbers 1 2.13 3.1e-10")[0]
8787

88-
assert toKdlVal("abc") == parseKdl("node \"abc\"")[0].args[0]
88+
assert toKdlVal("abc"[tag]) == parseKdl("node (tag)\"abc\"")[0].args[0]
89+
90+
## Furthermore there are the `toKdlArgs` and `toKdlProps` macros, they provide shortcuts for creating a sequence and a table of `KdlVal`:
91+
runnableExamples:
92+
assert toKdlArgs(1, 2[tag], "a") == [1.initKVal, 2.initKVal("tag".some), "a".initKVal]
93+
assert toKdlProps({"a": 1[tag], "b": 2}) == {"a": 1.initKVal("tag".some), "b": 2.initKVal}.toTable
94+
95+
## ## Compile flags
96+
## `-d:kdlDecoderAllowHoleyEnums`: to allow converting integers into holey enums.
97+
## `-d:kdlDecoderNoCaseTransitionError`: to not get a compile error when trying to change a discriminator field from an object variant in an init hook.
8998

9099
## ## More
91-
## Checkout these other useful modules as well:
92-
## - [kdl/decoder](kdl/decoder.html) for KDL deserializing
93-
## - [kdl/encoder](kdl/encoder.html) for KDL serializing
100+
## Checkout these other useful modules:
101+
## - [kdl/encoder](kdl/encoder.html) for KDL serializing (Nim objects to KDL)
102+
## - [kdl/decoder](kdl/decoder.html) for KDL deserializing (KDL to Nim objects)
94103
## - [kdl/xix](kdl/xik.html) for [XML-in-KDL](https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md)
95104
## - [kdl/jix](kdl/jix.html) for [JSON-in-KDL](https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md)
96-
## - [kdl/prefs](kdl/prefs.html) for simple preferences sytem.
105+
## - [kdl/prefs](kdl/prefs.html) for a simple preferences sytem.
97106

98107
import std/[algorithm, enumerate, strformat, strutils, sequtils, options, tables]
99108

@@ -183,3 +192,4 @@ proc writeFile*(path: string, doc: KdlDoc, pretty = false) =
183192
writeFile(path, doc.pretty())
184193
else:
185194
writeFile(path, $doc & '\n')
195+

0 commit comments

Comments
 (0)