tramlines-core 0.2.1

The format-neutral core of tramlines: the grammar a schema is written in, its shapes, formats and frontmatter rules, and the findings of a check.
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Checks a schema YAML against the grammar's `meta` and `schema`
//! sections generically. Each failure is a finding on the schema.

use super::{Grammar, KeyDef, RegexCache, TypeName};
use crate::{
    Finding, RepoPath, Spanned, Yaml,
    text::{self, is_slug},
};

/// Validate a schema against grammar `schema`.
// sokf-implements: TL_11
// sokf-implements: TL_12
// sokf-implements: CORE-4_AC-1
// sokf-implements: CORE-4_AC-5
// sokf:begin validate
pub fn validate_schema(
    yaml: &Spanned,
    grammar: &Grammar,
    regexes: &RegexCache,
    schema: &RepoPath,
) -> Vec<Finding> {
    // sokf:end validate
    validate_map(yaml, "schema", grammar, regexes, schema, None)
}

/// Validate one instance of rule map `rule` at `yaml`. `kind` is the
/// enclosing block's kind, for `requires: {kind}`.
///
/// # Panics
///
/// When `rule` names no rule map of the grammar: the caller's fault, since
/// the grammar refuses any `of` that names none.
// sokf:begin validate
pub fn validate_map(
    yaml: &Spanned,
    rule: &str,
    grammar: &Grammar,
    regexes: &RegexCache,
    schema: &RepoPath,
    kind: Option<&str>,
) -> Vec<Finding> {
    // sokf:end validate
    let mut v = Validator {
        grammar,
        regexes,
        schema,
        findings: Vec::new(),
    };
    v.map(yaml, rule, kind);
    v.findings
}

struct Validator<'a> {
    grammar: &'a Grammar,
    regexes: &'a RegexCache,
    schema: &'a RepoPath,
    findings: Vec<Finding>,
}

