introduce or function

Similarly to `or` for Rust's options, this patch provides a way to
'override' the value of a Yaml node if it's some form of error.
This commit is contained in:
lincoln auster [they/them] 2021-09-28 23:06:37 -06:00 committed by Ethiraric
parent 38aaea20db
commit 5ba5dfa6e6

View file

@ -383,6 +383,23 @@ impl Yaml {
pub fn into_f64(self) -> Option<f64> { pub fn into_f64(self) -> Option<f64> {
self.as_f64() self.as_f64()
} }
/// 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));
/// ```
#[must_use]
pub fn or(self, other: Self) -> Self {
match self {
Yaml::BadValue | Yaml::Null => other,
this => this,
}
}
} }
#[cfg_attr(feature = "cargo-clippy", allow(clippy::should_implement_trait))] #[cfg_attr(feature = "cargo-clippy", allow(clippy::should_implement_trait))]
@ -490,7 +507,7 @@ impl Iterator for YamlIter {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::YamlDecoder; use super::{Yaml, YamlDecoder};
#[test] #[test]
fn test_read_bom() { fn test_read_bom() {
@ -573,4 +590,10 @@ c: [1, 2]
assert_eq!(doc["c"][1].as_i64().unwrap(), 2i64); assert_eq!(doc["c"][1].as_i64().unwrap(), 2i64);
assert!(doc["d"][0].is_badvalue()); assert!(doc["d"][0].is_badvalue());
} }
#[test]
fn test_or() {
assert_eq!(Yaml::Null.or(Yaml::Integer(3)), Yaml::Integer(3));
assert_eq!(Yaml::Integer(3).or(Yaml::Integer(7)), Yaml::Integer(3));
}
} }