pub enum Value<'s> {
None,
Str(&'s str),
Int(i64),
Float(f64),
Bool(bool),
List(Vec<Value<'s>>),
Dict(HashMap<&'s str, Value<'s>>),
}Expand description
Value type returned by Dent.
Represents a value in Dent, which can be a string, integer, float, boolean, list or dictionary.
Values are stored as references to the original string, so they are only
valid as long as the original string is valid. For values returned by parsing
files, whether by Dent::parse_file or the @import function, they should
be valid for the lifetime of the parser object.
§Accessing values
Values can be accessed using the [] operator, which takes either a string
or an integer. If the value is a dictionary, the string will be used as a
key. If the value is a list, the integer will be used as an index.
If a dictionary key or list index is not found, the value Value::None will
be returned during immutable access.
During mutable access, if a dictionary key is not found, a new entry will be
created with the value Value::None. If a list index is not found, the program
will panic.
§Examples
use dent_parse::{Dent, Value};
let parser = Dent::default();
let value = parser.parse("{ foo: 1 }").unwrap();
assert_eq!(value["foo"], Value::Int(1));use dent_parse::{Dent, Value};
let parser = Dent::default();
let value = parser.parse("[ 1 2 3 ]").unwrap();
assert_eq!(value[1], Value::Int(2));Variants§
None
Str(&'s str)
Int(i64)
Float(f64)
Bool(bool)
List(Vec<Value<'s>>)
Dict(HashMap<&'s str, Value<'s>>)
Implementations§
Source§impl<'s> Value<'s>
impl<'s> Value<'s>
Sourcepub fn as_list(&self) -> Option<&Vec<Value<'s>>>
pub fn as_list(&self) -> Option<&Vec<Value<'s>>>
Returns the underlying list value, if it is one
Sourcepub fn as_dict(&self) -> Option<&HashMap<&'s str, Value<'s>>>
pub fn as_dict(&self) -> Option<&HashMap<&'s str, Value<'s>>>
Returns the underlying dictionary value, if it is one