Value

Enum 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§

Source§

impl Value

Source

pub fn insert<'a, I, V>(self, keys: I, value: V) -> Self
where I: IntoIterator<Item = &'a str>, V: Into<Value>,

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

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 Option, None may be converted successfully.

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

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

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

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

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

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

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

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

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

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

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

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

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

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a OsStr> for Value

Source§

fn from(s: &OsStr) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for Value

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(m: HashMap<S, V>) -> Self

Converts to this type from the input type.
Source§

impl From<OsString> for Value

Source§

fn from(s: OsString) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<V> From<Vec<V>> for Value
where Value: From<V>,

Source§

fn from(v: Vec<V>) -> Self

Converts to this type from the input type.
Source§

impl FromValue for Value

Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.