Crate kvon_rs

Source
Expand description

§KVON-rs

KVON is a human readable serialization format. This crates provides a parser that can deserialize KVON. Additionally, it also has a KVON encoder. For detailed examples, check the examples directory.

§Usage Example

Creating and parsing an object:

use kvon_rs::{object, parse_string};

static SOURCE: &'static str = "
a:
    b: 0
c: [1 2 [3 4]]
";

fn main() {
    let object1 = object! {
        a: {
            b: 0,
        },
        c: [1, 2, [3, 4]]
    };

    let object2 = parse_string(SOURCE).unwrap();

    assert_eq!(object1, object2);
}

Parsing and reading an object:

use kvon_rs::{
    parse_string,
    value::{GetterResult, PrimitiveValue, Value},
};

static SOURCE: &'static str = "
a:
    b: 0
c: [1 2 [3 4]]
";

fn main() -> GetterResult<()> {
    let object = parse_string(SOURCE).unwrap();

    // access nested values with if-let
    if let Value::Object(obj) = &object {
        let c = &obj["c"];
        if let Value::Array(arr) = c {
            if let Value::Array(arr) = &arr[2] {
                if let Value::Primitive(PrimitiveValue::Number(n)) = arr[1] {
                    assert_eq!(n, 4.0);
                }
            }
        }
    }

    // access nested values by unwrapping
    let n = object.get_objects()?["c"].get_vector()?[2].get_vector()?[1]
        .get_primitive()?
        .get_number()?;
    assert_eq!(n, 4.0);

    Ok(())
}

Modules§

error
indention
value

Macros§

array
Adapted from https://docs.rs/json/0.12.4/src/json/lib.rs.html.
object
Helper macro for creating instances of value::Value::Object. See the examples for usage.
value
Adapted from https://docs.rs/json/0.12.4/src/json/lib.rs.html.

Structs§

Parser
A struct that processes lines one by one, decoding them and building value::Values.

Functions§

encode_string_expanded
Encodes a value::Value into a string. This implementation will prefer to expand arrays and strings to multiple lines to improve readability.
parse_reader
Parses a std::io::Read into a value::Value.
parse_string
Parses a string into a value::Value.

Type Aliases§

ParserResult