Enum Value

Source
pub enum Value<'a> {
    Str(Cow<'a, str>),
    Obj(Obj<'a>),
}
Expand description

Enum representing all valid VDF values

VDF is composed of Keys and their respective Values where this represents the latter. A value is either going to be a Str(Cow<str>), or an Obj(Obj) that contains a list of keys and values.

let value_str = Value::Str(Cow::from("some text"));
let value_obj = Value::Obj(Obj::new());

Variants§

§

Str(Cow<'a, str>)

§

Obj(Obj<'a>)

Implementations§

Source§

impl<'a> Value<'a>

Source

pub fn is_str(&self) -> bool

Returns if the current value is the Str variant

use std::borrow::Cow;
use keyvalues_parser::{Obj, Value};

let value_str = Value::Str(Cow::default());
assert!(value_str.is_str());
Source

pub fn is_obj(&self) -> bool

Returns if the current value is the Obj variant

use keyvalues_parser::{Obj, Value};

let value_obj = Value::Obj(Obj::default());
assert!(value_obj.is_obj());
Source

pub fn get_str(&self) -> Option<&str>

Gets the inner &str if this is a Value::Str

let value = Value::Str(Cow::from("some text"));

if let Some(s) = value.get_str() {
    println!("value str: {}", s);
}
Source

pub fn get_obj(&self) -> Option<&Obj<'_>>

Gets the inner &Obj if this value is a Value::Obj

let value = Value::Obj(Obj::new());

if let Some(obj) = value.get_obj() {
    println!("value obj: {:?}", obj);
}
Source

pub fn get_mut_str(&mut self) -> Option<&mut Cow<'a, str>>

Gets the inner &mut str if this is a Value::Str

let mut value = Value::Str(Cow::from("some text"));
let mut inner_str = value.get_mut_str().unwrap();
inner_str.to_mut().make_ascii_uppercase();

assert_eq!(
    value,
    Value::Str(Cow::from("SOME TEXT"))
);
Source

pub fn get_mut_obj(&mut self) -> Option<&mut Obj<'a>>

Gets the inner &mut Obj if this is a Value::Obj

let mut value = Value::Obj(Obj::new());
let mut inner_obj = value.get_mut_obj().unwrap();
inner_obj.insert(Cow::from("new key"), vec![]);

// Prints:
// Value::Obj({
//    "new key": [],
// })
println!("{:?}", value);
Source

pub fn unwrap_str(self) -> Cow<'a, str>

Unwraps the Cow<str> from the Value::Str

§Panics

If the variant was Value::Obj

§Examples
use keyvalues_parser::Value;
use std::borrow::Cow;

let value = Value::Str(Cow::from("Sample text"));
assert_eq!(value.unwrap_str(), "Sample text");
use keyvalues_parser::{Value, Obj};

let value = Value::Obj(Obj::new());
value.unwrap_str(); // <-- panics
Source

pub fn unwrap_obj(self) -> Obj<'a>

Unwraps the Obj from the Value::Obj

§Panics

If the variant was Value::Str

§Examples
use keyvalues_parser::{Obj, Value};

let value = Value::Obj(Obj::new());
assert_eq!(value.unwrap_obj(), Obj::new());
use keyvalues_parser::Value;
use std::borrow::Cow;

let value = Value::Str(Cow::from("D'Oh"));
value.unwrap_obj(); // <-- panics
Source

pub fn expect_str(self, msg: &str) -> Cow<'a, str>

Refer to Value::unwrap_str. Same situation, but with a custom message

Source

pub fn expect_obj(self, msg: &str) -> Obj<'a>

Refer to Value::unwrap_obj. Same situation, but with a custom message

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

Source§

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

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

Source§

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

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

impl<'a> Display for Value<'a>

Source§

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

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

impl<'a> From<Pair<'a, Rule>> for Value<'a>

Source§

fn from(grammar_value: PestPair<'a, Rule>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Pair<'a, Rule>> for Value<'a>

Source§

fn from(grammar_value: PestPair<'a, Rule>) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for Value<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for Value<'a>

Source§

fn cmp(&self, other: &Value<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for Value<'a>

Source§

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

Source§

fn partial_cmp(&self, other: &Value<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Eq for Value<'a>

Source§

impl<'a> StructuralPartialEq for Value<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Value<'a>

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> Send for Value<'a>

§

impl<'a> Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

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.