sokf-core 0.5.1

sokf-core holds the logic of the sokf command-line tool: it loads and validates Superdev Open Knowledge Format (SOKF) knowledge stores, and runs each sokf command.
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! A SOKF schema is the engine's schema plus what SOKF's extension of the
//! grammar adds to it (sokf-schema-spec, contract-schema-extension): the
//! dispatch, the asset globs, the lifecycle root and paths, the link rules,
//! the citation rules, and the target types of the unit rules.

use std::collections::BTreeMap;

use tramlines_md::grammar::lower_common;

use super::{Block, RuleCommon, Schema, Unit};
use crate::{Finding, RepoPath, Spanned};

/// `Dispatch` says which files a schema governs (SOKF_SCHEMA_3).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Dispatch {
    /// `Type` selects every document of the named type.
    Type(String),
    /// `Globs` selects every repository file that matches one of the globs.
    Globs(Vec<String>),
}

/// `LinkRule` is one entry of a schema's `links`, the grammar's `link` rule.
#[derive(Debug, Clone)]
pub struct LinkRule {
    /// `common` holds the keys that every rule shares.
    pub common: RuleCommon,
    /// `rel` is the relationship of the links that the entry governs.
    pub rel: String,
    /// `to_type` lists the types that a link can point to, and an empty list
    /// admits any type.
    pub to_type: Vec<String>,
    /// `min` is the fewest links allowed. It defaults to one when the key is
    /// absent.
    pub min: usize,
    /// `max` is the most links allowed, if the entry sets it.
    pub max: Option<usize>,
    /// `guidance`: the reason for the link, shown with the link.
    pub guidance: Option<String>,
}

/// `CitedFrom` says what a `cited-by` entry counts as a citation
/// (SOKF_SCHEMA_48).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CitedFrom {
    /// `from-type` lists the types whose body links count: a link from a
    /// document of any of them is a citation.
    Types(Vec<String>),
    /// `from-marker`: the relationship of the markers that count.
    Marker(String),
}

/// `CitationRule` is one `cited-by` entry, the grammar's `citation` rule:
/// the inverse of a link rule.
#[derive(Debug, Clone)]
pub struct CitationRule {
    /// `common` holds the keys that every rule shares. Its `only` applies to
    /// the cited document's states.
    pub common: RuleCommon,
    /// `from-type` or `from-marker`.
    pub from: CitedFrom,
    /// `keys` names the rule whose keys must each be cited. `None` means that
    /// the document itself must be cited.
    pub keys: Option<String>,
    /// `message` is the sentence that the entry appends to each finding it
    /// raises, if the entry declares one.
    pub message: Option<String>,
}

/// `PathPlan` is one `plans-paths` entry, the grammar's `path-plan` rule: a
/// section whose path links plan files, while the document is in the states
/// of its `only`.
#[derive(Debug, Clone)]
pub struct PathPlan {
    /// `common` holds the keys that every rule shares. Its `only` applies to
    /// the planning document's states.
    pub common: RuleCommon,
    /// `section` is the `rule-id` of the section whose path links plan files.
    pub section: String,
}

/// `SokfSchema` is a SOKF schema lowered from its file: the engine's schema
/// and the keys that SOKF's extension adds to it.
#[derive(Debug, Clone)]
pub struct SokfSchema {
    /// `core` is the engine's schema.
    pub core: Schema,
    /// How the schema selects its files: by a `type` constant or by globs.
    pub dispatch: Dispatch,
    /// `asset_files` holds the globs of the assets that this type's documents
    /// describe, and the list is empty for a type without assets.
    pub asset_files: Vec<String>,
    /// `root` is the directory under which the documents of the type sit.
    pub root: Option<String>,
    /// The lifecycle dimensions that declare `path`, in lifecycle order.
    pub paths: Vec<String>,
    /// `links` holds the schema's link rules, one for each `rel`.
    pub links: Vec<LinkRule>,
    /// `links_open` is true when the schema accepts a link of any `rel`.
    pub links_open: bool,
    /// `cited_by` holds the schema's citation rules.
    pub cited_by: Vec<CitationRule>,
    /// `markers` lists the relationships of the markers that can cite a
    /// document of the type. `None` admits every relationship.
    pub markers: Option<Vec<String>>,
    /// `plans_paths` holds the sections whose path links plan files.
    pub plans_paths: Vec<PathPlan>,
    /// `current_links` holds the patterns of the paragraphs that link no
    /// archived document.
    pub current_links: Vec<regex::Regex>,
    /// `unit_types` holds the `to-type` of each unit rule that declares
    /// one, by the rule's `Unit::index`, which names the rule in the
    /// engine's record of an accepted unit.
    pub unit_types: BTreeMap<usize, Vec<String>>,
}

