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 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("3.14"), Yaml::Real(_)));
assert!(matches!(Yaml::from_str("foo"), Yaml::String(_)));
Trait Implementations§
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.
§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
.