Enum nereon::Value

source ·
pub enum Value {
    String(String),
    Table(HashMap<String, Value>),
    List(Vec<Value>),
}
Expand description

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()));

Variants§

§

String(String)

§

Table(HashMap<String, Value>)

§

List(Vec<Value>)

Implementations§

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.

Example
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());

Convert a Value into any type implementing FromValue

This function takes an Option<Value>. With some types, such as Option, None may be converted successfully.

Example
use nereon::Value;
let v = Value::from("42");
assert_eq!(Value::convert(Some(v)), Ok(42));

Get a Value from a Value::Table by key

Example
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

Example
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.

Example
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.

Example
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.

Example
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.

Example
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.

Example
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.

Example
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.

Example
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.

Example
assert_eq!(Value::List(vec![]).is_list(), true);
assert_eq!(Value::from("42").is_list(), false);

Convert a Value into a NOC String

Example
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

Example
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()));

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.