fn fail(
    path: &RepoPath,
    line: usize,
    criterion: &'static str,
    message: impl Into<String>,
) -> Finding {
    Finding::error(path.clone(), Some(line), criterion, message)
}

/// `glob_problem` says what is wrong with a glob of a schema, as the end of
/// a sentence about it, or returns `None` when the glob starts with / and
/// parses.
fn glob_problem(g: &str) -> Option<String> {
    if !g.starts_with('/') {
        return Some("does not start with /".into());
    }
    globset::Glob::new(g)
        .err()
        .map(|e| format!("does not parse: {e}"))
}

fn list(node: &Spanned) -> Vec<String> {
    node.as_str_or_list().unwrap_or_default()
}

/// `kebab` returns the lowercase kebab case form of `rel`, splitting camel
/// case and turning `_` and spaces into `-`, such as `test-of` for `TestOf`
/// or `test_of`. It returns `None` when the form still isn't a slug.
fn kebab(rel: &str) -> Option<String> {
    let mut out = String::new();
    let mut prev_lower = false;
    for c in rel.chars() {
        if c.is_ascii_uppercase() && prev_lower {
            out.push('-');
        }
        prev_lower = c.is_ascii_lowercase() || c.is_ascii_digit();
        out.push(match c {
            '_' | ' ' => '-',
            c => c.to_ascii_lowercase(),
        });
    }
    crate::markdown::is_slug(&out).then_some(out)
}

/// `glob_inside` reports whether every path that `glob` can match lies under
/// `dir`: it checks that the glob's literal prefix, up to its first wildcard,
/// starts with `dir/`.
pub fn glob_inside(glob: &str, dir: &RepoPath) -> bool {
    let prefix: String = glob
        .chars()
        .take_while(|c| !matches!(c, '*' | '?' | '[' | '{'))
        .collect();
    let dir = format!("{}/", dir.as_str().trim_end_matches('/'));
    prefix.starts_with(&dir)
}

impl SokfSchema {
    /// `of_schema_document` returns the schema that the grammar's
    /// `schema-document` section defines. That schema governs Schema
    /// documents and demands nothing that SOKF adds. Tests also use it as a
    /// base.
    pub fn of_schema_document(core: Schema) -> Self {
        SokfSchema {
            core,
            dispatch: Dispatch::Type("Schema".into()),
            asset_files: vec![],
            root: None,
            paths: vec![],
            links: vec![],
            links_open: true,
            cited_by: vec![],
            markers: None,
            plans_paths: vec![],
            current_links: vec![],
            unit_types: BTreeMap::new(),
        }
    }

