dre-core 0.8.0

DRE core engine: project parsing, validation and the run path
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! A parsed YAML document as a tree of nodes, each with the line and column it was written at.
//!
//! YAML is parsed by `serde-saphyr`, YAML 1.2 with strict booleans: only `true` and `false` are
//! booleans, so `yes`, `no`, `on` and `off` stay strings. Duplicate keys are errors; merge keys
//! (`<<: *base`) are expanded. Typed config is then read from the tree (see [`super::de`]), so every
//! diagnostic can point at the exact line of the key or value it's about.

use std::fmt;

use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use serde_saphyr::Spanned;

/// A YAML value and where it was written (1-based; 0 when unknown).
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
    pub kind: Kind,
    pub line: usize,
    pub column: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Kind {
    Null,
    Bool(bool),
    Int(i64),
    UInt(u64),
    Float(f64),
    Str(String),
    Seq(Vec<Node>),
    /// In the file's order. Keys are strings: a scalar key (`2024:`) keeps its spelling.
    Map(Vec<(Key, Node)>),
}

/// A map key and its line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Key {
    pub name: String,
    pub line: usize,
}

/// A YAML syntax error, with its line when known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyntaxError {
    pub line: Option<usize>,
    pub message: String,
}

impl Kind {
    /// How a value of this kind is named in diagnostics.
    pub fn name(&self) -> &'static str {
        match self {
            Kind::Null => "nothing",
            Kind::Bool(_) => "a boolean",
            Kind::Int(_) | Kind::UInt(_) | Kind::Float(_) => "a number",
            Kind::Str(_) => "a string",
            Kind::Seq(_) => "a list",
            Kind::Map(_) => "a map",
        }
    }
}

