Main Value
enum with variants for strings, tables, and lists.
There is currently no constructor for Value instances though
they can be created from str
, String
, Vec
and HashMap
instances
assert_eq!(Value::String("42".to_owned()), Value::from("42"));
assert_eq!(Value::Table(HashMap::new()), Value::from(HashMap::<String, Value>::new()));
assert_eq!(Value::List(vec![]), Value::from(Vec::<Value>::new()));
insert
a Value
at the end of the branch
described by a list of keys. Branch nodes are Table
variants
of Value
.
Missing branch nodes are automatically created. Any String
and List
nodes along the path are overwritten.
Inserting a Table
where a Table
already exists causes the
tables to be merged. Old table entries are overwritten by new
entries with the same key.
let mut v = Value::Table(HashMap::new());
assert_eq!(v, parse_noc::<Value>("").unwrap());
v = v.insert(vec!["forename"], Value::from("John"));
assert_eq!(v, parse_noc::<Value>("forename John").unwrap());
v = v.insert(vec!["surname"], Value::from("Doe"));
assert_eq!(v, parse_noc::<Value>("forename John, surname Doe").unwrap());
v = v.insert(vec!["forename"], Value::from(vec!["John", "Reginald"]));
assert_eq!(v, parse_noc::<Value>("forename [John, Reginald], surname Doe").unwrap());
v = v.insert(vec!["forename", "first"], Value::from("John"));
assert_eq!(v, parse_noc::<Value>("forename { first John }, surname Doe").unwrap());
v = v.insert(vec!["forename", "middle"], Value::from("Reginald"));
assert_eq!(v, parse_noc::<Value>(
"forename { first John, middle Reginald }, surname Doe").unwrap());
Get a Value
from a Value::Table
by key
let v = parse_noc::<Value>("number 42").unwrap();
assert_eq!(v.get("number"), Some(&Value::String("42".to_owned())));
Lookup a Value
in a tree by keys
list
let v = parse_noc::<Value>("a { b { c 42 } }").unwrap();
assert_eq!(
v.lookup(vec!["a", "b", "c"]),
Some(&Value::String("42".to_owned())));
Get a reference to contained String
from Value
.
Returns None
if value isn't Value::String
variant.
assert_eq!(Value::from("42").as_str(), Some("42"));
assert_eq!(Value::List(vec![]).as_str(), None);
Test whether a Value
is a Value::String
variant.
assert_eq!(Value::from("42").is_string(), true);
assert_eq!(Value::List(vec![]).is_string(), false);
Get a reference to contained HashMap
from Value
Returns None
if value isn't Value::Table
variant.
assert_eq!(Value::from("42").as_table(), None);
assert_eq!(Value::Table(HashMap::new()).as_table(), Some(&HashMap::new()));
Get a mutable reference to contained HashMap
from Value
Returns None
if value isn't Value::Table
variant.
assert_eq!(Value::from("42").as_table(), None);
assert_eq!(Value::Table(HashMap::new()).as_table(), Some(&HashMap::new()));
Test whether a Value
is a Value::Table
variant.
assert_eq!(Value::Table(HashMap::new()).is_table(), true);
assert_eq!(Value::from("42").is_table(), false);
Get a reference to contained Vec
from Value
Returns None
if value isn't Value::List
variant.
assert_eq!(Value::List(vec![]).as_list(), Some(&vec![]));
assert_eq!(Value::from("42").as_list(), None);
Get a mutable reference to contained Vec
from Value
Returns None
if value isn't Value::List
variant.
assert_eq!(Value::List(vec![]).as_list(), Some(&vec![]));
assert_eq!(Value::from("42").as_list(), None);
Test whether a Value
is a Value::List
variant.
assert_eq!(Value::List(vec![]).is_list(), true);
assert_eq!(Value::from("42").is_list(), false);
Convert a Value
into a NOC String
assert_eq!(parse_noc::<Value>(r#"
forenames [John, Reginald]
surname Doe
"#).map(|v| v.as_noc_string()),
Ok(r#""forenames" ["John","Reginald"],"surname" "Doe""#.to_owned()));
Convert a Value
into a pretty NOC String
assert_eq!(parse_noc::<Value>("forenames [John, Reginald], surname Doe")
.map(|v| v.as_noc_string_pretty()),
Ok("\"forenames\" [\n\t\"John\"\n\t\"Reginald\"\n]\n\"surname\" \"Doe\"".to_owned()));