kvon-rs

Crates.iokvon-rs
lib.rskvon-rs
version0.3.1
sourcesrc
created_at2022-08-17 00:10:40.749128
updated_at2022-09-13 15:16:29.629174
descriptionParsing the KVON format
homepagehttps://kvon.org/
repositoryhttps://github.com/kvon-format/kvon-rs
max_upload_size
id647063
size51,971
Roy Varon (Cringere)

documentation

README

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(())
}
Commit count: 6

cargo fmt