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§
Implementations§
Source§impl Value
impl Value
Sourcepub fn insert<'a, I, V>(self, keys: I, value: V) -> Self
pub fn insert<'a, I, V>(self, keys: I, value: V) -> Self
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());Sourcepub fn convert<T: FromValue>(v: Option<Value>) -> Result<T, ConversionError>
pub fn convert<T: FromValue>(v: Option<Value>) -> Result<T, ConversionError>
Convert a Value into any type implementing FromValue
This function takes an Option<Value>. With some types, such
as OptionNone may be converted successfully.
§Example
use nereon::Value;
let v = Value::from("42");
assert_eq!(Value::convert(Some(v)), Ok(42));Sourcepub fn get<'a>(&'a self, key: &str) -> Option<&'a Value>
pub fn get<'a>(&'a self, key: &str) -> Option<&'a Value>
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())));Sourcepub fn lookup<'a, I>(&'a self, keys: I) -> Option<&'a Value>where
I: IntoIterator<Item = &'a str>,
pub fn lookup<'a, I>(&'a self, keys: I) -> Option<&'a Value>where
I: IntoIterator<Item = &'a str>,
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())));Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
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);Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
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);Sourcepub fn as_table<'a>(&'a self) -> Option<&'a HashMap<String, Value>>
pub fn as_table<'a>(&'a self) -> Option<&'a HashMap<String, Value>>
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()));Sourcepub fn as_table_mut<'a>(&'a mut self) -> Option<&'a mut HashMap<String, Value>>
pub fn as_table_mut<'a>(&'a mut self) -> Option<&'a mut HashMap<String, Value>>
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()));Sourcepub fn is_table(&self) -> bool
pub fn is_table(&self) -> bool
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);Sourcepub fn as_list<'a>(&'a self) -> Option<&'a Vec<Value>>
pub fn as_list<'a>(&'a self) -> Option<&'a Vec<Value>>
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);Sourcepub fn as_list_mut<'a>(&'a mut self) -> Option<&'a mut Vec<Value>>
pub fn as_list_mut<'a>(&'a mut self) -> Option<&'a mut Vec<Value>>
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);Sourcepub fn is_list(&self) -> bool
pub fn is_list(&self) -> bool
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);Sourcepub fn as_noc_string(&self) -> String
pub fn as_noc_string(&self) -> String
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()));Sourcepub fn as_noc_string_pretty(&self) -> String
pub fn as_noc_string_pretty(&self) -> String
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()));