    /// `lower` reads the keys that SOKF's extension adds from `yaml`, the
    /// schema file at `path`, and builds the SOKF schema around `core`, the
    /// engine's schema of the same file. It also checks the rules of the SOKF
    /// schema extension (contract-schema-extension) that the generic check
    /// cannot test. It returns the schema with a finding for each rule that
    /// the file breaks.
    // sokf-implements: EXT-2_AC-2
    // sokf-implements: SOKF_SCHEMA_3
    // sokf-implements: SOKF_SCHEMA_27
    pub fn lower(core: Schema, yaml: &Spanned, path: &RepoPath) -> (Self, Vec<Finding>) {
        let mut findings = Vec::new();
        let ext = |key: &str| core.extensions.get(key);
        let globs = ext("additional-files").map(list);
        let assets = ext("asset-files").map(list);
        // A schema dispatches on a `type` const, on `additional-files`, or
        // on a `type` const with `asset-files`, and it must declare exactly
        // one of `type` and `additional-files`.
        let dispatch = match (core.core.type_const().map(str::to_string), globs) {
            (Some(_), Some(_)) => {
                findings.push(fail(
                    path,
                    yaml.line,
                    "SOKF_SCHEMA_3",
                    "the schema declares both a `type` const and `additional-files`. Declare only one of them",
                ));
                Dispatch::Globs(vec![])
            }
            (None, None) => {
                let message = if assets.is_some() {
                    "`asset-files` needs a `type` const as well"
                } else {
                    "the schema declares neither a `type` const nor `additional-files`. Declare one of them"
                };
                findings.push(fail(path, yaml.line, "SOKF_SCHEMA_3", message));
                Dispatch::Globs(vec![])
            }
            (Some(t), None) => Dispatch::Type(t),
            (None, Some(globs)) => {
                let line = ext("additional-files").map_or(yaml.line, |a| a.line);
                for g in &globs {
                    if let Some(why) = glob_problem(g) {
                        findings.push(fail(
                            path,
                            line,
                            "SOKF_SCHEMA_5",
                            format!("`additional-files` glob `{g}` {why}"),
                        ));
                    }
                }
                Dispatch::Globs(globs)
            }
        };
        let asset_files = match assets {
            None => vec![],
            Some(globs) => {
                if ext("additional-files").is_some() {
                    let line = ext("asset-files").map_or(yaml.line, |a| a.line);
                    findings.push(fail(
                        path,
                        line,
                        "SOKF_SCHEMA_4",
                        "the schema declares `asset-files` together with `additional-files`. Declare only one of them",
                    ));
                }
                globs
            }
        };
        let paths: Vec<String> = core
            .core
            .lifecycle
            .iter()
            .filter(|d| {
                d.extensions
                    .get("path")
                    .and_then(|p| p.as_bool())
                    .unwrap_or(false)
            })
            .map(|d| d.key.clone())
            .collect();
        let root = ext("root").and_then(|r| r.as_str()).map(str::to_string);
        // Each archived state must be a state of its own dimension. A
        // schema-level `archived: true` is allowed only where the schema has
        // no lifecycle dimension to declare it on.
        // sokf-implements: EXT-7_AC-2
        // sokf-implements: SOKF_SCHEMA_50
        for d in &core.core.lifecycle {
            let Some(node) = d.extensions.get("archived") else {
                continue;
            };
            for state in list(node) {
                if !d.states.contains(&state) {
                    findings.push(fail(
                        path,
                        node.line,
                        "SOKF_SCHEMA_50",
                        format!(
                            "`archived` state `{state}` is not a state of the dimension `{}`",
                            d.key
                        ),
                    ));
                }
            }
        }
        // sokf-implements: EXT-2_AC-3
        if let Some(a) = ext("archived")
            && !core.core.lifecycle.is_empty()
        {
            findings.push(fail(
                path,
                a.line,
                "SOKF_SCHEMA_50",
                "the schema declares `archived` together with `lifecycle`. Declare `archived` on a dimension",
            ));
        }
        if let Some(l) = yaml.get("lifecycle") {
            if ext("additional-files").is_some() {
                findings.push(fail(
                    path,
                    l.line,
                    "SOKF_SCHEMA_4",
                    "the schema declares `lifecycle` together with `additional-files`. Declare only one of them",
                ));
            }
            if !paths.is_empty() && root.is_none() {
                findings.push(fail(
                    path,
                    l.line,
                    "SOKF_SCHEMA_27",
                    "a path-enforced dimension needs `root`",
                ));
            }
        }
        if let Some(r) = ext("root")
            && root.as_deref().and_then(RepoPath::parse).is_none()
        {
            findings.push(fail(
                path,
                r.line,
                "SOKF_SCHEMA_27",
                "`root` is not a valid repository path: it must start with / and have no empty, `.`, or `..` segment",
            ));
        }
        findings.extend(asset_globs(
            path,
            ext("asset-files").map_or(yaml.line, |a| a.line),
            &asset_files,
            root.as_deref(),
        ));
        let mut links = Vec::new();
        for node in ext("links").and_then(|l| l.as_seq()).unwrap_or(&[]) {
            let (common, problems) = lower_common(node, &core.core, path);
            findings.extend(problems);
            let usize_of = |key: &str| {
                node.get(key)
                    .and_then(|v| v.as_int())
                    .map(|i| usize::try_from(i).unwrap_or(0))
            };
            links.push(LinkRule {
                common,
                rel: node
                    .get("rel")
                    .and_then(|r| r.as_str())
                    .unwrap_or_default()
                    .to_string(),
                to_type: node.get("to-type").map(list).unwrap_or_default(),
                min: usize_of("min").unwrap_or(1),
                max: usize_of("max"),
                guidance: node
                    .get("guidance")
                    .and_then(|g| g.as_str())
                    .map(str::to_string),
            });
        }
        let links_open = ext("links-open").and_then(|l| l.as_bool()).unwrap_or(false);
        // A citation entry's `keys` identifies a rule of this schema that
        // captures a key, and sokf resolves it as it resolves the rule of
        // `for-each-key-of`.
        // sokf-implements: EXT-9_AC-5
        let mut cited_by = Vec::new();
        for node in ext("cited-by").and_then(|l| l.as_seq()).unwrap_or(&[]) {
            let (common, problems) = lower_common(node, &core.core, path);
            findings.extend(problems);
            let keys = node
                .get("keys")
                .and_then(|k| k.as_str())
                .map(str::to_string);
            if let Some(k) = &keys
                && let Err(why) = core.rule_captures_key(k)
            {
                findings.push(fail(
                    path,
                    node.line,
                    "SOKF_SCHEMA_35",
                    why.message("keys", k),
                ));
            }
            // The grammar demands exactly one of `from-type` and
            // `from-marker`. The entry counts markers only when it has
            // `from-marker` and no `from-type`.
            let from = match (node.get("from-type"), node.get("from-marker")) {
                (None, Some(m)) => CitedFrom::Marker(m.as_str().unwrap_or_default().to_string()),
                (t, _) => CitedFrom::Types(t.map(list).unwrap_or_default()),
            };
            // sokf-implements: EXT-9_AC-9
            let message = node
                .get("message")
                .and_then(|m| m.as_str())
                .map(str::to_string);
            cited_by.push(CitationRule {
                common,
                from,
                keys,
                message,
            });
        }
        let markers_node = ext("markers");
        let markers = markers_node.map(list);
        // A tag is lowercase kebab case, so a rel of `markers` in another
        // form matches no marker, and the schema would refuse every marker
        // that its author meant.
        // sokf-implements: EXT-9_AC-11
        for item in markers_node.and_then(|n| n.as_seq()).unwrap_or(&[]) {
            let Some(rel) = item.as_str() else { continue };
            if crate::markdown::is_slug(rel) {
                continue;
            }
            let fix = match kebab(rel) {
                Some(k) => format!("Write it as `{k}`"),
                None => "Write it as lowercase words of letters and digits joined by `-`".into(),
            };
            findings.push(fail(
                path,
                item.line,
                "EXT-9_AC-11",
                format!(
                    "`markers` names the relationship `{rel}`, which isn't lowercase kebab case, so it matches no marker. {fix}"
                ),
            ));
        }
        // A path plan's `section` is the `rule-id` of a section of this
        // schema.
        // sokf-implements: SOKF_SCHEMA_52
        let mut plans_paths = Vec::new();
        for node in ext("plans-paths").and_then(|l| l.as_seq()).unwrap_or(&[]) {
            let (common, problems) = lower_common(node, &core.core, path);
            findings.extend(problems);
            let section = node
                .get("section")
                .and_then(|s| s.as_str())
                .unwrap_or_default()
                .to_string();
            if !core
                .sections
                .iter()
                .any(|s| s.rule_id.as_deref() == Some(section.as_str()))
            {
                findings.push(fail(
                    path,
                    node.line,
                    "SOKF_SCHEMA_52",
                    format!("`section: {section}` names no section of the schema; give the `rule-id` of a section"),
                ));
            }
            plans_paths.push(PathPlan { common, section });
        }
        // The grammar checks that each pattern compiles (TL_118).
        let current_links = ext("current-links")
            .map(list)
            .unwrap_or_default()
            .iter()
            .filter_map(|p| regex::Regex::new(p).ok())
            .collect();
        let unit_types = unit_types(&core);
        let schema = SokfSchema {
            core,
            dispatch,
            asset_files,
            root,
            paths,
            links,
            links_open,
            cited_by,
            markers,
            plans_paths,
            current_links,
            unit_types,
        };
        (schema, findings)
    }