impl Validator<'_> {
    /// A value of the wrong type.
    // sokf-implements: TL_101
    fn mistyped(&mut self, line: usize, message: String) {
        self.fail_on(line, "TL_101", message);
    }

    fn fail_on(&mut self, line: usize, criterion: &str, message: String) {
        self.findings.push(Finding::error(
            self.schema.clone(),
            Some(line),
            criterion,
            message,
        ));
    }

    fn map(&mut self, yaml: &Spanned, rule: &str, kind: Option<&str>) {
        // The grammar refuses an `of` that names no rule map when it is
        // built, so only a caller that names one wrongly reaches this.
        let Some(def) = self.grammar.rule(rule) else {
            panic!("internal error: the grammar has no rule map `{rule}`");
        };
        let Some(entries) = yaml.as_map() else {
            self.mistyped(
                yaml.line,
                format!(
                    "`{rule}` must be a map, not {} {}",
                    text::article(yaml.type_name()),
                    yaml.type_name()
                ),
            );
            return;
        };
        // A format patch states only its difference, so it is checked
        // after it is merged onto the entry it shadows, not here.
        // sokf-implements: TL_40
        if rule == "format"
            && entries
                .iter()
                .any(|(k, v)| k.value == "patch" && v.as_bool() == Some(true))
        {
            return;
        }
        let own_kind = if rule == "block" {
            entries
                .iter()
                .find(|(k, _)| k.value == "kind")
                .and_then(|(_, v)| v.as_str())
                .map(str::to_string)
        } else {
            kind.map(str::to_string)
        };
        let present: Vec<&str> = entries.iter().map(|(k, _)| k.value.as_str()).collect();
        for (key, value) in entries {
            let Some(key_def) = self.grammar.key_def(rule, &key.value) else {
                let hint = if rule == "section" && key.value == "heading-from" {
                    " (`heading-from: <key>` became `heading: { from: <key> }`)"
                } else {
                    ""
                };
                // sokf-implements: TL_12
                self.fail_on(
                    key.line,
                    "TL_12",
                    format!("unknown key `{}` in `{rule}`{hint}", key.value),
                );
                continue;
            };
            self.value(value, &key.value, key_def, own_kind.as_deref());
            for required in &key_def.requires_keys {
                if !present.contains(&required.as_str()) {
                    // sokf-implements: TL_103
                    self.fail_on(
                        key.line,
                        "TL_103",
                        format!("`{}` requires `{required}` beside it", key.value),
                    );
                }
            }
            // sokf-implements: TLMD_42
            if !key_def.requires_kind.is_empty() {
                match own_kind.as_deref() {
                    Some(k) if key_def.requires_kind.iter().any(|r| r == k) => {}
                    Some(k) => self.fail_on(
                        key.line,
                        "TLMD_42",
                        format!(
                            "`{}` does not apply to {} `{k}` block; it needs kind {}",
                            key.value,
                            text::article(k),
                            key_def.requires_kind.join(" or ")
                        ),
                    ),
                    None => {}
                }
            }
        }
        for (name, key_def) in &def.keys {
            if key_def.required && !present.contains(&name.as_str()) {
                self.fail_on(yaml.line, "TL_103", format!("`{rule}` requires `{name}`"));
            }
        }
        if !def.exactly_one_of.is_empty() {
            let n = def
                .exactly_one_of
                .iter()
                .filter(|k| present.contains(&k.as_str()))
                .count();
            // A group of shapes is the one-shape rule; any other group is
            // the grammar's. None of a group is a key missing, two of it a
            // key too many.
            // sokf-implements: CORE-4_AC-3
            // sokf-implements: TL_22
            // sokf-implements: TL_104
            // sokf-implements: TL_105
            let shapes = def
                .exactly_one_of
                .iter()
                .any(|k| matches!(k.as_str(), "text" | "template" | "pattern"));
            // sokf-implements: CORE-4_AC-4
            let criterion = match (n, shapes) {
                (0, true) => "TL_105",
                (0, false) => "TL_103",
                (_, true) => "TL_22",
                (_, false) => "TL_104",
            };
            if n != 1 {
                self.fail_on(
                    yaml.line,
                    criterion,
                    format!(
                        "`{rule}` needs exactly one of {}",
                        list(&def.exactly_one_of)
                    ),
                );
            }
        }
        if !def.at_least_one_of.is_empty()
            && !def
                .at_least_one_of
                .iter()
                .any(|k| present.contains(&k.as_str()))
        {
            self.fail_on(
                yaml.line,
                "TL_103",
                format!(
                    "`{rule}` needs at least one of {}",
                    list(&def.at_least_one_of)
                ),
            );
        }
    }

    fn value(&mut self, value: &Spanned, key: &str, def: &KeyDef, kind: Option<&str>) {
        let of = def.of.as_deref();
        match def.type_ {
            TypeName::String => self.scalar(value, key, def),
            TypeName::Integer => {
                if value.as_int().is_none() {
                    self.mistyped(value.line, format!("`{key}` must be an integer"));
                } else {
                    self.enum_check(value, key, def);
                }
            }
            TypeName::Boolean => {
                if value.as_bool().is_none() {
                    self.mistyped(value.line, format!("`{key}` must be true or false"));
                }
            }
            TypeName::List => match value.as_seq() {
                Some(items) => {
                    for item in items {
                        self.item(item, key, of, def, kind);
                    }
                }
                None => self.mistyped(value.line, format!("`{key}` must be a list")),
            },
            TypeName::Map => match value.as_map() {
                Some(entries) => {
                    for (_, item) in entries {
                        self.item(item, key, of, def, kind);
                    }
                }
                None => self.mistyped(value.line, format!("`{key}` must be a map")),
            },
            TypeName::Object => self.item(value, key, of, def, kind),
            TypeName::StringOrList => match &value.value {
                Yaml::Str(_) => self.scalar(value, key, def),
                // Each item is held to the key's format and enum as the
                // single string is.
                Yaml::Seq(items) => {
                    for item in items {
                        match item.as_str() {
                            Some(_) => self.scalar(item, key, def),
                            None => {
                                self.mistyped(item.line, format!("`{key}` items must be strings"));
                            }
                        }
                    }
                }
                _ => self.mistyped(value.line, format!("`{key}` must be a string or a list")),
            },
            TypeName::Text => match &value.value {
                Yaml::Str(_) => self.scalar(value, key, def),
                Yaml::Map(entries) if is_from(entries) => {}
                _ => self.mistyped(
                    value.line,
                    format!("`{key}` must be a string or `{{ from: <frontmatter key> }}`"),
                ),
            },
            TypeName::TextOrRule => match &value.value {
                Yaml::Str(_) => self.scalar(value, key, def),
                Yaml::Map(entries) if is_from(entries) => {}
                Yaml::Map(_) => self.item(value, key, of, def, kind),
                _ => self.mistyped(
                    value.line,
                    format!(
                        "`{key}` must be a string, `{{ from: <frontmatter key> }}` or a `{}` map",
                        of.unwrap_or("rule")
                    ),
                ),
            },
            TypeName::StringOrMap => match &value.value {
                Yaml::Str(_) => self.scalar(value, key, def),
                Yaml::Map(_) => {}
                _ => self.mistyped(value.line, format!("`{key}` must be a string or a map")),
            },
        }
    }

    /// One item of a list, value of a map, or instance of an object.
    fn item(
        &mut self,
        item: &Spanned,
        key: &str,
        of: Option<&str>,
        def: &KeyDef,
        kind: Option<&str>,
    ) {
        match of {
            None => {}
            // An item is held to the key's format and enum as a scalar
            // is, so a list of regexes reports the one that does not parse.
            // sokf-implements: TL_17
            Some("string") => match item.as_str() {
                Some(_) => self.scalar(item, key, def),
                None => self.mistyped(item.line, format!("`{key}` items must be strings")),
            },
            Some("integer") => {
                if item.as_int().is_none() {
                    self.mistyped(item.line, format!("`{key}` items must be integers"));
                }
            }
            Some("boolean") => {
                if item.as_bool().is_none() {
                    self.mistyped(item.line, format!("`{key}` items must be booleans"));
                }
            }
            Some("string-or-list") => {
                if item.as_str_or_list().is_none() {
                    self.mistyped(
                        item.line,
                        format!("`{key}` values must be strings or lists of strings"),
                    );
                }
            }
            Some(name) if self.grammar.rule(name).is_some() => {
                let inner_kind = if name == "block" { None } else { kind };
                self.map(item, name, inner_kind);
            }
            // The grammar refuses an `of` that names nothing when it is
            // built.
            Some(other) => {
                unreachable!("the grammar names an unknown shape `{other}` for `{key}`");
            }
        }
    }

    fn scalar(&mut self, value: &Spanned, key: &str, def: &KeyDef) {
        let Some(text) = value.as_str() else {
            self.mistyped(
                value.line,
                format!(
                    "`{key}` must be a string, not {} {}",
                    text::article(value.type_name()),
                    value.type_name()
                ),
            );
            return;
        };
        self.enum_check(value, key, def);
        match def.format.as_deref() {
            Some("regex") => {
                if let Err(e) = self.regexes.get(text) {
                    // sokf-implements: CORE-4_AC-2
                    // sokf-implements: TL_118
                    self.fail_on(
                        value.line,
                        "TL_118",
                        format!("`{key}` is not a valid regex: {e}"),
                    );
                }
            }
            Some("slug") if !is_slug(text) => {
                // sokf-implements: TL_102
                self.fail_on(
                    value.line,
                    "TL_102",
                    format!("`{key}` must be a slug: `{text}` is not"),
                );
            }
            // References resolve when the schema is lowered.
            _ => {}
        }
    }

    fn enum_check(&mut self, value: &Spanned, key: &str, def: &KeyDef) {
        if def.enum_.is_empty() {
            return;
        }
        let text = value.scalar_text().unwrap_or_default();
        if !def.enum_.contains(&text) {
            self.fail_on(
                value.line,
                "TL_102",
                format!(
                    "`{key}` must be one of {}; `{text}` is not",
                    list(&def.enum_)
                ),
            );
        }
    }
}

