tramlines-core 0.5.0

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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! The lowering of the keys every format shares: a typed [`CoreSchema`]
//! from a schema YAML that passed the generic check. A format crate wraps
//! [`Lowerer`] and lowers its own keys beside these.

use std::collections::{BTreeMap, HashSet};

use regex::Regex;

use super::{
    Condition, CoreSchema, DerivationSource, Example, FormatRegistry, FormatSource, Grammar,
    KeyDerivation, LifecycleDimension, Limit, LimitKind, Only, RegexCache, RuleCommon,
    TypeRegistry,
};
use crate::{Finding, Problem, RepoPath, Severity, Spanned, Yaml};

// sokf-implements: CORE-6_AC-1
// sokf:begin lowering
/// The keys of `schema` the core grammar lowers.
pub const CORE_SCHEMA_KEYS: &[&str] = &[
    "line-limit",
    "guidance",
    "language",
    "lifecycle",
    "frontmatter",
    "frontmatter-open",
    "prohibited-patterns",
    "formats",
    "types",
    "examples",
];
// sokf:end lowering

/// The keys of `lifecycle-dimension` the engine lowers.
const DIMENSION_KEYS: &[&str] = &["key", "states", "guidance"];

/// The entries of `node` whose keys `known` does not name, as written.
// sokf-implements: CORE-3_AC-3
pub fn extensions(node: &Spanned, known: &[&str]) -> BTreeMap<String, Spanned> {
    node.as_map()
        .unwrap_or(&[])
        .iter()
        .filter(|(k, _)| !known.contains(&k.value.as_str()))
        .map(|(k, v)| (k.value.clone(), v.clone()))
        .collect()
}

/// The registry a schema sees: its own `formats` and `types` as the
/// nearest layer on `registry`. A refusal is added to `findings`, and the
/// schema then sees `registry` alone. A format of `registry` that a type
/// of the schema breaks is left out, and reported at the line of the type.
// sokf-implements: TL_34
// sokf-implements: TL_36
// sokf-implements: CORE-6_AC-5
// sokf:begin lowering
pub fn lower_formats(
    yaml: &Spanned,
    grammar: &Grammar,
    registry: &FormatRegistry,
    regexes: &RegexCache,
    schema: &RepoPath,
    schema_id: &str,
    findings: &mut Vec<Finding>,
) -> FormatRegistry {
    // sokf:end lowering
    if yaml.get("formats").is_none() && yaml.get("types").is_none() {
        return registry.clone();
    }
    let none = Spanned::new(Yaml::Map(vec![]), yaml.line);
    // What loads is kept, so a type or a format that fails is reported
    // once, and not again by each rule that uses it.
    let (own, refused) = FormatRegistry::layer(
        yaml.get("formats").unwrap_or(&none),
        yaml.get("types"),
        FormatSource::Schema(schema_id.to_string()),
        grammar,
        regexes,
        schema,
        Some(registry),
    );
    // sokf-implements: CORE-6_AC-6
    findings.extend(refused);
    let (seen, broken) = registry.layered(&own);
    let types = yaml.get("types").and_then(Spanned::as_map).unwrap_or(&[]);
    // A format that the schema declares and that did not load has its own
    // finding, which a finding on the farther format of that name would
    // repeat.
    let declared = |name: &str| yaml.get("formats").and_then(|f| f.get(name)).is_some();
    for b in broken.iter().filter(|b| !declared(&b.name)) {
        let line = types
            .iter()
            .find(|(name, _)| name.value == b.types[0])
            .map_or(yaml.line, |(name, _)| name.line);
        // sokf-implements: TL_140
        // sokf-implements: CORE-6_AC-15
        findings.extend(b.findings(schema, Some(line)));
    }
    seen
}