    /// `has_assets` reports whether the schema's documents are sidecars, that
    /// is, whether the schema declares `asset-files` (SOKF_SCHEMA_11).
    pub fn has_assets(&self) -> bool {
        !self.asset_files.is_empty()
    }

    /// `is_path` reports whether the lifecycle dimension `key` declares `path`.
    pub fn is_path(&self, key: &str) -> bool {
        self.paths.iter().any(|p| p == key)
    }
}

/// `unit_types` returns the `to-type` of each unit rule of `schema` that
/// declares one, by the rule's `Unit::index`: the rules of each block of a
/// section or of the preamble, of the blocks nested in a block or an item,
/// and of a fence's `lines`.
// sokf-implements: EXT-5_AC-2
fn unit_types(schema: &Schema) -> BTreeMap<usize, Vec<String>> {
    let mut out = BTreeMap::new();
    let mut blocks: Vec<&Block> = schema
        .sections
        .iter()
        .filter_map(|s| s.blocks.as_deref())
        .chain(schema.preamble.iter().filter_map(|p| p.blocks.as_deref()))
        .flatten()
        .collect();
    while let Some(b) = blocks.pop() {
        blocks.extend(b.blocks.iter().flatten());
        let mut units: Vec<&Unit> = b.units().collect();
        while let Some(u) = units.pop() {
            units.extend(u.lines.iter().flatten());
            blocks.extend(u.blocks.iter().flatten());
            if let Some(types) = u.extensions.get("to-type") {
                out.insert(u.index, list(types));
            }
        }
    }
    out
}

