Rust parser for KDL
Go to file
Kat Marchán 5846bea079 deps: remove phf dep 2022-04-23 01:21:28 -07:00
.github feat(api): complete rewrite into document-oriented parser (#29) 2022-04-22 02:20:30 -07:00
examples feat(tests): add test for kdl-schema.kdl (#30) 2022-04-22 02:26:52 -07:00
src deps: remove phf dep 2022-04-23 01:21:28 -07:00
.editorconfig feat(api): complete rewrite into document-oriented parser (#29) 2022-04-22 02:20:30 -07:00
.gitignore initial commit 2020-12-10 18:42:50 -08:00
CHANGELOG.md doc: update changelog 2021-09-16 01:31:50 -07:00
CODE_OF_CONDUCT.md add coc 2020-12-14 20:30:58 -08:00
Cargo.toml deps: remove phf dep 2022-04-23 01:21:28 -07:00
LICENSE feat(license): change license to Apache-2.0 2021-09-15 23:59:34 -07:00
Makefile.toml chore: clean up and docs 2022-04-23 01:21:28 -07:00
README.md chore: clean up and docs 2022-04-23 01:21:28 -07:00
README.tpl chore: clean up and docs 2022-04-23 01:21:28 -07:00
cliff.toml meta: fix changelog gen 2021-09-16 00:01:34 -07:00

README.md

kdl

kdl is a "document-oriented" parser and API. That means that, unlike serde-based implementations, it's meant to preserve formatting when editing, as well as inserting values with custom formatting. This is useful when working with human-maintained KDL files.

You can think of this crate as toml_edit, but for KDL.

Example

use kdl::KdlDocument;

let doc_str = r#"
hello 1 2 3

world prop="value" {
    child 1
    child 2
}
"#;

let doc: KdlDocument = doc_str.parse().expect("failed to parse KDL");

assert_eq!(
    doc.get_args("hello"),
    vec![&1.into(), &2.into(), &3.into()]
);

assert_eq!(
    doc.get("world").map(|node| &node["prop"]),
    Some(&"value".into())
);

// Documents fully roundtrip:
assert_eq!(doc.to_string(), doc_str);

Controlling Formatting

By default, everything is created with default formatting. You can parse items manually to provide custom representations, comments, etc:

let node_str = r#"
  // indented comment
  "formatted" 1 /* comment */ \
    2;
"#;

let mut doc = kdl::KdlDocument::new();
doc.nodes_mut().push(node_str.parse().unwrap());

assert_eq!(&doc.to_string(), node_str);

KdlDocument, KdlNode, KdlEntry, and KdlIdentifier can all be parsed and managed this way.

License

The code in this repository is covered by the Apache-2.0 License.