Module jupiter::ig::yaml

source ·
Expand description

Permits to load YAML data into a Doc.

Wraps the given YAML data and returns it as Doc.

§Examples

Loading an object into a doc:

let input = "
a_string: 'Test'
a_map:
    inner_key: 42
a_list:
    - 1
    - 2
    - test: Plain String
a_bool: true        
";

let yaml = &YamlLoader::load_from_str(input).unwrap()[0];
let doc = hash_to_doc(yaml.as_hash().unwrap(), |_| true).unwrap();

assert_eq!(
    yaml["a_string"].as_str(),
    doc.root().query("a_string").as_str()
);
assert_eq!(
    yaml["a_list"][0].as_i64(),
    doc.root().query("a_list").at(0).as_int()
);
assert_eq!(
    yaml["a_list"][1].as_i64(),
    doc.root().query("a_list").at(1).as_int()
);
assert_eq!(
    yaml["a_bool"].as_bool().unwrap(),
    doc.root().query("a_bool").as_bool()
);
assert_eq!(
    yaml["a_map"]["inner_key"].as_str(),
    doc.root().query("a_map.inner_key").as_str()
);

Loading a list into a doc:

let input = "
a_string: 'Test'
_skipped: true
---
a_string: 'Test1'
---
a_string: 'Test2'
---
42
";

let yaml = &YamlLoader::load_from_str(input).unwrap();
let doc = list_to_doc(yaml, | key | !key.starts_with("_")).unwrap();

assert_eq!(doc.root().len(), 4);
assert_eq!(yaml[0]["a_string"].as_str(), doc.root().at(0).query("a_string").as_str());
// Ensure that keys starting with _ are skipped (as we use !key.starts_with("_") as filter)...
assert_eq!(false, doc.root().at(0).query("_skipped").as_bool());
assert_eq!(yaml[1]["a_string"].as_str(), doc.root().at(1).query("a_string").as_str());
assert_eq!(yaml[2]["a_string"].as_str(), doc.root().at(2).query("a_string").as_str());
assert_eq!(yaml[3].as_i64(), doc.root().at(3).as_int());

Functions§