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
impl Yaml
Sourcepub fn as_bool(&self) -> Option<bool>
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.
Sourcepub fn as_i64(&self) -> Option<i64>
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.
Sourcepub fn as_str(&self) -> Option<&str>
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.
Sourcepub fn as_hash(&self) -> Option<&Hash>
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.
Sourcepub fn as_vec(&self) -> Option<&Array>
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.
Sourcepub fn as_mut_hash(&mut self) -> Option<&mut Hash>
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.
Sourcepub fn as_mut_vec(&mut self) -> Option<&mut Array>
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.
Sourcepub fn into_bool(self) -> Option<bool>
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.
Sourcepub fn into_i64(self) -> Option<i64>
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.
Sourcepub fn into_string(self) -> Option<String>
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.
Sourcepub fn into_hash(self) -> Option<Hash>
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.
Sourcepub fn into_vec(self) -> Option<Array>
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.
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Return whether self is a Yaml::Null node.
Sourcepub fn is_badvalue(&self) -> bool
pub fn is_badvalue(&self) -> bool
Return whether self is a Yaml::BadValue node.
Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Return whether self is a Yaml::Array node.
Sourcepub fn is_hash(&self) -> bool
pub fn is_hash(&self) -> bool
Return whether self is a Yaml::Hash node.
Sourcepub fn as_f64(&self) -> Option<f64>
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.
Sourcepub fn into_f64(self) -> Option<f64>
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.
Sourcepub fn or(self, other: Self) -> Self
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));Sourcepub fn borrowed_or<'a>(&'a self, other: &'a Self) -> &'a Self
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
impl Yaml
Sourcepub fn from_str(v: &str) -> Yaml
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("True"), Yaml::Boolean(true)));
assert!(matches!(Yaml::from_str("TRUE"), Yaml::Boolean(true)));
assert!(matches!(Yaml::from_str("false"), Yaml::Boolean(false)));
assert!(matches!(Yaml::from_str("False"), Yaml::Boolean(false)));
assert!(matches!(Yaml::from_str("FALSE"), Yaml::Boolean(false)));
assert!(matches!(Yaml::from_str("3.14"), Yaml::Real(_)));
assert!(matches!(Yaml::from_str("foo"), Yaml::String(_)));Trait Implementations§
Source§impl<'a> Index<&'a str> for Yaml
impl<'a> Index<&'a str> for Yaml
Source§fn index(&self, idx: &'a str) -> &Yaml
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§impl Index<usize> for Yaml
impl Index<usize> for Yaml
Source§fn index(&self, idx: usize) -> &Yaml
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§impl<'a> IndexMut<&'a str> for Yaml
impl<'a> IndexMut<&'a str> for Yaml
Source§fn index_mut(&mut self, idx: &'a str) -> &mut Yaml
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
impl IndexMut<usize> for Yaml
Source§fn index_mut(&mut self, idx: usize) -> &mut Yaml
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
impl IntoIterator for Yaml
Source§fn into_iter(self) -> Self::IntoIter
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);