1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Trait implementations that allow the solver to be gneric over serde_yaml's `Value`.

use std::borrow::Cow;

pub use serde_yaml::{Mapping, Number, Value as Yaml};

use crate::value::{AsValue, Object, Value};

impl AsValue for Yaml {
    #[inline]
    fn as_value(&self) -> Value<'_> {
        match self {
            Self::Null => Value::Null,
            Self::String(s) => Value::String(Cow::Borrowed(s)),
            Self::Number(n) => {
                if n.is_u64() {
                    Value::UInt(n.as_u64().unwrap())
                } else if n.is_i64() {
                    Value::Int(n.as_i64().unwrap())
                } else if n.is_f64() {
                    Value::Float(n.as_f64().unwrap())
                } else {
                    unreachable!()
                }
            }
            Self::Bool(b) => Value::Bool(*b),
            Self::Mapping(o) => Value::Object(o),
            Self::Sequence(s) => Value::Array(s),
            Self::Tagged(t) => t.value.as_value(),
        }
    }
}

impl Object for Mapping {
    #[inline]
    fn get(&self, key: &str) -> Option<Value<'_>> {
        self.get(&Yaml::String(key.to_string()))
            .map(|v| v.as_value())
    }

    #[inline]
    fn keys(&self) -> Vec<Cow<'_, str>> {
        self.iter()
            .filter_map(|(k, _)| k.as_value().to_string().map(Cow::Owned))
            .collect()
    }

    #[inline]
    fn len(&self) -> usize {
        self.len()
    }
}