impl Node {
    pub fn null() -> Node {
        Node {
            kind: Kind::Null,
            line: 0,
            column: 0,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match &self.kind {
            Kind::Str(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_map(&self) -> Option<&[(Key, Node)]> {
        match &self.kind {
            Kind::Map(m) => Some(m),
            _ => None,
        }
    }

    /// A key's value in a map node.
    pub fn get(&self, key: &str) -> Option<&Node> {
        self.as_map()?.iter().find(|(k, _)| k.name == key).map(|(_, v)| v)
    }

    /// A key's entry (key and value) in a map node.
    pub fn entry(&self, key: &str) -> Option<&(Key, Node)> {
        self.as_map()?.iter().find(|(k, _)| k.name == key)
    }

    /// The node as JSON, for values handed on unvalidated (a plugin's fields). Keys are strings;
    /// numbers that don't fit JSON's become strings.
    pub fn to_json(&self) -> serde_json::Value {
        use serde_json::Value as J;
        match &self.kind {
            Kind::Null => J::Null,
            Kind::Bool(b) => J::Bool(*b),
            Kind::Int(i) => J::from(*i),
            Kind::UInt(u) => J::from(*u),
            Kind::Float(f) => {
                serde_json::Number::from_f64(*f).map_or_else(|| J::String(f.to_string()), J::Number)
            }
            Kind::Str(s) => J::String(s.clone()),
            Kind::Seq(s) => J::Array(s.iter().map(Node::to_json).collect()),
            Kind::Map(m) => J::Object(m.iter().map(|(k, v)| (k.name.clone(), v.to_json())).collect()),
        }
    }
}

/// Parse a YAML document. An empty document is a null node.
pub fn parse(text: &str) -> Result<Node, SyntaxError> {
    let mut options = serde_saphyr::options::Options::default();
    options.strict_booleans = true;
    options.duplicate_keys = serde_saphyr::options::DuplicateKeyPolicy::Error;
    options.with_snippet = false;
    // `.inf`, `.nan` and out-of-range floats like `1e999` reach DRE as the strings they are; a
    // plugin decides what they mean.
    options.non_finite_float_policy = serde_saphyr::options::NonFiniteFloatPolicy::AsString;
    if text.trim().is_empty() {
        return Ok(Node::null());
    }
    match serde_saphyr::from_str_with_options::<Spanned<Raw>>(text, options) {
        Ok(raw) => {
            let mut node: Node = raw.into();
            // Top-level `x-*` keys (the docker-compose convention) hold YAML anchors to reuse
            // with aliases and `<<:` merges; once those are resolved, DRE ignores them.
            if let Kind::Map(m) = &mut node.kind {
                m.retain(|(k, _)| !k.name.starts_with("x-"));
            }
            Ok(node)
        }
        Err(e) => Err(syntax_error(&e)),
    }
}

fn syntax_error(e: &serde_saphyr::Error) -> SyntaxError {
    static LOCATION: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
        regex::Regex::new(r"^(?:error: )?(?:line \d+ column \d+: )?(.*?)(?: at line \d+, column \d+)?$")
            .unwrap()
    });
    let line = e.location().map(|l| l.line() as usize).filter(|l| *l > 0);
    let text = e.to_string();
    let first = text.lines().next().unwrap_or_default();
    // DRE prints the line itself, and serde-saphyr's hints name its own options and functions.
    let message = LOCATION
        .captures(first)
        .map_or(first, |c| c.get(1).map_or(first, |m| m.as_str()));
    let message = message.replace(", set DuplicateKeyPolicy in Options if acceptable", "");
    let message = message
        .split("; use from_")
        .next()
        .unwrap_or_default()
        .to_string();
    let message = if message.contains("merge value must be") {
        "invalid YAML merge key (`<<`): `<<` must be a map or a list of maps".to_string()
    } else {
        message
    };
    SyntaxError { line, message }
}

/// The tree as serde-saphyr hands it over: every value with its location.
enum Raw {
    Null,
    Bool(bool),
    Int(i64),
    UInt(u64),
    Float(f64),
    Str(String),
    Seq(Vec<Spanned<Raw>>),
    Map(Vec<(Spanned<KeyRaw>, Spanned<Raw>)>),
}

/// A map key as written: a scalar, kept as its spelling (`null`, `true`, `2024`).
struct KeyRaw(String);

impl From<Spanned<Raw>> for Node {
    fn from(s: Spanned<Raw>) -> Node {
        let kind = match s.value {
            Raw::Null => Kind::Null,
            Raw::Bool(b) => Kind::Bool(b),
            Raw::Int(i) => Kind::Int(i),
            Raw::UInt(u) => Kind::UInt(u),
            Raw::Float(f) => Kind::Float(f),
            Raw::Str(t) => Kind::Str(t),
            Raw::Seq(items) => Kind::Seq(items.into_iter().map(Node::from).collect()),
            Raw::Map(entries) => Kind::Map(
                entries
                    .into_iter()
                    .map(|(k, v)| {
                        let key = Key {
                            name: k.value.0,
                            line: k.referenced.line() as usize,
                        };
                        (key, Node::from(v))
                    })
                    .collect(),
            ),
        };
        Node {
            kind,
            line: s.referenced.line() as usize,
            column: s.referenced.column() as usize,
        }
    }
}

impl<'de> Deserialize<'de> for Raw {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct V;
        impl<'de> Visitor<'de> for V {
            type Value = Raw;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("a YAML value")
            }
            fn visit_unit<E>(self) -> Result<Raw, E> {
                Ok(Raw::Null)
            }
            fn visit_none<E>(self) -> Result<Raw, E> {
                Ok(Raw::Null)
            }
            fn visit_some<D: Deserializer<'de>>(self, d: D) -> Result<Raw, D::Error> {
                Raw::deserialize(d)
            }
            fn visit_bool<E>(self, v: bool) -> Result<Raw, E> {
                Ok(Raw::Bool(v))
            }
            fn visit_i64<E>(self, v: i64) -> Result<Raw, E> {
                Ok(Raw::Int(v))
            }
            fn visit_u64<E>(self, v: u64) -> Result<Raw, E> {
                Ok(i64::try_from(v).map_or(Raw::UInt(v), Raw::Int))
            }
            fn visit_f64<E>(self, v: f64) -> Result<Raw, E> {
                Ok(Raw::Float(v))
            }
            fn visit_str<E>(self, v: &str) -> Result<Raw, E> {
                Ok(Raw::Str(v.to_string()))
            }
            fn visit_string<E>(self, v: String) -> Result<Raw, E> {
                Ok(Raw::Str(v))
            }
            fn visit_seq<A: SeqAccess<'de>>(self, mut a: A) -> Result<Raw, A::Error> {
                let mut items = Vec::new();
                while let Some(x) = a.next_element()? {
                    items.push(x);
                }
                Ok(Raw::Seq(items))
            }
            fn visit_map<A: MapAccess<'de>>(self, mut a: A) -> Result<Raw, A::Error> {
                let mut entries = Vec::new();
                while let Some(e) = a.next_entry()? {
                    entries.push(e);
                }
                Ok(Raw::Map(entries))
            }
        }
        d.deserialize_any(V)
    }
}

/// The message for a key that's a list or a map.
pub const COMPLEX_KEY: &str = "a map key is a list or a map; keys must be names";

