Enum nereon::Value[][src]

pub enum Value {
    String(String),
    Table(HashMap<String, Value>),
    List(Vec<Value>),
}

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

Methods

impl Value
[src]

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

impl Debug for Value
[src]

Formats the value using the given formatter. Read more

impl PartialEq for Value
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Clone for Value
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<'a> From<&'a str> for Value
[src]

Performs the conversion.

impl From<String> for Value
[src]

Performs the conversion.

impl<'a> From<&'a OsStr> for Value
[src]

Performs the conversion.

impl From<OsString> for Value
[src]

Performs the conversion.

impl<S, V> From<HashMap<S, V>> for Value where
    Value: From<V>,
    String: From<S>,
    S: Eq + Hash
[src]

Performs the conversion.

impl<V> From<Vec<V>> for Value where
    Value: From<V>, 
[src]

Performs the conversion.

impl FromValue for Value
[src]

Auto Trait Implementations

impl Send for Value

impl Sync for Value