use tramlines_core::frontmatter::Host as FrontmatterHost;
use tramlines_md::{
check::{self as engine, CheckOptions, Host, ScaffoldFront, Subject},
markdown::Dialect,
};
use super::{
CheckOutput, Ctx, DocView, current_links, frontmatter as fm_check, link_rules, unit_types,
};
use crate::{
Locator, RepoPath, Severity,
grammar::{Dispatch, SokfSchema, States},
markdown::{IncludeTarget, SOKF},
model::{
CoreFields,
frontmatter::{CORE_KEYS, core_fields},
keys::Label,
},
scaffold::SokfScaffold,
};
pub struct SokfHost<'a> {
ctx: &'a Ctx<'a>,
schema: &'a SokfSchema,
core: Option<&'a CoreFields>,
}
impl<'a> SokfHost<'a> {
pub fn new(ctx: &'a Ctx<'a>, schema: &'a SokfSchema, core: Option<&'a CoreFields>) -> Self {
SokfHost { ctx, schema, core }
}
}
impl Host for SokfHost<'_> {
fn dialect(&self) -> Dialect {
SOKF
}
fn image(&self, reference: &str) -> Subject {
if self.ctx.scaffold {
return Subject::Unknown;
}
let label = Label::parse(reference);
label
.id
.and_then(|id| {
self.ctx
.model
.document_in(self.ctx.knowledge?, label.store, id)
})
.map_or(Subject::Unknown, |(_, doc)| {
Subject::Text(self.ctx.model.content_path(&doc.path).to_string())
})
}
fn region(&self, target: &str) -> Subject {
let IncludeTarget::Document {
store,
id,
fragment,
} = IncludeTarget::parse(target)
else {
return Subject::Text(target.to_string());
};
if self.ctx.scaffold {
return Subject::Unknown;
}
let Some((_, doc)) = self
.ctx
.knowledge
.and_then(|k| self.ctx.model.document_in(k, store.as_deref(), &id))
else {
return Subject::Unknown;
};
match (self.ctx.model.asset_of(&doc.path), fragment.as_deref()) {
(Some((_, asset)), None) => Subject::Text(asset.path.to_string()),
(Some((_, asset)), Some(region)) if !region.is_empty() => {
Subject::Text(format!("{}#{region}", asset.path))
}
_ => Subject::Document,
}
}
fn records_accepted(&self) -> bool {
!self.ctx.scaffold && !self.schema.unit_types.is_empty()
}
fn check(
&self,
doc: &engine::DocView<'_>,
states: &States,
_options: CheckOptions,
locator: &Locator,
out: &mut CheckOutput,
) {
if let Some(core) = self.core {
link_rules::check(doc.path, core, self.schema, states, self.ctx, locator, out);
}
current_links::check(doc, self.schema, self.ctx, out);
unit_types::check(doc, self.schema, self.ctx, locator, out);
}
}
impl FrontmatterHost for SokfHost<'_> {
fn scalar_format(&self, name: &str, value: &str) -> Option<Result<(), String>> {
match name {
"repo-path" => Some(match RepoPath::parse(value) {
None => Err(format!("is not a /-rooted path: `{value}`")),
Some(_) if self.ctx.scaffold => Ok(()),
Some(p) if self.ctx.model.exists(&p) => Ok(()),
Some(_) => Err(format!("refers to a file that does not exist: `{value}`")),
}),
"document-id" if self.ctx.scaffold => None,
"document-id" => {
let known = self
.ctx
.knowledge
.and_then(|k| self.ctx.model.document(k, value))
.is_some();
Some(if known {
Ok(())
} else {
Err(format!("is not the id of any document: `{value}`"))
})
}
_ => None,
}
}
fn reserved_keys(&self) -> &[&str] {
CORE_KEYS
}
}
pub struct SchemaHost<'a> {
check: SokfHost<'a>,
scaffold: SokfScaffold<'a>,
}
impl<'a> SchemaHost<'a> {
pub fn new(ctx: &'a Ctx<'a>, schema: &'a SokfSchema) -> Self {
debug_assert!(ctx.scaffold);
SchemaHost {
check: SokfHost::new(ctx, schema, None),
scaffold: SokfScaffold::new(schema),
}
}
}
impl FrontmatterHost for SchemaHost<'_> {
fn scalar_format(&self, name: &str, value: &str) -> Option<Result<(), String>> {
self.check.scalar_format(name, value)
}
fn reserved_keys(&self) -> &[&str] {
self.check.reserved_keys()
}
}
impl Host for SchemaHost<'_> {
fn dialect(&self) -> Dialect {
self.check.dialect()
}
fn image(&self, reference: &str) -> Subject {
self.check.image(reference)
}
fn region(&self, target: &str) -> Subject {
self.check.region(target)
}
fn scaffold_front(&self, states: &States) -> Option<ScaffoldFront> {
self.scaffold.scaffold_front(states)
}
fn check(
&self,
doc: &engine::DocView<'_>,
states: &States,
_options: CheckOptions,
locator: &Locator,
out: &mut CheckOutput,
) {
let Some(frontmatter) = doc.frontmatter else {
return;
};
if matches!(self.check.schema.dispatch, Dispatch::Globs(_)) {
return;
}
let core = core_fields(Some(Ok(frontmatter)), doc.path);
out.findings.extend(
fm_check::check_core_fields(&core, doc.path)
.into_iter()
.filter(|f| f.severity == Severity::Error),
);
link_rules::check(
doc.path,
&core,
self.check.schema,
states,
self.check.ctx,
locator,
out,
);
}
}
pub fn check_against(
view: DocView<'_>,
schema: &SokfSchema,
states: &States,
ctx: &Ctx<'_>,
locator: Locator,
) -> CheckOutput {
let host = SokfHost::new(ctx, schema, view.core);
let options = CheckOptions {
scaffold: ctx.scaffold,
};
engine::check_against(view.engine(), &schema.core, states, &host, options, locator)
}