/// Whether a map is `{ from: <key> }`, which binds a text to a frontmatter
/// key.
fn is_from(entries: &[(Spanned<String>, Spanned)]) -> bool {
    matches!(entries, [(k, v)] if k.value == "from" && v.as_str().is_some())
}

fn list(items: &[String]) -> String {
    text::code_list(items, ", ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{grammar::load_core_grammar, yaml};

    fn check(text: &str) -> Vec<String> {
        let grammar = load_core_grammar().unwrap();
        let yaml = yaml::parse(text, 1).unwrap();
        validate_schema(&yaml, &grammar, &RegexCache::new(), &RepoPath::root())
            .into_iter()
            .map(|f| f.message)
            .collect()
    }

    // sokf-tests: TL_11
    // sokf-tests: TL_12
    // sokf-tests: TL_102
    // sokf-tests: TL_103
    #[test]
    fn the_core_keys_are_checked_against_the_core_grammar() {
        assert!(check("line-limit: 0\nfrontmatter:\n  title:\n    example: A\n").is_empty());
        let out = check(
            "frontmatter:\n  title:\n    format: nope\n    list: 1\nlifecycle:\n  - key: Not Slug\n    states: [a]\nsections: []\n",
        );
        assert!(
            out.iter().any(|m| m.contains("requires `line-limit`")),
            "{out:?}"
        );
        assert!(
            out.iter().any(|m| m.contains("unknown key `sections`")),
            "{out:?}"
        );
        assert!(
            out.iter().any(|m| m.contains("`format` must be one of")),
            "{out:?}"
        );
        assert!(out.iter().any(|m| m.contains("must be a slug")), "{out:?}");
    }
}