sokf-core 0.8.0

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
//! This module checks the markers in a file outside the knowledge roots
//! (sokf-format-spec ยง9.5). It checks that each reference resolves, that
//! each tag is spelt correctly, and that each ignore region is opened and
//! closed once, and it warns about each relationship outside the core set.

use super::{Citation, Citations, Ctx, warnings::CORE_RELS};
use crate::{
    Finding, RepoPath, Severity,
    markers::{IgnoreFault, MarkerRow, RowKind, resolve},
};

/// `check` returns the findings for the marker rows of one file, and
/// records in `citations` each marker that resolves, for the coverage
/// pass. When `markers.check = "warn"`, each failure is a warning, not an
/// error.
// sokf-implements: SOKF_FORMAT_143
// sokf-implements: SOKF_FORMAT_144
// sokf-implements: SOKF_FORMAT_145
// sokf-implements: SOKF_FORMAT_152
// sokf-implements: SOKF_FORMAT_153
// sokf-implements: SOKF_VALIDATION_51
// sokf-implements: CONF-7_AC-1
// sokf-implements: CONF-7_AC-2
// sokf-implements: EXT-9_AC-6
pub fn check(
    path: &RepoPath,
    rows: &[MarkerRow],
    ctx: &Ctx<'_>,
    citations: &mut Citations,
) -> Vec<Finding> {
    let failure = ctx.model.marker_check.severity();
    let mut findings = Vec::new();
    let mut push = |line: usize, severity: Severity, message: String, key: &'static str| {
        findings.push(Finding::new(
            path.clone(),
            Some(line),
            severity,
            key,
            message,
        ));
    };
    for row in rows {
        let line = row.line;
        match &row.kind {
            RowKind::Marker { rel, target } => {
                let written = format!("sokf-{rel}: {target}");
                if !CORE_RELS.contains(&rel.as_str()) {
                    push(
                        line,
                        Severity::Warning,
                        format!(
                            "`{written}` has the relationship `{rel}`, which SOKF doesn't define, so consumers read it as `relates-to`"
                        ),
                        "SOKF_VALIDATION_51",
                    );
                }
                // A marker that names a broken store waits until the store
                // mends, as the store's own files do (FORMAT-5_AC-13).
                if crate::markers::names_broken_store(ctx.model, target) {
                    continue;
                }
                match resolve(ctx.model, ctx.keys, target) {
                    Ok(r) => citations.add(
                        r.path,
                        Citation {
                            declared: false,
                            slug: r.slug,
                            source: path.clone(),
                            source_type: String::new(),
                            rel: Some(rel.clone()),
                            line,
                            include: false,
                            supersedes: false,
                            retired: false,
                        },
                    ),
                    Err(u) => push(
                        line,
                        failure,
                        format!("`{written}` {}", u.message),
                        u.criterion,
                    ),
                }
            }
            RowKind::Malformed(tag) => push(
                line,
                failure,
                format!(
                    "`{tag}` has no reference after it, or has a word after its reference. A marker holds exactly one reference"
                ),
                "SOKF_FORMAT_144",
            ),
            // sokf-implements: CLI-4_AC-41
            RowKind::Rel(tag) => push(
                line,
                failure,
                format!(
                    "`{tag}` has a rel that isn't kebab case. Write the rel as lowercase words of letters and digits joined by `-`, such as `sokf-implements:`"
                ),
                "SOKF_FORMAT_143",
            ),
            RowKind::Ignore(fault) => {
                let (message, key) = match fault {
                    IgnoreFault::BeginInside => (
                        "`sokf:ignore-begin` appears inside an ignore region that is already open",
                        "SOKF_FORMAT_152",
                    ),
                    IgnoreFault::NeverEnds => (
                        "`sokf:ignore-begin` has no `sokf:ignore-end` after it",
                        "SOKF_FORMAT_152",
                    ),
                    IgnoreFault::EndOutside => (
                        "`sokf:ignore-end` appears outside an ignore region",
                        "SOKF_FORMAT_153",
                    ),
                };
                push(line, failure, message.to_string(), key);
            }
        }
    }
    findings
}