/// The keys every rule map accepts, `severity` and `only`, lowered for a
/// rule map a host's extension adds: `only` resolves against the schema's
/// lifecycle.
// sokf-implements: CORE-6_AC-7
// sokf:begin lowering
pub fn lower_common(
    node: &Spanned,
    schema: &CoreSchema,
    path: &RepoPath,
) -> (RuleCommon, Vec<Finding>) {
    // sokf:end lowering
    let regexes = RegexCache::new();
    let mut l = Lowerer::new(&regexes, path, TypeRegistry::default());
    l.lifecycle.clone_from(&schema.lifecycle);
    let common = l.common(node);
    (common, l.findings)
}

// sokf:begin lowerer
/// The state of one lowering: the schema it reports on, what it found,
/// and what it has read so far that later keys resolve against.
pub struct Lowerer<'a> {
    /// The cache the schema's regexes compile through.
    pub regexes: &'a RegexCache,
    /// The schema that every finding is reported on.
    pub schema: &'a RepoPath,
    /// The findings so far.
    pub findings: Vec<Finding>,
    /// The `rule-id`s declared so far.
    pub rule_ids: HashSet<String>,
    /// The lifecycle, which each condition resolves against.
    pub lifecycle: Vec<LifecycleDimension>,
    /// The types a template of this schema is written with.
    pub types: TypeRegistry,
}
// sokf:end lowerer

/// The string at `key` of `node`.
pub fn str_of(node: &Spanned, key: &str) -> Option<String> {
    node.get(key).and_then(|v| v.as_str()).map(str::to_string)
}

/// A grammar `key-derivation`: a rule's keys or a frontmatter value.
pub fn derivation(node: &Spanned) -> KeyDerivation {
    let source = match str_of(node, "from") {
        Some(key) => DerivationSource::From(key),
        None => DerivationSource::Rule(str_of(node, "rule").unwrap_or_default()),
    };
    KeyDerivation {
        source,
        separator: str_of(node, "separator").unwrap_or_default(),
    }
}

/// The boolean at `key` of `node`; absent means false.
pub fn bool_of(node: &Spanned, key: &str) -> bool {
    node.get(key).and_then(Spanned::as_bool).unwrap_or(false)
}

/// The count at `key` of `node`; a negative count reads as zero.
pub fn usize_of(node: &Spanned, key: &str) -> Option<usize> {
    node.get(key)
        .and_then(Spanned::as_int)
        .map(|i| usize::try_from(i).unwrap_or(0))
}

/// The string or the list of strings at `key` of `node`.
pub fn list_of(node: &Spanned, key: &str) -> Vec<String> {
    node.get(key)
        .and_then(Spanned::as_str_or_list)
        .unwrap_or_default()
}

impl<'a> Lowerer<'a> {
    /// A lowering of a schema at `schema`, whose templates see `types`.
    // sokf:begin lowering
    pub fn new(regexes: &'a RegexCache, schema: &'a RepoPath, types: TypeRegistry) -> Self {
        // sokf:end lowering
        Lowerer {
            regexes,
            schema,
            findings: Vec::new(),
            rule_ids: HashSet::new(),
            lifecycle: Vec::new(),
            types,
        }
    }

    /// A finding on the schema, at `line`, that breaks `criterion`.
    pub fn fail(&mut self, line: usize, criterion: &'static str, message: impl Into<String>) {
        self.findings.push(Finding::error(
            self.schema.clone(),
            Some(line),
            criterion,
            message,
        ));
    }

    /// The grammar `limit` at `key` of `node`, such as a block's `max`. The
    /// generic check has already checked the map against the grammar, so
    /// only an empty `when-at-limit` is reported here.
    // sokf-implements: TL_138
    // sokf-implements: CORE-6_AC-13
    pub fn limit(&mut self, node: &Spanned, key: &str) -> Option<Limit> {
        let map = node.get(key)?;
        let (kind, count) = match (usize_of(map, "soft"), usize_of(map, "hard")) {
            (Some(n), _) => (LimitKind::Soft, n),
            (None, Some(n)) => (LimitKind::Hard, n),
            (None, None) => return None,
        };
        let when_at_limit = str_of(map, "when-at-limit").unwrap_or_default();
        if when_at_limit.trim().is_empty() {
            let line = map.get("when-at-limit").map_or(map.line, |w| w.line);
            self.fail(
                line,
                "TL_138",
                format!(
                    "`when-at-limit` of `{key}` is empty: it holds the text that tells the writer what to do at the limit"
                ),
            );
        }
        Some(Limit {
            kind,
            count,
            when_at_limit,
        })
    }

