pub enum Value {
Representation(String, ScalarStyle, Option<Tag>),
Value(ScalarOwned),
Sequence(Vec<YamlOwned>),
Mapping(LinkedHashMap<YamlOwned, YamlOwned>),
Tagged(Tag, Box<YamlOwned>),
Alias(usize),
BadValue,
}Expand description
Wrapper around saphyr’s YamlOwned type for consistent API.
This re-exports the saphyr types to provide a stable API
that can be extended in the future without breaking changes.
We use YamlOwned instead of Yaml to avoid lifetime parameters.
A YAML node is stored as this Yaml enumeration, which provides an easy way to
access your YAML document.
§Examples
use saphyr::{Scalar, Yaml};
let foo = Yaml::value_from_str("-123"); // convert the string to the appropriate YAML type
assert_eq!(foo.as_integer().unwrap(), -123);
// iterate over an Sequence
let vec = Yaml::Sequence(vec![Yaml::Value(Scalar::Integer(1)), Yaml::Value(Scalar::Integer(2))]);
for v in vec.as_vec().unwrap() {
assert!(v.is_integer());
}Variants§
Representation(String, ScalarStyle, Option<Tag>)
The raw string from the input.
When the field is left in the Representation variant, methods that rely on the value
(e.g.: is_boolean, as_integer, into_floating_point, …) will always return
None.
This variant is only meant:
- As an optimization, when lazy-parsing is preferred.
- As a more generic way of handling keys in
Mappings (if user-defined key duplication detection is required.
Value(ScalarOwned)
The resolved value from the representation.
Sequence(Vec<YamlOwned>)
YAML sequence, can be accessed as a Vec.
Mapping(LinkedHashMap<YamlOwned, YamlOwned>)
YAML mapping, can be accessed as a LinkedHashMap.
Iteration order will match the order of insertion into the map and that of the document.
If keys use the Representation variant, equality will be based on their representation.
When comparing representations for equality, the string, scalar style and tags must
match. This means that '100' and "100", although similar in their value, have different
representations.
If keys use the Value variant, they will be compared by value. It is discouraged to use
floating point values as keys. ScalarOwned uses OrderedFloat for hash and equality.
Refer to their documentation for details on float comparisons.
Comparison between Representation variants and Value variants will always fail.
Users must ensure all keys in a map are of the same variant, as well as the query keys.
For complex keys, the Mapping and Sequence variants are compared for equality. Both
these comparisons are sensitive to the order of insertions. For instance, in the following
mapping, the two complex keys are considered different:
? { a: b, c: d }: foo
? { c: d, a: b }: barTagged(Tag, Box<YamlOwned>)
A tagged node.
Tags can be applied to any node, whether a scalar or a collection.
Alias(usize)
Alias, not fully supported yet.
BadValue
A variant used when parsing the representation of a scalar node fails.
The YAML is syntactically valid, but its contents are incoherent. See
ScalarOwned::parse_from_cow_and_metadata for details.
This variant is also used when stealing the contents of self, meaning self should no
longer be used. See Self::take for details
Implementations§
Source§impl YamlOwned
impl YamlOwned
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 Self::$variant, return Some($t) with a copy of the $t contained.
Otherwise, return None.
Sourcepub fn as_integer(&self) -> Option<i64>
pub fn as_integer(&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 Self::$variant, return Some($t) with a copy of the $t contained.
Otherwise, return None.
Sourcepub fn as_floating_point(&self) -> Option<f64>
pub fn as_floating_point(&self) -> Option<f64>
Get a copy of the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, 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 Self::$variant, return Some(&$t) with the $t contained. Otherwise,
return None.
Sourcepub fn as_bool_mut(&mut self) -> Option<&mut bool>
pub fn as_bool_mut(&mut self) -> Option<&mut bool>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn as_integer_mut(&mut self) -> Option<&mut i64>
pub fn as_integer_mut(&mut self) -> Option<&mut i64>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn as_floating_point_mut(&mut self) -> Option<&mut f64>
pub fn as_floating_point_mut(&mut self) -> Option<&mut f64>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, 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 Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn into_integer(self) -> Option<i64>
pub fn into_integer(self) -> Option<i64>
Get the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn into_floating_point(self) -> Option<f64>
pub fn into_floating_point(self) -> Option<f64>
Get the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, 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 Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn as_mapping(&self) -> Option<&LinkedHashMap<YamlOwned, YamlOwned>>
pub fn as_mapping(&self) -> Option<&LinkedHashMap<YamlOwned, YamlOwned>>
Get a reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&$t) with the $t contained. Otherwise,
return None.
Sourcepub fn as_sequence(&self) -> Option<&Vec<YamlOwned>>
pub fn as_sequence(&self) -> Option<&Vec<YamlOwned>>
Get a reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&$t) with the $t contained. Otherwise,
return None.
Sourcepub fn as_vec(&self) -> Option<&Vec<YamlOwned>>
pub fn as_vec(&self) -> Option<&Vec<YamlOwned>>
Get a reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&$t) with the $t contained. Otherwise,
return None.
Sourcepub fn as_mapping_mut(
&mut self,
) -> Option<&mut LinkedHashMap<YamlOwned, YamlOwned>>
pub fn as_mapping_mut( &mut self, ) -> Option<&mut LinkedHashMap<YamlOwned, YamlOwned>>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn as_sequence_mut(&mut self) -> Option<&mut Vec<YamlOwned>>
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<YamlOwned>>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn as_vec_mut(&mut self) -> Option<&mut Vec<YamlOwned>>
pub fn as_vec_mut(&mut self) -> Option<&mut Vec<YamlOwned>>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn into_mapping(self) -> Option<LinkedHashMap<YamlOwned, YamlOwned>>
pub fn into_mapping(self) -> Option<LinkedHashMap<YamlOwned, YamlOwned>>
Get the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn into_vec(self) -> Option<Vec<YamlOwned>>
pub fn into_vec(self) -> Option<Vec<YamlOwned>>
Get the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn into_sequence(self) -> Option<Vec<YamlOwned>>
pub fn into_sequence(self) -> Option<Vec<YamlOwned>>
Get the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some($t) with the $t contained. Otherwise,
return None.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_floating_point(&self) -> bool
pub fn is_floating_point(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_sequence(&self) -> bool
pub fn is_sequence(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_badvalue(&self) -> bool
pub fn is_badvalue(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_mapping(&self) -> bool
pub fn is_mapping(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_alias(&self) -> bool
pub fn is_alias(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_representation(&self) -> bool
pub fn is_representation(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_value(&self) -> bool
pub fn is_value(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn is_tag_node(&self) -> bool
pub fn is_tag_node(&self) -> bool
Check whether the YAML enum contains the given variant.
§Return
If the variant of self is Self::$variant, return true. Otherwise, return false.
Sourcepub fn get_tagged_node(&self) -> Option<&YamlOwned>
pub fn get_tagged_node(&self) -> Option<&YamlOwned>
Sourcepub fn get_tagged_node_mut(&mut self) -> Option<&mut YamlOwned>
pub fn get_tagged_node_mut(&mut self) -> Option<&mut YamlOwned>
Sourcepub fn is_empty_collection(&self) -> bool
pub fn is_empty_collection(&self) -> bool
Check whether the YAML enum is an empty sequence ([]) or empty mapping ({}).
§Return
If the variant of self is Self::Sequence(x) with x.is_empty() or Self::Mapping(x)
with y.is_empty(), return true. If self is a non-empty sequence, a non-empty mapping,
or not a collection, return false.
Sourcepub fn is_non_empty_collection(&self) -> bool
pub fn is_non_empty_collection(&self) -> bool
Check whether the YAML enum is a non-empty sequence or a non-empty mapping.
§Return
If the variant of self is Self::Sequence(x) with !x.is_empty() or Self::Mapping(x)
with !y.is_empty(), return true. If self is an empty sequence, an empty mapping, or
not a collection, return false.
Sourcepub fn parse_representation_recursive(&mut self) -> bool
pub fn parse_representation_recursive(&mut self) -> bool
Call Self::parse_representation on self and children nodes.
If self was Self::Value or Self::Alias upon calling, this function does nothing
and returns true.
If Self::parse_representation fails on a descendent node, this function will not short
circuit but still attempt to call Self::parse_representation on further nodes. Even if
all further nodes succeed, this function will still return false.
§Return
Returns true if all self and its children are successfully parsed, false otherwise.
Sourcepub fn or(self, other: YamlOwned) -> YamlOwned
pub fn or(self, other: YamlOwned) -> YamlOwned
If a value is null or otherwise bad (see variants), consume it and
replace it with a given value other. Otherwise, return self unchanged.
assert_eq!(
Yaml::Value(Scalar::Null).or(Yaml::Value(Scalar::Integer(3))),
Yaml::Value(Scalar::Integer(3))
);
assert_eq!(
Yaml::Value(Scalar::Integer(3)).or(Yaml::Value(Scalar::Integer(7))),
Yaml::Value(Scalar::Integer(3))
);Sourcepub fn borrowed_or<'a>(&'a self, other: &'a YamlOwned) -> &'a YamlOwned
pub fn borrowed_or<'a>(&'a self, other: &'a YamlOwned) -> &'a YamlOwned
See Self::or for behavior.
This performs the same operations, but with borrowed values for less linear pipelines.
Sourcepub fn contains_mapping_key(&self, key: &str) -> bool
pub fn contains_mapping_key(&self, key: &str) -> bool
Check whether self is a Self::Mapping and that it contains the given key.
This is equivalent to:
matches!(self, Self::Mapping(ref x) if x.contains_key(&Yaml::<'_>::String(key.into())))§Return
If the variant of self is Self::Mapping and the mapping contains the key, returns true.
Otherwise, returns false.
Sourcepub fn as_mapping_get(&self, key: &str) -> Option<&YamlOwned>
pub fn as_mapping_get(&self, key: &str) -> Option<&YamlOwned>
Return the value associated to the given key if self is a Self::Mapping.
This is equivalent to:
self.as_mapping().and_then(|mapping| mapping.get(key))§Return
If the variant of self is Self::Mapping and the mapping contains the key, returns the
value associated with it.
Otherwise, returns None.
Sourcepub fn as_mapping_get_mut(&mut self, key: &str) -> Option<&mut YamlOwned>
pub fn as_mapping_get_mut(&mut self, key: &str) -> Option<&mut YamlOwned>
Return the value associated to the given key if self is a Self::Mapping.
This is equivalent to:
self.as_mapping_mut().and_then(|mapping| mapping.get_mut(key))§Return
If the variant of self is Self::Mapping and the mapping contains the key, returns the
value associated with it.
Otherwise, returns None.
Sourcepub fn as_sequence_get(&self, idx: usize) -> Option<&YamlOwned>
pub fn as_sequence_get(&self, idx: usize) -> Option<&YamlOwned>
Return the value at the given index if self is a Self::Sequence.
This is equivalent to:
self.as_sequence().and_then(|seq| seq.get(idx))§Return
If the variant of self is Self::Sequence and the index is not out of bounds, returns
the value at the given index.
Otherwise, returns None.
Sourcepub fn as_sequence_get_mut(&mut self, idx: usize) -> Option<&mut YamlOwned>
pub fn as_sequence_get_mut(&mut self, idx: usize) -> Option<&mut YamlOwned>
Return the value at the given index if self is a Self::Sequence.
This is equivalent to:
self.as_sequence_mut().and_then(|seq| seq.get_mut(idx))§Return
If the variant of self is Self::Sequence and the index is not out of bounds, returns
the value at the given index.
Otherwise, returns None.
Source§impl YamlOwned
impl YamlOwned
Sourcepub fn as_str_mut(&mut self) -> Option<&mut str>
pub fn as_str_mut(&mut self) -> Option<&mut str>
Get a mutable reference to the inner object in the YAML enum if it is a $t.
§Return
If the variant of self is Self::$variant, return Some(&mut $t) with the $t contained.
Otherwise, return None.
Sourcepub fn parse_representation(&mut self) -> bool
pub fn parse_representation(&mut self) -> bool
If self is of the Self::Representation variant, parse it to the value.
If self was Self::Value, Self::Sequence, Self::Mapping or Self::Alias
upon calling, this function does nothing and returns true.
If parsing fails, *self is assigned Self::BadValue.
§Return
Returns true if self is successfully parsed, false otherwise.
Trait Implementations§
Source§impl<'key> Index<&'key str> for YamlOwned
impl<'key> Index<&'key str> for YamlOwned
Source§impl Index<usize> for YamlOwned
impl Index<usize> for YamlOwned
Source§fn index(&self, idx: usize) -> &YamlOwned
fn index(&self, idx: usize) -> &YamlOwned
Perform indexing if self is a sequence or a mapping.
§Panics
This function panics if the index given is out of range (as per Index). If self is a
[$t::Sequence], this is when the index is bigger or equal to the length of the underlying
Vec. If self is a [$t::Mapping], this is when the mapping sequence
does not contain Scalar::Integer(idx) as a key.
This function also panics if self is not a [$t::Sequence] nor a [$t::Mapping].
Source§impl IndexMut<usize> for YamlOwned
impl IndexMut<usize> for YamlOwned
Source§fn index_mut(&mut self, idx: usize) -> &mut YamlOwned
fn index_mut(&mut self, idx: usize) -> &mut YamlOwned
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 is
a [$t::Sequence], this is when the index is bigger or equal to the length of the
underlying Vec. If self is a [$t::Mapping], this is when the mapping sequence does
not contain Scalar::Integer(idx) as a key.
This function also panics if self is not a [$t::Sequence] nor a [$t::Mapping].
Source§impl IntoIterator for YamlOwned
impl IntoIterator for YamlOwned
Source§impl LoadableYamlNode<'_> for YamlOwned
impl LoadableYamlNode<'_> for YamlOwned
Source§fn from_bare_yaml(yaml: Yaml<'_>) -> YamlOwned
fn from_bare_yaml(yaml: Yaml<'_>) -> YamlOwned
Source§fn is_sequence(&self) -> bool
fn is_sequence(&self) -> bool
Source§fn is_mapping(&self) -> bool
fn is_mapping(&self) -> bool
Source§fn is_badvalue(&self) -> bool
fn is_badvalue(&self) -> bool
BadValue.Source§fn sequence_mut(&mut self) -> &mut Vec<YamlOwned>
fn sequence_mut(&mut self) -> &mut Vec<YamlOwned>
Source§fn mapping_mut(
&mut self,
) -> &mut LinkedHashMap<<YamlOwned as LoadableYamlNode<'_>>::HashKey, YamlOwned>
fn mapping_mut( &mut self, ) -> &mut LinkedHashMap<<YamlOwned as LoadableYamlNode<'_>>::HashKey, YamlOwned>
Source§fn take(&mut self) -> YamlOwned
fn take(&mut self) -> YamlOwned
Self, leaving a BadValue in its place.