use tramlines_md::check as engine;
use super::{Ctx, SchemaHost, check_against, doc_view, lifecycle, states_of};
use crate::{
Finding, Knowledge, Locator, MarkerCheck, Severity,
grammar::{CitedFrom, SokfSchema, States},
model::{Document, SchemaRef},
};
pub fn check_schema(knowledge: &Knowledge, i: usize, ctx: &Ctx<'_>) -> Vec<Finding> {
let mut findings = Vec::new();
let Some(k) = ctx.knowledge else {
return findings;
};
let document = &knowledge.schemas[i];
let r = SchemaRef {
knowledge: k,
index: i,
};
ctx.model.note(ctx.model.schema_query(r));
let out = check_against(
doc_view(&document.doc),
&ctx.compiled.schema_document,
&States::default(),
ctx,
Locator::schema("schema-document"),
);
findings.extend(out.findings);
if let Some(t) = &document.type_const {
let claimants = knowledge.schemas_for(t);
if claimants.len() > 1 {
let others: Vec<String> = claimants
.iter()
.filter(|&&j| j != i)
.map(|&j| knowledge.schemas[j].doc.path.to_string())
.collect();
findings.push(document.finding(
"SOKF_SCHEMA_13",
format!(
"{} also claims type `{t}`. A type has one schema",
others.join(", ")
),
));
}
}
if let Some(schema) = ctx.compiled.schema(r) {
findings.extend(self_check(document, schema, r, ctx));
findings.extend(markers_off(document, schema, ctx));
}
findings
}
fn markers_off(
document: &crate::model::SchemaDocument,
schema: &SokfSchema,
ctx: &Ctx<'_>,
) -> Option<Finding> {
if ctx.model.marker_check != MarkerCheck::Off {
return None;
}
let first = schema
.cited_by
.iter()
.find(|c| matches!(c.from, CitedFrom::Marker(_)))?;
Some(
Finding::new(
document.doc.path.clone(),
Some(first.common.line),
Severity::Warning,
"EXT-9_AC-8",
"`markers.check` is `off`, so sokf checks no `cited-by` entry that uses `from-marker`",
)
.at(&Locator::schema(document.id()).at("cited-by")),
)
}
pub fn check_typed(knowledge: &Knowledge, doc: &Document, ctx: &Ctx<'_>) -> Vec<Finding> {
let mut findings = Vec::new();
let Some(k) = ctx.knowledge else {
return findings;
};
let Some(type_name) = doc.type_name() else {
return stray_anchors(doc);
};
if type_name == "Schema" {
findings.push(Finding::error(
doc.path.clone(),
doc.core.line_of("type"),
"SOKF_FORMAT_2",
"a `Schema` document lives in `schemas/` at the knowledge root, nowhere else",
));
return findings;
}
ctx.model.note(crate::model::Query::Schema {
knowledge: k,
key: type_name.to_string(),
});
let claimants = knowledge.schemas_for(type_name);
match claimants {
[] => {
findings.push(Finding::error(
doc.path.clone(),
doc.core.line_of("type"),
"SOKF_SCHEMA_13",
format!("type `{type_name}` has no schema"),
));
findings.extend(stray_anchors(doc));
}
[one] => {
let r = SchemaRef {
knowledge: k,
index: *one,
};
match ctx.compiled.schema(r) {
Some(schema) => findings.extend(check_document(doc, schema, r, ctx)),
None => findings.push(Finding::error(
doc.path.clone(),
doc.core.line_of("type"),
"SOKF_VALIDATION_41",
format!("the schema of `{type_name}` ({}) breaks the grammar, so sokf doesn't check the document", knowledge.schemas[*one].doc.path),
)),
}
}
many => findings.push(Finding::error(
doc.path.clone(),
doc.core.line_of("type"),
"SOKF_SCHEMA_13",
format!("type `{type_name}` resolves to {} schemas", many.len()),
)),
}
findings
}
fn stray_anchors(doc: &Document) -> Vec<Finding> {
doc.body
.anchors
.iter()
.map(|a| {
Finding::error(
doc.path.clone(),
Some(a.line),
"SOKF_FORMAT_83",
format!(
"the anchor `#{}` names no key: no schema governs the document, so it has none",
a.id
),
)
})
.collect()
}
fn check_document(
doc: &Document,
schema: &SokfSchema,
r: SchemaRef,
ctx: &Ctx<'_>,
) -> Vec<Finding> {
let schema_id = ctx.model.schema(r).id();
let states = states_of(doc.frontmatter.as_ref(), schema);
let mut findings = check_against(
doc_view(doc),
schema,
&states,
ctx,
Locator::schema(schema_id),
)
.findings;
findings.extend(lifecycle::check_directory(&doc.path, schema, &states));
findings
}
pub fn check_additional(view: super::DocView<'_>, r: SchemaRef, ctx: &Ctx<'_>) -> Vec<Finding> {
ctx.model.note(ctx.model.schema_query(r));
let Some(schema) = ctx.compiled.schema(r) else {
return vec![Finding::error(
view.path.clone(),
None,
"SOKF_VALIDATION_41",
format!(
"the schema {} breaks the grammar, so sokf doesn't check the file",
ctx.model.schema(r).doc.path
),
)];
};
let schema_id = ctx.model.schema(r).id();
check_against(
view,
schema,
&States::default(),
ctx,
Locator::schema(schema_id),
)
.findings
}
fn self_check(
document: &crate::model::SchemaDocument,
schema: &SokfSchema,
r: SchemaRef,
ctx: &Ctx<'_>,
) -> Vec<Finding> {
let ctx = Ctx {
knowledge: Some(r.knowledge),
scaffold: true,
..*ctx
};
let host = SchemaHost::new(&ctx, schema);
engine::check_schema(&schema.core, &host, &document.doc.path, &document.id())
}