/// `asset_globs` checks the glob rules for `asset-files` (SOKF_SCHEMA_8,
/// SOKF_SCHEMA_10): each glob starts with /, parses, and lies inside the
/// schema's knowledge root, which is the grandparent of the schema's path.
/// Where `root` is declared, each glob also begins with `root` followed by `/**/`.
// sokf-implements: EXT-2_AC-1
fn asset_globs(path: &RepoPath, line: usize, globs: &[String], root: Option<&str>) -> Vec<Finding> {
    let mut findings = Vec::new();
    let knowledge = path.parent().and_then(|d| d.parent());
    for g in globs {
        if let Some(why) = glob_problem(g) {
            findings.push(fail(
                path,
                line,
                "SOKF_SCHEMA_8",
                format!("`asset-files` glob `{g}` {why}"),
            ));
            continue;
        }
        if let Some(k) = &knowledge
            && !glob_inside(g, k)
        {
            findings.push(fail(
                path,
                line,
                "SOKF_SCHEMA_8",
                format!("`asset-files` glob `{g}` lies outside the knowledge root `{k}`"),
            ));
        }
        if let Some(r) = root
            && !g.starts_with(&format!("{}/**/", r.trim_end_matches('/')))
        {
            findings.push(fail(
                path,
                line,
                "SOKF_SCHEMA_10",
                format!(
                    "`asset-files` glob `{g}` does not begin with `{r}/**/`, where `{r}` is the lifecycle root"
                ),
            ));
        }
    }
    findings
}