Crate kfl

source ·
Expand description

KFL

A KDL file format parser with great error reporting and convenient derive macros.

About KDL

To give you some background on the KDL format. Here is a small example:

foo 1 "three" key="val" {
    bar
    (role)baz 1 2
}

Here is what are annotations for all the datum as described by the specification and this guide:

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)

Usage

Most common usage of this library is using derive and parse function:

use kfl::Decode;

#[derive(Decode)]
struct Config {
    #[kfl(children)]
    routes: Vec<Route>,
    #[kfl(children)]
    plugins: Vec<Plugin>,
}

#[derive(Decode)]
struct Route {
    #[kfl(argument)]
    path: String,
    #[kfl(children)]
    subroutes: Vec<Route>,
}

#[derive(Decode)]
struct Plugin {
    #[kfl(argument)]
    name: String,
    #[kfl(property)]
    url: String,
}

let config = kfl::decode_children::<Config>("example.kdl", r#"
    route "/api" {
        route "/api/v1"
    }
    plugin "http" url="https://example.org/http"
"#)?;

This parses into a vector of nodes as enums Config, but you also use some node as a root of the document if there is no properties and arguments declared:

#[derive(Decode)]
struct Document {
    #[kfl(child, unwrap(argument))]
    version: Option<String>,
    #[kfl(children)]
    routes: Vec<Route>,
    #[kfl(children)]
    plugins: Vec<Plugin>,
}

let config = kfl::decode_children::<Document>("example.kdl", r#"
    version "2.0"
    route "/api" {
        route "/api/v1"
    }
    plugin "http" url="https://example.org/http"
"#)?;

See description of Decode and DecodeScalar for the full reference on allowed attributes and parse modes.

License

Licensed under either of

Contribution

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.

Re-exports

pub use traits::Decode;
pub use traits::DecodePartial;
pub use traits::DecodeScalar;
pub use traits::DecodeChildren;
pub use traits::Encode;
pub use traits::EncodePartial;
pub use traits::EncodeScalar;
pub use traits::EncodeChildren;
pub use errors::Error;

Modules

Structures that represent abstract syntax tree (AST) of the KDL document
Decode support stuff
Error types for the kfl library
kfl supports to kinds of the span for parsing
Traits used for the library

Functions

Parse KDL text and decode it into Rust object
Parse KDL text and decode Rust object
Parse KDL text and decode Rust object providing extra context for the decoder
Parse KDL text and return AST

Derive Macros

Currently DecodeScalar derive is only implemented for enums