mod assets;
mod config;
mod conflicts;
mod coverage;
mod current_links;
mod deprecated;
mod frontmatter;
mod generated;
mod host;
mod index;
mod lifecycle;
mod link_rules;
mod links;
mod manifest;
mod markers;
mod plans;
mod schema_document;
mod unit_types;
mod verified;
mod warnings;
use std::collections::{BTreeMap, BTreeSet, HashSet};
pub use config::{check_config, leftover_packs};
pub use coverage::{Citation, Citations};
pub use generated::asset_include;
pub use host::{SchemaHost, SokfHost, check_against};
pub use index::check_indexes;
pub use lifecycle::{derived_dir, placed};
pub use plans::{capture_plans_of, planned_by};
pub use tramlines_md::check::{
Capture, CheckOutput, Gate, LabelRepair, Placement, Repair, Spot, heading_col, placements,
};
use crate::{
Body, Compiled, Expected, Finding, Indexes, Locator, Model, RepoPath, Segments, Spanned,
grammar::{KeyScope, SokfSchema, States},
markdown,
model::{CoreFields, KeyEntry, KeyIndex, Query},
};
#[derive(Debug, Clone, Copy)]
pub struct DocView<'a> {
pub path: &'a RepoPath,
pub core: Option<&'a CoreFields>,
pub frontmatter: Option<&'a Spanned>,
pub body: &'a Body,
pub segments: &'a Segments,
pub line_count: usize,
pub text: &'a str,
pub body_line: usize,
}
impl<'a> DocView<'a> {
pub fn engine(&self) -> tramlines_md::check::DocView<'a> {
tramlines_md::check::DocView {
path: self.path,
frontmatter: self.core.filter(|c| c.present).and(self.frontmatter),
body: self.body,
segments: self.segments,
line_count: self.line_count,
text: self.text,
body_line: self.body_line,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Ctx<'a> {
pub model: &'a Model,
pub compiled: &'a Compiled,
pub expected: &'a Expected,
pub keys: &'a KeyIndex,
pub knowledge: Option<usize>,
pub scaffold: bool,
}
impl<'a> Ctx<'a> {
pub fn new(
model: &'a Model,
compiled: &'a Compiled,
expected: &'a Expected,
keys: &'a KeyIndex,
knowledge: Option<usize>,
) -> Self {
Ctx {
model,
compiled,
expected,
keys,
knowledge,
scaffold: false,
}
}
}
pub fn states_of(frontmatter: Option<&Spanned>, schema: &SokfSchema) -> States {
schema.core.states(frontmatter)
}
pub fn capture_keys_of(
model: &Model,
compiled: &Compiled,
only: Option<&BTreeSet<RepoPath>>,
) -> KeyIndex {
let mut keys = KeyIndex::default();
let empty = KeyIndex::default();
let expected = &Expected::default();
for (k, doc) in model.documents() {
if only.is_some_and(|o| !o.contains(&doc.path)) {
continue;
}
let Some((_, schema)) = compiled.for_doc(model, k, doc) else {
continue;
};
let ctx = Ctx::new(model, compiled, expected, &empty, Some(k));
model.watch(&doc.path);
let out = check_against(
doc_view(doc),
schema,
&states_of(doc.frontmatter.as_ref(), schema),
&ctx,
Locator::schema(""),
);
model.unwatch();
let captured: HashSet<String> = out
.captures
.iter()
.map(|c| markdown::slug(&c.key))
.collect();
for anchor in out.anchors {
if !captured.contains(&anchor.id) {
keys.add_anchor(doc.path.clone(), anchor.id);
}
}
let capped = model.knowledges[k].capped();
for c in out.captures {
keys.add(KeyEntry {
key: c.key,
path: doc.path.clone(),
line: c.line,
heading_slug: c.heading_slug,
scope: cap(c.scope, capped),
knowledge: k,
rule_id: c.rule_id,
});
}
}
keys
}
fn cap(scope: KeyScope, capped: bool) -> KeyScope {
match scope {
KeyScope::Host(h) if capped && h == "repository" => KeyScope::Host("knowledge".into()),
other => other,
}
}
pub fn doc_view(doc: &crate::Document) -> DocView<'_> {
DocView {
path: &doc.path,
core: Some(&doc.core),
frontmatter: doc.frontmatter.as_ref(),
body: &doc.body,
segments: &doc.segments,
line_count: doc.line_count(),
text: &doc.source.text,
body_line: doc.body_line,
}
}
pub fn additional_view(file: &crate::model::AdditionalFile) -> DocView<'_> {
DocView {
path: &file.path,
core: None,
frontmatter: None,
body: &file.body,
segments: &file.segments,
line_count: file.source.line_count(),
text: &file.source.text,
body_line: 1,
}
}
#[derive(Debug, Default)]
pub struct RunOutput {
pub fresh: Vec<Finding>,
pub documents: BTreeMap<RepoPath, Vec<Finding>>,
pub citations: BTreeMap<RepoPath, Vec<(RepoPath, Citation)>>,
}
impl RunOutput {
pub fn findings(&self) -> Vec<Finding> {
let mut all = self.fresh.clone();
for list in self.documents.values() {
all.extend(list.iter().cloned());
}
all
}
}
pub fn run(
model: &Model,
compiled: &Compiled,
expected: &Expected,
keys: &KeyIndex,
indexes: &Indexes,
) -> Vec<Finding> {
run_units(
model,
compiled,
expected,
keys,
indexes,
None,
Vec::new(),
true,
)
.findings()
}
#[allow(clippy::too_many_arguments)]
pub fn run_units(
model: &Model,
compiled: &Compiled,
expected: &Expected,
keys: &KeyIndex,
indexes: &Indexes,
units: Option<&BTreeSet<RepoPath>>,
stored_citations: Vec<(RepoPath, Citation)>,
fresh: bool,
) -> RunOutput {
let mut out = RunOutput::default();
let broken: Vec<bool> = (0..model.knowledges.len())
.map(|k| model.broken(k).is_some())
.collect();
let checked =
|path: &RepoPath| units.is_none_or(|u| u.contains(path)) && !model.in_broken_store(path);
let ctx = |knowledge: Option<usize>| Ctx::new(model, compiled, expected, keys, knowledge);
if fresh {
out.fresh.extend(repository_findings(model, compiled));
}
let mut citations = Citations::default();
for (k, knowledge) in model.knowledges.iter().enumerate() {
let ctx = ctx(Some(k));
if fresh {
out.fresh.extend(manifest::check(knowledge));
if !broken[k] {
out.fresh.extend(assets::check_assets(model, knowledge));
}
}
for doc in &knowledge.documents {
if broken[k] && units.is_none_or(|u| u.contains(&doc.path)) {
check_unit(model, &mut out, &mut citations, &doc.path, |c| {
let _ = links::check_document(doc_view(doc), &ctx, c);
Vec::new()
});
continue;
}
if !checked(&doc.path) {
continue;
}
check_unit(model, &mut out, &mut citations, &doc.path, |c| {
check_document(k, doc, &ctx, c)
});
}
for (i, schema) in knowledge.schemas.iter().enumerate() {
if !checked(&schema.doc.path) {
continue;
}
check_unit(model, &mut out, &mut citations, &schema.doc.path, |c| {
check_schema_unit(k, i, &ctx, c)
});
}
}
let mut index_findings = index::check_indexes_by_index(model, indexes, &expected.layout);
for path in indexes.expected.keys() {
if !checked(path) {
continue;
}
out.documents.insert(
path.clone(),
index_findings.remove(path).unwrap_or_default(),
);
}
for file in &model.additional {
if !checked(&file.path) {
continue;
}
let knowledge = crate::include::knowledge_of_additional(model, file);
let ctx = ctx(knowledge);
check_unit(model, &mut out, &mut citations, &file.path, |c| {
check_additional(file, &ctx, c)
});
}
for (path, rows) in &model.markers {
if !checked(path) {
continue;
}
let ctx = ctx(None);
check_unit(model, &mut out, &mut citations, path, |c| {
markers::check(path, rows, &ctx, c)
});
}
for (target, citation) in stored_citations {
citations.add(target, citation);
}
for (k, doc) in model.documents() {
if !checked(&doc.path) {
continue;
}
model.watch(&doc.path);
let mut findings = uniqueness_of(model, keys, doc);
findings.extend(coverage::check_document(
model, compiled, keys, &citations, k, doc,
));
findings.extend(deprecated::check_document(model, &citations, k, doc));
model.unwatch();
out.documents
.entry(doc.path.clone())
.or_default()
.extend(findings);
}
out
}
fn repository_findings(model: &Model, compiled: &Compiled) -> Vec<Finding> {
let schema_paths: BTreeSet<&RepoPath> = model
.knowledges
.iter()
.flat_map(|k| k.schemas.iter().map(|s| &s.doc.path))
.collect();
let mut findings: Vec<Finding> = model
.problems
.iter()
.filter(|p| {
model
.knowledge_of(&p.path)
.is_none_or(|k| model.broken(k).is_none())
})
.cloned()
.collect();
findings.extend(
compiled
.problems
.iter()
.filter(|p| !schema_paths.contains(&p.path))
.cloned(),
);
findings.extend(manifest::check_identities(model));
findings.extend(manifest::check_extends(model));
findings
}
fn check_schema_unit(k: usize, i: usize, ctx: &Ctx<'_>, citations: &mut Citations) -> Vec<Finding> {
let knowledge = &ctx.model.knowledges[k];
let schema = &knowledge.schemas[i];
let mut findings: Vec<Finding> = ctx
.compiled
.problems
.iter()
.filter(|p| p.path == schema.doc.path)
.cloned()
.collect();
findings.extend(conflicts::check(&schema.doc));
findings.extend(schema_document::check_schema(knowledge, i, ctx));
findings.extend(frontmatter::check_core(&schema.doc));
findings.extend(frontmatter::check_ids(ctx.model, k, &schema.doc));
findings.extend(links::check_document(doc_view(&schema.doc), ctx, citations));
findings.extend(generated::check(doc_view(&schema.doc), ctx, citations));
findings.extend(verified::check(&schema.doc, ctx));
findings
}
fn check_unit(
model: &Model,
out: &mut RunOutput,
citations: &mut Citations,
path: &RepoPath,
check: impl FnOnce(&mut Citations) -> Vec<Finding>,
) {
model.watch(path);
let before = citations.len();
let findings = check(citations);
out.citations
.entry(path.clone())
.or_default()
.extend(citations.since(before));
model.unwatch();
out.documents
.entry(path.clone())
.or_default()
.extend(findings);
}
pub fn check_document(
k: usize,
doc: &crate::Document,
ctx: &Ctx<'_>,
citations: &mut Citations,
) -> Vec<Finding> {
let mut findings = frontmatter::check(k, doc, ctx);
findings.extend(conflicts::check(doc));
findings.extend(schema_document::check_typed(doc, ctx));
findings.extend(assets::check_document(doc, ctx));
findings.extend(links::check_document(doc_view(doc), ctx, citations));
findings.extend(generated::check(doc_view(doc), ctx, citations));
findings.extend(warnings::check(doc));
findings.extend(verified::check(doc, ctx));
findings
}
pub fn check_additional(
file: &crate::model::AdditionalFile,
ctx: &Ctx<'_>,
citations: &mut Citations,
) -> Vec<Finding> {
let view = additional_view(file);
let mut findings = links::check_document(view, ctx, citations);
findings.extend(generated::check(view, ctx, citations));
if let Some(r) = file.schema {
findings.extend(schema_document::check_additional(view, r, ctx));
}
findings
}
fn uniqueness_of(model: &Model, keys: &KeyIndex, doc: &crate::Document) -> Vec<Finding> {
let mut findings = Vec::new();
let mut clashes: BTreeMap<(RepoPath, usize, String), Vec<RepoPath>> = BTreeMap::new();
for e in keys.of_document(&doc.path) {
let s = markdown::slug(&e.key);
let entries: &[KeyEntry] = match &e.scope {
KeyScope::Host(scope) if scope == "knowledge" => {
model.note(Query::KeyKnowledge {
knowledge: e.knowledge,
slug: s.clone(),
});
keys.knowledge
.get(&(e.knowledge, s))
.map(Vec::as_slice)
.unwrap_or_default()
}
KeyScope::Host(scope) if scope == "repository" => {
model.note(Query::KeyRepository(s.clone()));
keys.repository
.get(&s)
.map(Vec::as_slice)
.unwrap_or_default()
}
_ => continue,
};
let others: Vec<RepoPath> = entries
.iter()
.filter(|o| o.path != e.path)
.map(|o| o.path.clone())
.collect();
if others.is_empty() {
continue;
}
clashes
.entry((e.path.clone(), e.line, e.key.clone()))
.or_default()
.extend(others);
}
for ((path, line, key), mut others) in clashes {
others.sort();
others.dedup();
let names: Vec<String> = others.iter().map(ToString::to_string).collect();
findings.push(Finding::error(
path,
Some(line),
"SOKF_FORMAT_69",
format!("key `{key}` is also captured by {}", names.join(", ")),
));
}
findings
}