yaml_rust2::yaml

Enum 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 is_hash(&self) -> bool

Return whether self is a Yaml::Hash 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

source§

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

Perform indexing if self is a mapping.

§Return

If self is a Yaml::Hash, returns an immutable borrow to the value associated to the given key in the hash.

This function returns a Yaml::BadValue if the underlying Hash does not contain Yaml::String{idx} as a key.

This function also returns a Yaml::BadValue if self is not a Yaml::Hash.

source§

type Output = Yaml

The returned type after indexing.
source§

impl Index<usize> for Yaml

source§

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

Perform indexing if self is a sequence or a mapping.

§Return

If self is a Yaml::Array, returns an immutable borrow to the value located at the given index in the array.

Otherwise, if self is a Yaml::Hash, returns a borrow to the value whose key is Yaml::Integer(idx) (this would not work if the key is Yaml::String("1").

This function returns a Yaml::BadValue if the index given is out of range. If self is 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 not contain Yaml::Integer(idx) as a key.

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

source§

type Output = Yaml

The returned type after indexing.
source§

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

source§

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

Perform indexing if self is a mapping.

Since we cannot return a mutable borrow to a static Yaml::BadValue as we return an immutable one in [Index<&'a str>], this function panics on out of bounds.

§Panics

This function panics if the given key is not contained in self (as per IndexMut).

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

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.

Since we cannot return a mutable borrow to a static Yaml::BadValue as we return an immutable one in Index<usize>, this function panics on out of bounds.

§Panics

This function panics if the index given is out of range (as per IndexMut). If self is 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 not 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

source§

fn into_iter(self) -> Self::IntoIter

Extract the Array from self and iterate over it.

If self is not of the Yaml::Array variant, this function will not panic or return an error (as per the IntoIterator trait it cannot) but will instead return an iterator over an empty Array. Callers have to ensure (using Yaml::is_array, matches or something similar) that the Yaml object is a Yaml::Array if they want to do error handling.

§Examples

// An array of 2 integers, 1 and 2.
let arr = &YamlLoader::load_from_str("- 1\n- 2").unwrap()[0];

assert_eq!(arr.clone().into_iter().count(), 2);
assert_eq!(arr.clone().into_iter().next(), Some(Yaml::Integer(1)));
assert_eq!(arr.clone().into_iter().nth(1), Some(Yaml::Integer(2)));

// An empty array returns an empty iterator.
let empty = Yaml::Array(vec![]);
assert_eq!(empty.into_iter().count(), 0);

// A hash with 2 key-value pairs, `(a, b)` and `(c, d)`.
let hash = YamlLoader::load_from_str("a: b\nc: d").unwrap().remove(0);
// The hash has 2 elements.
assert_eq!(hash.as_hash().unwrap().iter().count(), 2);
// But since `into_iter` can't be used with a `Yaml::Hash`, `into_iter` returns an empty
// iterator.
assert_eq!(hash.into_iter().count(), 0);
source§

type Item = Yaml

The type of the elements being iterated over.
source§

type IntoIter = YamlIter

Which kind of iterator are we turning this into?
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,

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

impl PartialEq for Yaml

source§

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

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 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<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

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