Enum yaml_rust2::yaml::Yaml

source ·
pub enum Yaml {
    Real(String),
    Integer(i64),
    String(String),
    Boolean(bool),
    Array(Array),
    Hash(Hash),
    Alias(usize),
    Null,
    BadValue,
}
Expand description

A YAML node is stored as this Yaml enumeration, which provides an easy way to access your YAML document.

§Examples

use yaml_rust2::Yaml;
let foo = Yaml::from_str("-123"); // convert the string to the appropriate YAML type
assert_eq!(foo.as_i64().unwrap(), -123);

// iterate over an Array
let vec = Yaml::Array(vec![Yaml::Integer(1), Yaml::Integer(2)]);
for v in vec.as_vec().unwrap() {
    assert!(v.as_i64().is_some());
}

Variants§

§

Real(String)

Float types are stored as String and parsed on demand. Note that f64 does NOT implement Eq trait and can NOT be stored in BTreeMap.

§

Integer(i64)

YAML int is stored as i64.

§

String(String)

YAML scalar.

§

Boolean(bool)

YAML bool, e.g. true or false.

§

Array(Array)

YAML array, can be accessed as a Vec.

§

Hash(Hash)

YAML hash, can be accessed as a LinkedHashMap.

Insertion order will match the order of insertion into the map.

§

Alias(usize)

Alias, not fully supported yet.

§

Null

YAML null, e.g. null or ~.

§

BadValue

Accessing a nonexistent node via the Index trait returns BadValue. This simplifies error handling in the calling code. Invalid type conversion also returns BadValue.

Implementations§

source§

impl Yaml

source

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

Get a copy of the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with a copy of the $t contained. Otherwise, return None.

source

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

Get a copy of the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with a copy of the $t contained. Otherwise, return None.

source

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

Get a reference to the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some(&$t) with the $t contained. Otherwise, return None.

source

pub fn as_hash(&self) -> Option<&Hash>

Get a reference to the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some(&$t) with the $t contained. Otherwise, return None.

source

pub fn as_vec(&self) -> Option<&Array>

Get a reference to the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some(&$t) with the $t contained. Otherwise, return None.

source

pub fn as_mut_hash(&mut self) -> Option<&mut Hash>

Get a mutable reference to the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some(&mut $t) with the $t contained. Otherwise, return None.

source

pub fn as_mut_vec(&mut self) -> Option<&mut Array>

Get a mutable reference to the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some(&mut $t) with the $t contained. Otherwise, return None.

source

pub fn into_bool(self) -> Option<bool>

Get the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with the $t contained. Otherwise, return None.

source

pub fn into_i64(self) -> Option<i64>

Get the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with the $t contained. Otherwise, return None.

source

pub fn into_string(self) -> Option<String>

Get the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with the $t contained. Otherwise, return None.

source

pub fn into_hash(self) -> Option<Hash>

Get the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with the $t contained. Otherwise, return None.

source

pub fn into_vec(self) -> Option<Array>

Get the inner object in the YAML enum if it is a $t.

§Return

If the variant of self is Yaml::$yt, return Some($t) with the $t contained. Otherwise, return None.

source

pub fn is_null(&self) -> bool

Return whether self is a Yaml::Null node.

source

pub fn is_badvalue(&self) -> bool

Return whether self is a Yaml::BadValue node.

source

pub fn is_array(&self) -> bool

Return whether self is a Yaml::Array node.

source

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

Return the f64 value contained in this YAML node.

If the node is not a Yaml::Real YAML node or its contents is not a valid f64 string, None is returned.

source

pub fn into_f64(self) -> Option<f64>

Return the f64 value contained in this YAML node.

If the node is not a Yaml::Real YAML node or its contents is not a valid f64 string, None is returned.

source

pub fn or(self, other: Self) -> Self

If a value is null or otherwise bad (see variants), consume it and replace it with a given value other. Otherwise, return self unchanged.

use yaml_rust2::yaml::Yaml;

assert_eq!(Yaml::BadValue.or(Yaml::Integer(3)),  Yaml::Integer(3));
assert_eq!(Yaml::Integer(3).or(Yaml::BadValue),  Yaml::Integer(3));
source

pub fn borrowed_or<'a>(&'a self, other: &'a Self) -> &'a Self

See or for behavior. This performs the same operations, but with borrowed values for less linear pipelines.

source§

impl Yaml

source

pub fn from_str(v: &str) -> Yaml

Convert a string to a Yaml node.

Yaml does not implement std::str::FromStr since conversion may not fail. This function falls back to Yaml::String if nothing else matches.

§Examples
assert!(matches!(Yaml::from_str("42"), Yaml::Integer(42)));
assert!(matches!(Yaml::from_str("0x2A"), Yaml::Integer(42)));
assert!(matches!(Yaml::from_str("0o52"), Yaml::Integer(42)));
assert!(matches!(Yaml::from_str("~"), Yaml::Null));
assert!(matches!(Yaml::from_str("null"), Yaml::Null));
assert!(matches!(Yaml::from_str("true"), Yaml::Boolean(true)));
assert!(matches!(Yaml::from_str("3.14"), Yaml::Real(_)));
assert!(matches!(Yaml::from_str("foo"), Yaml::String(_)));

Trait Implementations§

source§

impl Clone for Yaml

source§

fn clone(&self) -> Yaml

Returns a copy 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 Debug for Yaml

source§

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

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

impl Hash for Yaml

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> Index<&'a str> for Yaml

§

type Output = Yaml

The returned type after indexing.
source§

fn index(&self, idx: &'a str) -> &Yaml

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

impl Index<usize> for Yaml

§

type Output = Yaml

The returned type after indexing.
source§

fn index(&self, idx: usize) -> &Yaml

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

impl<'a> IndexMut<&'a str> for Yaml

source§

fn index_mut(&mut self, idx: &'a str) -> &mut Yaml

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

impl IndexMut<usize> for Yaml

source§

fn index_mut(&mut self, idx: usize) -> &mut Yaml

Perform indexing if self is a sequence or a mapping.

§Panics

This function panics if the index given is out of range (as per IndexMut). If self i a Yaml::Array, this is when the index is bigger or equal to the length of the underlying Vec. If self is a Yaml::Hash, this is when the mapping sequence does no contain Yaml::Integer(idx) as a key.

This function also panics if self is not a Yaml::Array nor a Yaml::Hash.

source§

impl IntoIterator for Yaml

§

type Item = Yaml

The type of the elements being iterated over.
§

type IntoIter = YamlIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl Ord for Yaml

source§

fn cmp(&self, other: &Yaml) -> 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 + PartialOrd,

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

impl PartialEq for Yaml

source§

fn eq(&self, other: &Yaml) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Yaml

source§

fn partial_cmp(&self, other: &Yaml) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Yaml

source§

impl StructuralPartialEq for Yaml

Auto Trait Implementations§

§

impl Freeze for Yaml

§

impl RefUnwindSafe for Yaml

§

impl Send for Yaml

§

impl Sync for Yaml

§

impl Unpin for Yaml

§

impl UnwindSafe for Yaml

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.