Enum ayaka_primitive::RawValue
source · pub enum RawValue {
Unit,
Bool(bool),
Num(i64),
Str(String),
}
Expand description
The basic and only type used in scripts.
assert_eq!(serde_yaml::from_str::<RawValue>("~").unwrap(), RawValue::Unit);
assert_eq!(serde_yaml::from_str::<RawValue>("true").unwrap(), RawValue::Bool(true));
assert_eq!(serde_yaml::from_str::<RawValue>("123").unwrap(), RawValue::Num(123));
assert_eq!(serde_yaml::from_str::<RawValue>("\"hello\"").unwrap(), RawValue::Str("hello".to_string()));
Variants§
Unit
The unit type. It is empty, just like None
or [()
] in Rust.
Bool(bool)
The boolean type.
Num(i64)
The number type. It’s i64
.
Str(String)
The string type.
Implementations§
source§impl RawValue
impl RawValue
sourcepub fn get_bool(&self) -> bool
pub fn get_bool(&self) -> bool
Gets a boolean from the value:
- A
RawValue::Unit
converts tofalse
. - A
RawValue::Num
converts tofalse
if and only if it’s zero. - A
RawValue::Str
converts tofalse
if and only if it’s empty.
let unit_value = RawValue::Unit;
assert!(!unit_value.get_bool());
let num_value = RawValue::Num(123);
assert!(num_value.get_bool());
let str_value = RawValue::Str("hello".to_string());
assert!(str_value.get_bool());
let empty_str_value = RawValue::Str(String::default());
assert!(!empty_str_value.get_bool());
sourcepub fn get_num(&self) -> i64
pub fn get_num(&self) -> i64
Gets a number from the value:
- A
RawValue::Unit
converts to 0. - A
RawValue::Bool
convertsfalse
to 0 andtrue
to 1. - A
RawValue::Str
converts to the length of the string.
let unit_value = RawValue::Unit;
assert_eq!(unit_value.get_num(), 0);
let bool_value = RawValue::Bool(true);
assert_eq!(bool_value.get_num(), 1);
let str_value = RawValue::Str("hello".to_string());
assert_eq!(str_value.get_num(), 5);
sourcepub fn get_str(&self) -> Cow<'_, str>
pub fn get_str(&self) -> Cow<'_, str>
Gets a string from the value:
- A
RawValue::Unit
converts to empty string. - A
RawValue::Bool
converts to “false” or “true”. - A
RawValue::Num
converts to the string representation of the number.
Be careful to use get_str().into_owned()
, if possible, use into_str()
instead.
let unit_value = RawValue::Unit;
assert_eq!(unit_value.get_str(), "");
let bool_value = RawValue::Bool(true);
assert_eq!(bool_value.get_str(), "true");
let num_value = RawValue::Num(123);
assert_eq!(num_value.get_str(), "123");
sourcepub fn into_str(self) -> String
pub fn into_str(self) -> String
Gets a string from the value:
- A
RawValue::Unit
converts to empty string. - A
RawValue::Bool
converts to “false” or “true”. - A
RawValue::Num
converts to the string representation of the number.
Trait Implementations§
source§impl<'de> Deserialize<'de> for RawValue
impl<'de> Deserialize<'de> for RawValue
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Ord for RawValue
impl Ord for RawValue
source§impl PartialEq for RawValue
impl PartialEq for RawValue
source§impl PartialOrd for RawValue
impl PartialOrd for RawValue
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl Eq for RawValue
impl StructuralEq for RawValue
impl StructuralPartialEq for RawValue
Auto Trait Implementations§
impl RefUnwindSafe for RawValue
impl Send for RawValue
impl Sync for RawValue
impl Unpin for RawValue
impl UnwindSafe for RawValue
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more