pub enum Value<'a> {
Str(Cow<'a, str>),
Obj(Obj<'a>),
}
Expand description
Enum representing all valid VDF values
VDF is composed of Key
s and their respective Value
s 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§
Implementations§
Source§impl<'a> Value<'a>
impl<'a> Value<'a>
Sourcepub fn is_str(&self) -> bool
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());
Sourcepub fn is_obj(&self) -> bool
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());
Sourcepub fn get_str(&self) -> Option<&str>
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);
}
Sourcepub fn get_obj(&self) -> Option<&Obj<'_>>
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);
}
Sourcepub fn get_mut_str(&mut self) -> Option<&mut Cow<'a, str>>
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"))
);
Sourcepub fn get_mut_obj(&mut self) -> Option<&mut Obj<'a>>
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);
Sourcepub fn unwrap_str(self) -> Cow<'a, str>
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
Sourcepub fn unwrap_obj(self) -> Obj<'a>
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
Sourcepub fn expect_str(self, msg: &str) -> Cow<'a, str>
pub fn expect_str(self, msg: &str) -> Cow<'a, str>
Refer to Value::unwrap_str. Same situation, but with a custom message
Sourcepub fn expect_obj(self, msg: &str) -> Obj<'a>
pub fn expect_obj(self, msg: &str) -> Obj<'a>
Refer to Value::unwrap_obj. Same situation, but with a custom message