tau_engine/
yaml.rs

1// Trait implementations that allow the solver to be gneric over serde_yaml's `Value`.
2
3use std::borrow::Cow;
4
5pub use serde_yaml::{Mapping, Value as Yaml};
6
7use crate::value::{AsValue, Object, Value};
8
9impl AsValue for Yaml {
10    #[inline]
11    fn as_value(&self) -> Value<'_> {
12        match self {
13            Self::Null => Value::Null,
14            Self::String(s) => Value::String(Cow::Borrowed(s)),
15            Self::Number(n) => {
16                if n.is_u64() {
17                    Value::UInt(n.as_u64().unwrap())
18                } else if n.is_i64() {
19                    Value::Int(n.as_i64().unwrap())
20                } else if n.is_f64() {
21                    Value::Float(n.as_f64().unwrap())
22                } else {
23                    unreachable!()
24                }
25            }
26            Self::Bool(b) => Value::Bool(*b),
27            Self::Mapping(o) => Value::Object(o),
28            Self::Sequence(s) => Value::Array(s),
29            Self::Tagged(t) => t.value.as_value(),
30        }
31    }
32}
33
34impl Object for Mapping {
35    #[inline]
36    fn get(&self, key: &str) -> Option<Value<'_>> {
37        self.get(Yaml::String(key.to_string()))
38            .map(|v| v.as_value())
39    }
40
41    #[inline]
42    fn keys(&self) -> Vec<Cow<'_, str>> {
43        self.iter()
44            .filter_map(|(k, _)| k.as_value().to_string().map(Cow::Owned))
45            .collect()
46    }
47
48    #[inline]
49    fn len(&self) -> usize {
50        self.len()
51    }
52}