Crates.io | kdl |
lib.rs | kdl |
version | |
source | src |
created_at | 2020-12-11 02:53:50.595151 |
updated_at | 2024-12-04 21:06:46.745961 |
description | Document-oriented KDL parser and API. Allows formatting/whitespace/comment-preserving parsing and modification of KDL text. |
homepage | https://kdl.dev |
repository | https://github.com/kdl-org/kdl-rs |
max_upload_size | |
id | 321695 |
Cargo.toml error: | TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
kdl
kdl
is a "document-oriented" parser and API for the KDL Document
Language, a node-based, human-friendly configuration and
serialization format. Unlike serde-based implementations, this crate
preserves formatting when editing, as well as when inserting or changing
values with custom formatting. This is most useful when working with
human-maintained KDL files.
You can think of this crate as
toml_edit
, but for KDL.
If you don't care about formatting or programmatic manipulation, you might
check out knuffel
or
kaydle
instead for serde (or
serde-like) parsing.
This crate supports parsing KDL 2.0.0-draft.6
use kdl::KdlDocument;
let doc_str = r#"
hello 1 2 3
// Comment
world prop=value {
child 1
child 2
child #inf
}
"#;
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);
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.
KdlError
implements miette::Diagnostic
and can be used to display
detailed, pretty-printed diagnostic messages when using miette::Result
and the "fancy"
feature flag for miette
:
# Cargo.toml
[dependencies]
miette = { version = "x.y.z", features = ["fancy"] }
fn main() -> miette::Result<()> {
"foo 1.".parse::<kdl::KdlDocument>()?;
Ok(())
}
This will display a message like:
Error:
× Expected valid value.
╭────
1 │ foo 1.
· ─┬
· ╰── invalid float
╰────
help: Floating point numbers must be base 10, and have numbers after the decimal point.
Multiple properties with the same name are allowed, and all duplicated
will be preserved, meaning those documents will correctly round-trip.
When using node.get()
/node["key"]
& company, the last property with
that name's value will be returned.
KDL itself does not specify a particular representation for numbers and accepts just about anything valid, no matter how large and how small. This means a few things:
i128
].f64
].#inf
, #-inf
, and #nan
evaluate to [f64::INFINITY
],
[f64::NEG_INFINITY
], and [f64::NAN
].KdlDocument::autoformat
] in which case the original
representation will be thrown away and the actual value will be used when
serializing.You must be at least 1.70.0
tall to get on this ride.
The code in this repository is covered by the Apache-2.0 License.