Crates.io | kvon-rs |
lib.rs | kvon-rs |
version | 0.3.1 |
source | src |
created_at | 2022-08-17 00:10:40.749128 |
updated_at | 2022-09-13 15:16:29.629174 |
description | Parsing the KVON format |
homepage | https://kvon.org/ |
repository | https://github.com/kvon-format/kvon-rs |
max_upload_size | |
id | 647063 |
size | 51,971 |
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.
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(())
}