    /// A finding on the schema, at `line`, for `problem`.
    pub fn fail_with(&mut self, line: usize, problem: Problem) {
        self.findings
            .push(problem.error(self.schema.clone(), Some(line)));
    }

    /// The regex at `key` of `node`, checked where the grammar was read.
    pub fn regex(&self, node: &Spanned, key: &str) -> Option<Regex> {
        let text = node.get(key)?.as_str()?;
        self.regexes.get(text).ok()
    }

    /// The `prohibited-patterns` of `node`.
    pub fn prohibitions(&self, node: &Spanned) -> Vec<super::Prohibition> {
        super::prohibitions(node, self.regexes)
    }

    /// The `only` of a rule, resolved against the lifecycle.
    // sokf-implements: TL_19
    pub fn only(&mut self, node: &Spanned) -> Condition {
        match node.get("only") {
            Some(value) => self.condition(value, "only"),
            None => Condition::default(),
        }
    }

    /// The `optional` of a rule that takes a boolean or a condition: `None`
    /// where the rule is required in every state, and otherwise the
    /// condition in which it is optional, which admits every state for
    /// `true`.
    // sokf-implements: CORE-6_AC-10
    pub fn optional(&mut self, node: &Spanned) -> Option<Condition> {
        let value = node.get("optional")?;
        match value.as_bool() {
            Some(true) => Some(Condition::default()),
            Some(false) => None,
            None => Some(self.condition(value, "optional")),
        }
    }

    /// The condition at `value`, the value of `key`, resolved against the
    /// lifecycle: one map, or a list of maps, as [`Condition::new`] builds
    /// it.
    // sokf-implements: TL_20
    // sokf-implements: TL_136
    fn condition(&mut self, value: &Spanned, key: &str) -> Condition {
        if self.lifecycle.is_empty() {
            // sokf-implements: TL_111
            self.fail(value.line, "TL_111", format!("`{key}` without a lifecycle"));
            return Condition::default();
        }
        let maps: Vec<&Spanned> = match value.as_seq() {
            Some([]) => {
                self.fail(
                    value.line,
                    "TL_136",
                    format!("`{key}` is an empty list; give it a map of states, or a list of maps"),
                );
                return Condition::default();
            }
            Some(items) => items.iter().collect(),
            None => vec![value],
        };
        let maps: Vec<Only> = maps.into_iter().map(|m| self.only_map(m, key)).collect();
        Condition::new(maps)
    }

    /// One map of a condition, each dimension and state checked against
    /// the lifecycle. A dimension that the lifecycle does not declare stays
    /// in the map, which then admits no document, so that a map that names
    /// only unknown dimensions does not admit every state.
    fn only_map(&mut self, map: &Spanned, key: &str) -> Only {
        let mut only = Only::default();
        for (dim, states) in map.as_map().unwrap_or(&[]) {
            let Some(dimension) = self
                .lifecycle
                .iter()
                .find(|d| d.key == dim.value)
                .map(|d| d.states.clone())
            else {
                only.0.insert(
                    dim.value.clone(),
                    states.as_str_or_list().unwrap_or_default(),
                );
                self.fail(
                    dim.line,
                    "TL_20",
                    format!(
                        "`{key}` names a dimension the lifecycle does not declare: `{}`",
                        dim.value
                    ),
                );
                continue;
            };
            let admitted = states.as_str_or_list().unwrap_or_default();
            // A dimension with no state admits no document. An undeclared
            // dimension is reported above alone, because its states mean
            // nothing until it is named right.
            // sokf-implements: TL_136
            if admitted.is_empty() && states.as_seq().is_some() {
                self.fail(
                    states.line,
                    "TL_136",
                    format!(
                        "`{key}` lists no state for `{}`; list at least one",
                        dim.value
                    ),
                );
            }
            for s in &admitted {
                if !dimension.contains(s) {
                    self.fail(
                        states.line,
                        "TL_20",
                        format!("`{key}` names `{s}`, which is no state of `{}`", dim.value),
                    );
                }
            }
            only.0.insert(dim.value.clone(), admitted);
        }
        only
    }

