A KDL file format parser with great error reporting and convenient derive macros.
To give you some background on the KDL format. Here is a small example:
kdl
foo 1 key="val" "three" {
bar
(role)baz 1 2
}
Here is what are annotations for all the datum as described by the
[specification] and this guide:
text
foo 1 "three" key="val" { ╮
─┬─ ┬ ───┬─── ────┬──── │
│ │ │ ╰───── property (can be multiple) │
│ │ │ │
│ ╰────┴────────────── arguments │
│ │
└── node name ├─ node "foo", with
│ "bar" and "baz"
bar │ being children
(role)baz 1 2 │
──┬─ │
└────── type name for node named "baz" │
} ╯
(note, the order of properties doesn't matter as well as the order of
properties with respect to arguments, so I've moved arguments to have less
intersections for the arrows)
Most common usage of this library is using derive
and [parse] function:
```rust
enum TopLevelNode { Route(Route), Plugin(Plugin), }
struct Route {
#[knuffel(argument)]
path: String,
#[knuffel(children(name="route"))]
subroutes: Vec
struct Plugin { #[knuffel(argument)] name: String, #[knuffel(property)] url: String, }
let config = knuffel::parse::
```
This parses into a vector of nodes as enums TopLevelNode
, but you also use some node as a root of the document if there is no properties and arguments declared:
```rust,ignore
struct Document {
#[knuffel(child, unwrap(argument))]
version: Option
let config = parse::
See description of Decode and DecodeScalar for the full reference on allowed attributes and parse modes.
This crate publishes nice errors, like this:
To make them working, [miette]'s "fancy" feature must be enabled in the final
application's Cargo.toml
:
toml
[dependencies]
miette = { version="4.3.0", features=["fancy"] }
And the error returned from parser should be converted to [miette::Report] and
printed with debugging handler. The most manual way to do that is:
```rust
let config = match knuffel::parse::
But usually function that returns `miette::Result` is good enough:
rust,norun
use miette::{IntoDiagnostic, Context};
fn parseconfig(path: &str) -> miette::Result
See [miette guide] for other ways of configuring error output.
KDL is pronounced as cuddle. "Knuffel" means the same as cuddle in Dutch.
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.