KeyTree

KeyTree is an elegant markup language designed to convert human readable information into Rust data-structures. It is designed to be fast, to reduce cognitive load and to be easy to implement for one's own types. It has no dependencies on other crates and so is fast to compile. The format looks like

text hobbit: name: Frodo Baggins age: 98 friends: hobbit: name: Bilbo Baggins age: 176 hobbit: name: Samwise Gamgee age: 66

so data can be recursive. Also, it is easy to refer to a set of data using a path such as hobbit::friends::hobbit refers to a collection of two hobbits.

This library does not follow the standard Rust error handling pattern. If there is a parsing error it will crash or if there is an error in converting a value into a Rust type it will crash (with a nice error message). If you don't want this to happen, you will need to run this in its own thread/process.

Data Format Rules

text name: Frodo are used for struct fields and enum variants.

Keys refer to child keys or child key/value pairs indented on lines under it, for example

text hobbit: name: Frodo hobbit refers to the name of the struct or enum. In this way, the data maps simply to Rust data-structures.

test hobbit: name: Frodo name: Bilbo is a collection of hobbits.

text // comment hobbit: name: "Frodo"

Example

Into from KeyTree into Rust types is automatically implemented for Vec<T>, Option<T> and basic Rust types. KeyTree text can be automatically converted to these data types, making use of type inference. The at() function returns an iterator over KeyTree types that can be used to implement Into for your own types. The following example should cover 90 percent of use cases,

```rust use keytree::KeyTree; use keytree::parser::KeyTreeBuilder;

[derive(Debug)]

struct Hobbit { name: String, age: u32, friends: Vec, nick: Option, }

impl<'a> Into for KeyTree<'a> { fn into(self) -> Hobbit { Hobbit { name: self.at("hobbit::name"), age: self.at("hobbit::age"), friends: self.at("hobbit::friends::hobbit"), nick: self.op("hobbit::nick"), } } }

fn main() { let s = r#" hobbit: name: Frodo Baggins age: 98 friends: hobbit: name: Bilbo Baggins age: 176 hobbit: name: Samwise Gamgee age: 66 nick: Sam"#;

let core = KeyTreeBuilder::parse(s);
let hobbit: Hobbit = KeyTree::from_core(&core).into();
dbg!(&hobbit);

} ```