    /// The keys every rule map carries: `severity`, and `only`.
    // sokf-implements: TL_18
    pub fn common(&mut self, node: &Spanned) -> RuleCommon {
        RuleCommon {
            severity: match str_of(node, "severity").as_deref() {
                Some("warn") => Severity::Warning,
                _ => Severity::Error,
            },
            only: self.only(node),
            line: node.line,
        }
    }

    /// The keys of the core grammar of a schema: the lifecycle first, which
    /// `only` resolves against, then the frontmatter, the bans and the
    /// examples. `formats` is the registry the schema sees.
    // sokf-implements: CORE-6_AC-2
    // sokf:begin lowering
    pub fn lower_core(&mut self, yaml: &Spanned, formats: FormatRegistry) -> CoreSchema {
        // sokf:end lowering
        self.lifecycle = yaml
            .get("lifecycle")
            .and_then(|l| l.as_seq())
            .map(|dims| {
                dims.iter()
                    .map(|d| LifecycleDimension {
                        key: str_of(d, "key").unwrap_or_default(),
                        states: list_of(d, "states"),
                        guidance: str_of(d, "guidance"),
                        line: d.line,
                        // sokf-implements: CORE-6_AC-8
                        extensions: extensions(d, DIMENSION_KEYS),
                    })
                    .collect()
            })
            .unwrap_or_default();
        let mut seen_keys = HashSet::new();
        let twice: Vec<(usize, String)> = self
            .lifecycle
            .iter()
            .filter(|d| !seen_keys.insert(d.key.as_str()))
            .map(|d| (d.line, d.key.clone()))
            .collect();
        for (line, key) in twice {
            // sokf-implements: CORE-6_AC-3
            // sokf-implements: TL_95
            self.fail(
                line,
                "TL_95",
                format!("dimension `{key}` is declared twice"),
            );
        }

        let mut frontmatter = Vec::new();
        for (key, node) in yaml
            .get("frontmatter")
            .and_then(|f| f.as_map())
            .unwrap_or(&[])
        {
            if self.lifecycle.iter().any(|d| d.key == key.value) {
                // sokf-implements: CORE-6_AC-4
                // sokf-implements: TL_112
                self.fail(
                    key.line,
                    "TL_112",
                    format!(
                        "`{}` is a lifecycle dimension; it is not repeated under `frontmatter`",
                        key.value
                    ),
                );
            }
            let c = self.frontmatter_constraint(node, &key.value);
            frontmatter.push((key.value.clone(), c));
        }
        // A ban is a bare pattern; its failure quotes what it matched.
        // sokf-implements: TL_17
        let prohibited_patterns = self.prohibitions(yaml);
        let examples = yaml
            .get("examples")
            .and_then(|e| e.as_seq())
            .map(|items| {
                items
                    .iter()
                    .map(|e| Example {
                        document: str_of(e, "document").unwrap_or_default(),
                        line: e.line,
                    })
                    .collect()
            })
            .unwrap_or_default();
        CoreSchema {
            line_limit: yaml
                .get("line-limit")
                .and_then(Spanned::as_int)
                .unwrap_or(0),
            lifecycle: self.lifecycle.clone(),
            frontmatter,
            frontmatter_open: bool_of(yaml, "frontmatter-open"),
            prohibited_patterns,
            formats,
            guidance: str_of(yaml, "guidance"),
            // sokf-implements: CORE-6_AC-16
            language: str_of(yaml, "language"),
            examples,
            line: yaml.line,
            key_lines: yaml
                .as_map()
                .unwrap_or(&[])
                .iter()
                .map(|(k, _)| (k.value.clone(), k.line))
                .collect(),
        }
    }
}

mod checks;
mod shapes;