Value

Enum Value 

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

Source

pub fn as_str(&self) -> Option<&'s str>

Returns the underlying string value, if it is one

Source

pub fn as_int(&self) -> Option<i64>

Returns the underlying integer value, if it is one

Source

pub fn as_float(&self) -> Option<f64>

Returns the underlying float value, if it is one

Source

pub fn as_bool(&self) -> Option<bool>

Returns the underlying boolean value, if it is one

Source

pub fn as_list(&self) -> Option<&Vec<Value<'s>>>

Returns the underlying list value, if it is one

Source

pub fn as_dict(&self) -> Option<&HashMap<&'s str, Value<'s>>>

Returns the underlying dictionary value, if it is one

Source

pub fn is_none(&self) -> bool

Returns true if the value is None

Source

pub fn is_str(&self) -> bool

Returns true if the value is a string

Source

pub fn is_int(&self) -> bool

Returns true if the value is an integer

Source

pub fn is_float(&self) -> bool

Returns true if the value is a float

Source

pub fn is_bool(&self) -> bool

Returns true if the value is a boolean

Source

pub fn is_list(&self) -> bool

Returns true if the value is a list

Source

pub fn is_dict(&self) -> bool

Returns true if the value is a dictionary

Source

pub fn len(&self) -> Option<usize>

Returns the length of the value, if it is a list or dictionary

Source

pub fn is_empty(&self) -> bool

Returns true if the value is empty, if it is a list or dictionary

Trait Implementations§

Source§

impl<'s> Clone for Value<'s>

Source§

fn clone(&self) -> Value<'s>

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<'s> Debug for Value<'s>

Source§

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

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

impl<'s> Display for Value<'s>

Source§

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

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

impl<'i, 's> Index<&'i str> for Value<'s>

Source§

type Output = Value<'s>

The returned type after indexing.
Source§

fn index(&self, key: &'i str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'s> Index<usize> for Value<'s>

Source§

type Output = Value<'s>

The returned type after indexing.
Source§

fn index(&self, key: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'s> IndexMut<&'s str> for Value<'s>

Source§

fn index_mut(&mut self, key: &'s str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'s> IndexMut<usize> for Value<'s>

Source§

fn index_mut(&mut self, key: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'s> PartialEq for Value<'s>

Source§

fn eq(&self, other: &Value<'s>) -> 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<'s> StructuralPartialEq for Value<'s>

Auto Trait Implementations§

§

impl<'s> Freeze for Value<'s>

§

impl<'s> RefUnwindSafe for Value<'s>

§

impl<'s> Send for Value<'s>

§

impl<'s> Sync for Value<'s>

§

impl<'s> Unpin for Value<'s>

§

impl<'s> UnwindSafe for Value<'s>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.