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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::sync::Arc;

use anyhow::Context;

use super::{ConfigSource, Hierarchical};
use crate::value::{FileFormat, Map, Pointer, Value, ValueOrigin, WithOrigin};

/// YAML-based configuration source.
#[derive(Debug, Clone)]
pub struct Yaml {
    origin: Arc<ValueOrigin>,
    inner: Map,
}

impl Yaml {
    /// Creates a source with the specified name and contents.
    ///
    /// # Errors
    ///
    /// Returns an error if the input doesn't conform to the JSON object model; e.g., if it has objects / maps
    /// with array or object keys.
    pub fn new(filename: &str, object: serde_yaml::Mapping) -> anyhow::Result<Self> {
        let origin = Arc::new(ValueOrigin::File {
            name: filename.to_owned(),
            format: FileFormat::Yaml,
        });
        let inner =
            Self::map_value(serde_yaml::Value::Mapping(object), &origin, String::new())?.inner;
        let Value::Object(inner) = inner else {
            unreachable!();
        };
        Ok(Self { origin, inner })
    }

    fn map_key(key: serde_yaml::Value, parent_path: &str) -> anyhow::Result<String> {
        Ok(match key {
            serde_yaml::Value::String(value) => value,
            serde_yaml::Value::Number(value) => value.to_string(),
            serde_yaml::Value::Bool(value) => value.to_string(),
            serde_yaml::Value::Null => "null".into(),
            _ => anyhow::bail!("unsupported key type at {parent_path:?}: {key:?}; only primitive value types are supported as keys"),
        })
    }

    fn map_number(number: &serde_yaml::Number, path: &str) -> anyhow::Result<serde_json::Number> {
        Ok(if let Some(number) = number.as_u64() {
            number.into()
        } else if let Some(number) = number.as_i64() {
            number.into()
        } else if let Some(number) = number.as_f64() {
            serde_json::Number::from_f64(number)
                .with_context(|| format!("unsupported number at {path:?}: {number:?}"))?
        } else {
            anyhow::bail!("unsupported number at {path:?}: {number:?}")
        })
    }

    fn map_value(
        value: serde_yaml::Value,
        file_origin: &Arc<ValueOrigin>,
        path: String,
    ) -> anyhow::Result<WithOrigin> {
        let inner = match value {
            serde_yaml::Value::Null => Value::Null,
            serde_yaml::Value::Bool(value) => value.into(),
            serde_yaml::Value::Number(value) => Value::Number(Self::map_number(&value, &path)?),
            serde_yaml::Value::String(value) => value.into(),
            serde_yaml::Value::Sequence(items) => Value::Array(
                items
                    .into_iter()
                    .enumerate()
                    .map(|(i, value)| {
                        let child_path = Pointer(&path).join(&i.to_string());
                        Self::map_value(value, file_origin, child_path)
                    })
                    .collect::<anyhow::Result<_>>()?,
            ),
            serde_yaml::Value::Mapping(items) => Value::Object(
                items
                    .into_iter()
                    .map(|(key, value)| {
                        let key = Self::map_key(key, &path)?;
                        let child_path = Pointer(&path).join(&key);
                        anyhow::Ok((key, Self::map_value(value, file_origin, child_path)?))
                    })
                    .collect::<anyhow::Result<_>>()?,
            ),
            serde_yaml::Value::Tagged(tagged) => {
                return Self::map_value(tagged.value, file_origin, path);
            }
        };

        Ok(WithOrigin {
            inner,
            origin: if path.is_empty() {
                file_origin.clone()
            } else {
                Arc::new(ValueOrigin::Path {
                    source: file_origin.clone(),
                    path,
                })
            },
        })
    }
}

impl ConfigSource for Yaml {
    type Kind = Hierarchical;

    fn into_contents(self) -> WithOrigin<Map> {
        WithOrigin::new(self.inner, self.origin)
    }
}

#[cfg(test)]
mod tests {
    use assert_matches::assert_matches;

    use super::*;
    use crate::value::StrValue;

    const YAML_CONFIG: &str = r#"
bool: true
nested:
    int: 123
    string: "what?"
array:
    - test: 23
    "#;

    fn filename(source: &ValueOrigin) -> &str {
        if let ValueOrigin::File {
            name,
            format: FileFormat::Yaml,
        } = source
        {
            name
        } else {
            panic!("unexpected source: {source:?}");
        }
    }

    #[test]
    fn creating_yaml_config() {
        let yaml: serde_yaml::Value = serde_yaml::from_str(YAML_CONFIG).unwrap();
        let serde_yaml::Value::Mapping(yaml) = yaml else {
            unreachable!();
        };
        let yaml = Yaml::new("test.yml", yaml).unwrap();

        assert_matches!(yaml.inner["bool"].inner, Value::Bool(true));
        assert_matches!(
            yaml.inner["bool"].origin.as_ref(),
            ValueOrigin::Path { path, source } if filename(source) == "test.yml" && path == "bool"
        );

        let str = yaml.inner["nested"].get(Pointer("string")).unwrap();
        assert_matches!(&str.inner, Value::String(StrValue::Plain(s)) if s == "what?");
        assert_matches!(
            str.origin.as_ref(),
            ValueOrigin::Path { path, source } if filename(source) == "test.yml" && path == "nested.string"
        );

        let inner_int = yaml.inner["array"].get(Pointer("0.test")).unwrap();
        assert_matches!(&inner_int.inner, Value::Number(num) if *num == 23_u64.into());
    }

    #[test]
    fn unsupported_key() {
        let yaml = r"
array:
    - [12, 34]: bogus
        ";
        let yaml: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
        let serde_yaml::Value::Mapping(yaml) = yaml else {
            unreachable!();
        };

        let err = Yaml::new("test.yml", yaml).unwrap_err().to_string();
        assert!(err.contains("unsupported key type"), "{err}");
        assert!(err.contains("array.0"), "{err}");
    }
}