impl<'de> Deserialize<'de> for KeyRaw {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct V;
        impl<'de> Visitor<'de> for V {
            type Value = KeyRaw;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("a key")
            }
            fn visit_unit<E>(self) -> Result<KeyRaw, E> {
                Ok(KeyRaw("null".into()))
            }
            fn visit_none<E>(self) -> Result<KeyRaw, E> {
                Ok(KeyRaw("null".into()))
            }
            fn visit_bool<E>(self, v: bool) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v.to_string()))
            }
            fn visit_i64<E>(self, v: i64) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v.to_string()))
            }
            fn visit_u64<E>(self, v: u64) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v.to_string()))
            }
            fn visit_f64<E>(self, v: f64) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v.to_string()))
            }
            fn visit_str<E>(self, v: &str) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v.to_string()))
            }
            fn visit_string<E>(self, v: String) -> Result<KeyRaw, E> {
                Ok(KeyRaw(v))
            }
            fn visit_seq<A: SeqAccess<'de>>(self, _: A) -> Result<KeyRaw, A::Error> {
                Err(serde::de::Error::custom(COMPLEX_KEY))
            }
            fn visit_map<A: MapAccess<'de>>(self, _: A) -> Result<KeyRaw, A::Error> {
                Err(serde::de::Error::custom(COMPLEX_KEY))
            }
        }
        d.deserialize_any(V)
    }
}

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

    #[test]
    fn values_keep_their_lines() {
        let n = parse("a:\n  b: 1\n  c: [x, y]\n").unwrap();
        let a = n.get("a").unwrap();
        assert_eq!(n.entry("a").unwrap().0.line, 1);
        assert_eq!(a.entry("c").unwrap().0.line, 3);
        assert_eq!(a.get("b").unwrap().kind, Kind::Int(1));
        assert_eq!(a.get("b").unwrap().line, 2);
    }

    #[test]
    fn only_true_and_false_are_booleans() {
        let n = parse("a: yes\nb: no\nc: on\nd: true\n").unwrap();
        assert_eq!(n.get("a").unwrap().as_str(), Some("yes"));
        assert_eq!(n.get("c").unwrap().as_str(), Some("on"));
        assert_eq!(n.get("d").unwrap().kind, Kind::Bool(true));
    }

    #[test]
    fn merge_keys_are_expanded_and_written_keys_win() {
        let n = parse("base: &b {type: duckdb, path: x}\ndev: {<<: *b, path: y}\n").unwrap();
        let dev = n.get("dev").unwrap();
        assert_eq!(dev.get("type").unwrap().as_str(), Some("duckdb"));
        assert_eq!(dev.get("path").unwrap().as_str(), Some("y"));
    }

    #[test]
    fn duplicate_keys_are_errors_with_their_line() {
        let e = parse("a: 1\na: 2\n").unwrap_err();
        assert_eq!(e.line, Some(2));
        assert_eq!(e.message, "duplicate mapping key: a");
    }

    #[test]
    fn syntax_errors_have_a_line() {
        let e = parse("a:\n  b: [1\n").unwrap_err();
        assert_eq!(e.line, Some(2));
        assert!(!e.message.starts_with("line"), "{}", e.message);
    }

    #[test]
    fn scalar_keys_are_strings_and_empty_files_are_null() {
        assert_eq!(
            parse("2024: x\n").unwrap().get("2024").unwrap().as_str(),
            Some("x")
        );
        assert_eq!(parse("\n# nothing\n").unwrap().kind, Kind::Null);
    }

    #[test]
    fn merged_keys_report_the_merge_line() {
        let n = parse("base: &b\n  x: 1\ndev:\n  <<: *b\n  z: 3\n").unwrap();
        let dev = n.get("dev").unwrap();
        assert_eq!(dev.entry("x").unwrap().0.line, 4);
        assert_eq!(dev.entry("z").unwrap().0.line, 5);
    }

    #[test]
    fn multiple_documents_name_no_library_functions() {
        let e = parse("a: 1\n---\nb: 2\n").unwrap_err();
        assert_eq!(e.line, Some(3));
        assert_eq!(e.message, "multiple YAML documents detected");
    }

    #[test]
    fn non_finite_floats_are_strings() {
        let j = parse("a: .inf\nb: -.inf\nc: .nan\nd: 1e999\n").unwrap().to_json();
        assert_eq!(
            j,
            serde_json::json!({"a": ".inf", "b": "-.inf", "c": ".nan", "d": ".inf"})
        );
    }

    #[test]
    fn relaxed_yaml_forms_parse() {
        let quoted = parse("a: \"one\n two\"\n").unwrap();
        assert_eq!(quoted.get("a").unwrap().as_str(), Some("one two"));
        assert_eq!(parse("?\tk\n").unwrap().to_json(), serde_json::json!({"k": null}));
        assert!(parse("?").is_ok());
        let block = parse("s: |\n  a\n  ").unwrap();
        assert_eq!(block.get("s").unwrap().as_str(), Some("a\n"));
    }

    #[test]
    fn jinja_in_quoted_values_is_a_string() {
        let n = parse("pw: \"{{ env_var('X') }}\"\n").unwrap();
        assert_eq!(n.get("pw").unwrap().as_str(), Some("{{ env_var('X') }}"));
    }
}

#[cfg(test)]
mod x_key_tests {
    use super::*;

    #[test]
    fn top_level_x_keys_hold_anchors_and_are_dropped() {
        let n = parse("x-dest: &dest {profile: s3, path: a.csv}\noutput:\n  destination:\n    <<: *dest\n    path: b.csv\n").unwrap();
        let j = n.to_json();
        assert!(j.get("x-dest").is_none(), "{j}");
        assert_eq!(
            j["output"]["destination"],
            serde_json::json!({"profile": "s3", "path": "b.csv"})
        );
    }
}