Crate yamlscript
source ·Expand description
Rust binding/API for the libyamlscript shared library.
§Loading a YAMLScript file
The YAMLScript::load
function is the main entrypoint of the library.
It allows loading YAMLScript and returns a JSON object.
serde_json
is used to deserialize the JSON.
One can either use serde_json::Value
as a return type or a custom serde::Deserialize
able
type.
§Using serde_json::Value
// Create an instance of a YAMLScript object.
// This object holds data from the library and all execution context.
let ys = yamlscript::YAMLScript::new().unwrap();
// Load some YAMLScript.
let data = ys.load::<serde_json::Value>(
r#"!yamlscript/v0/data
key: ! inc(42)"#
)
.unwrap();
// Our YAMLScript returns a `serde_json::Value` which holds an object.
let data = data.as_object().unwrap();
// We have a `key` field which holds the value 43.
assert_eq!(data.get("key").unwrap().as_u64().unwrap(), 43);
§Using a user-defined type
#[derive(Deserialize)]
struct Foo {
key: u64,
}
// Create an instance of a YAMLScript object.
// This object holds data from the library and all execution context.
let ys = yamlscript::YAMLScript::new().unwrap();
// Load some YAMLScript and deserialize as `Foo`.
let foo = ys.load::<Foo>(
r#"!yamlscript/v0/data
key: ! inc(42)"#
)
.unwrap();
// Our `foo` object has its key field set to 43.
assert_eq!(foo.key, 43);
Structs§
- A wrapper around libyamlscript.
Enums§
- An error with the binding.