use std::{
collections::{BTreeMap, BTreeSet},
path::PathBuf,
};
use tramlines_md::{
check::{self, Counts, HeadingMatch},
derive,
};
use crate::{
Cache, CacheMode, Document, Error, Model, RepoPath, RepoRoot, Result, Spanned, Yaml, config,
grammar::{self, Label, LimitKind, Section, SokfSchema, States, squash},
graph,
model::{ParseMode, parse_source},
scaffold, source,
};
#[derive(Debug, Clone)]
pub struct GuideOptions {
pub documents: Vec<String>,
pub states: Vec<(String, String)>,
pub cwd: PathBuf,
pub cache: CacheMode,
}
mod report;
pub use report::*;
pub fn guide(root: &RepoRoot, opts: &GuideOptions) -> Result<GuideReport> {
let config = config::load_config(root)?;
let (grammar, repo) = super::load::load_formats(root)?;
let cache = Cache::open(root, opts.cache);
let (mut model, keys) = if opts.documents.iter().any(|d| graph::is_key_reference(d)) {
let slugs = super::locate::key_slugs(&opts.documents, graph::is_key_reference);
let (model, keys) = super::load_keys_for(root, &config, &cache, &slugs)?;
(model, Some(keys))
} else {
(super::load_for_lookup(root, &config, &cache)?, None)
};
let mut documents = Vec::new();
for arg in &opts.documents {
let path = graph::resolve_document(&model, keys.as_ref(), &opts.cwd, arg)?;
documents.push(read(root, &path)?);
}
let types: BTreeSet<&str> = documents.iter().map(|(_, t)| t.as_str()).collect();
for type_name in types {
if type_name != "Schema" {
super::admit_schemas_of_type(&mut model, root, &cache, type_name)?;
}
}
let texts = config::load_texts(root)?;
let compiled = grammar::compile_all(&model, grammar, &repo, texts);
let mut guides = Vec::new();
for (doc, type_name) in &documents {
let (schema, schema_path, k) = schema_of(&model, &compiled, doc, type_name)?;
let language = scaffold::language(&model, &compiled, schema, type_name, Some(k), Some(k));
let words = scaffold::Words::new(&compiled.texts, language);
let mut states = schema.core.states(doc.frontmatter.as_ref());
let given = scaffold::given_states(schema, &opts.states)
.map_err(|e| Error::Guide(format!("{}: {e}", doc.path)))?;
states.0.extend(given.0);
own_states(doc, schema, &states)?;
guides.push(document_guide(
doc,
type_name,
schema,
schema_path,
&states,
&words,
));
}
Ok(GuideReport { documents: guides })
}
fn read(root: &RepoRoot, path: &RepoPath) -> Result<(Document, String)> {
let src = source::load(root, path)?;
let parsed = parse_source(&src, ParseMode::Document);
let doc = Document::new(src, parsed);
let Some(type_name) = doc.type_name().map(str::to_string) else {
return Err(Error::Guide(format!(
"{path} has no `type`, or its frontmatter does not parse, so it has no schema"
)));
};
Ok((doc, type_name))
}
fn schema_of<'a>(
model: &Model,
compiled: &'a grammar::Compiled,
doc: &Document,
type_name: &str,
) -> Result<(&'a SokfSchema, Option<RepoPath>, usize)> {
let path = &doc.path;
let Some(k) = model.knowledge_of(path) else {
return Err(Error::Guide(format!("{path} lies in no knowledge store")));
};
let (_, schema) =
scaffold::schema_for(model, compiled, type_name, Some(k)).map_err(|e| match e {
Error::New(why) => Error::Guide(format!("{path}: {why}")),
e => e,
})?;
let schema_path = compiled
.for_type(model, k, type_name)
.filter(|_| type_name != "Schema")
.map(|(r, _)| model.schema(r).doc.path.clone());
Ok((schema, schema_path, k))
}
fn own_states(doc: &Document, schema: &SokfSchema, states: &States) -> Result<()> {
for dim in &schema.core.core.lifecycle {
let key = &dim.key;
let why = match states.0.get(key).and_then(|v| v.first()) {
Some(state) if dim.states.contains(state) => continue,
Some(state) => format!(
"`{key}: {state}` is not a state of `{key}`. The states are {}",
dim.states.join(", ")
),
None => format!("the frontmatter sets no `{key}`"),
};
return Err(Error::Guide(format!(
"{}: {why}, so give `--state {key}=STATE`",
doc.path
)));
}
Ok(())
}
fn document_guide(
doc: &Document,
type_name: &str,
schema: &SokfSchema,
schema_path: Option<RepoPath>,
states: &States,
words: &scaffold::Words<'_>,
) -> DocumentGuide {
let host = scaffold::SokfScaffold::with_words(schema, words);
let (g, plain) = guides(doc, schema, states, &host);
let (frontmatter_text, body_text) = texts(doc);
let key_example = |key: &str| {
plain
.frontmatter
.iter()
.find(|k| k.key == key)
.and_then(|k| k.example.clone())
};
let admitted: Vec<usize> = (0..schema.core.sections.len())
.filter(|&i| schema.core.sections[i].common.only.admits(states))
.collect();
let matches = check::match_headings(crate::check::doc_view(doc).engine(), &schema.core, states);
let opening: Vec<String> = g
.guidance
.iter()
.chain(&g.bans)
.map(|t| squash(t))
.filter(|t| !t.is_empty())
.collect();
DocumentGuide {
path: doc.path.clone(),
id: doc.id().map(str::to_string),
type_name: type_name.to_string(),
schema: schema_path,
states: schema
.core
.core
.lifecycle
.iter()
.filter_map(|d| {
let state = states.0.get(&d.key)?.first()?;
Some((d.key.clone(), state.clone().into()))
})
.collect(),
guidance: g.guidance,
bans: g.bans,
text: (!opening.is_empty()).then(|| opening.join(" ")),
labels: g
.labels
.into_iter()
.map(|l| LabelGuide {
meaning: words.attribution(&l.key),
key: l.key,
words: l.words,
})
.collect(),
frontmatter: g
.frontmatter
.into_iter()
.map(|mut k| {
k.example = key_example(&k.key);
key_guide(k, words)
})
.collect(),
links: schema
.written_links(states)
.map(|r| LinkGuide {
rel: r.rel.clone(),
optional: r.min == 0,
limit: r.max.clone().map(limit),
text: scaffold::link_text(r, Some(words)),
example: scaffold::link_example(r),
})
.collect(),
preamble: g.preamble.map(|p| PreambleGuide {
guidance: p.guidance,
text: p.text,
example: p.example,
blocks: p.blocks.into_iter().map(block_guide).collect(),
line: p.line,
}),
sections: g
.sections
.into_iter()
.enumerate()
.map(|(i, s)| {
let section = admitted[i];
debug_assert_eq!(schema.core.sections[section].common.line, s.line);
let held = holds(doc, &schema.core.sections, &matches, section);
SectionGuide {
heading: s.heading,
level: s.level,
rule_id: s.rule_id,
parent: s.parent,
optional: s.optional,
repeat: s.repeat,
held,
guidance: s.guidance,
text: s.text,
example: s.example,
blocks: s.blocks.into_iter().map(block_guide).collect(),
line: s.line,
}
})
.collect(),
frontmatter_text,
body_text,
}
}
fn texts(doc: &Document) -> (String, String) {
let split = source::split(&doc.source);
let front = split.frontmatter.map_or("", |(yaml, _)| yaml);
(front.to_string(), split.body.to_string())
}
fn guides(
doc: &Document,
schema: &SokfSchema,
states: &States,
host: &scaffold::SokfScaffold<'_>,
) -> (derive::Guide, derive::Guide) {
let mut values = scalar_values(doc);
if derive::derive_for(&schema.core, states, host, &values).is_err() {
values.clear();
}
let own: BTreeMap<String, String> = values
.iter()
.filter(|(k, _)| matches!(k.as_str(), "id" | "title"))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
(
derive::guide_for(&schema.core, states, host, &values),
derive::guide_for(&schema.core, states, host, &own),
)
}
fn scalar_values(doc: &Document) -> BTreeMap<String, String> {
let Some(Spanned {
value: Yaml::Map(entries),
..
}) = &doc.frontmatter
else {
return BTreeMap::new();
};
entries
.iter()
.filter(|(_, v)| !matches!(v.value, Yaml::Null | Yaml::Seq(_) | Yaml::Map(_)))
.filter_map(|(k, v)| Some((k.value.clone(), v.scalar_text()?)))
.filter(|(_, v)| !v.trim().is_empty())
.collect()
}
#[expect(
clippy::too_many_arguments,
reason = "the guide of a new document needs what `new` resolved, field by field"
)]
pub(super) fn new_guide(
path: &RepoPath,
text: &str,
type_name: &str,
schema: &SokfSchema,
model: &Model,
compiled: &grammar::Compiled,
knowledge: Option<usize>,
states: &States,
words: &scaffold::Words<'_>,
) -> DocumentGuide {
let source = crate::Source::from_text(path.clone(), text);
let parsed = parse_source(&source, ParseMode::Document);
let doc = Document::new(source, parsed);
let schema_path = model
.knowledge_of(path)
.or(knowledge)
.and_then(|k| compiled.for_type(model, k, type_name))
.filter(|_| type_name != "Schema")
.map(|(r, _)| model.schema(r).doc.path.clone());
document_guide(&doc, type_name, schema, schema_path, states, words)
}
fn key_guide(k: derive::KeyGuide, words: &scaffold::Words<'_>) -> KeyGuide {
let text = match (host_text(&k.key, words), k.text) {
(Some(host), Some(own)) => Some(format!("{host} {own}")),
(host, own) => host.or(own),
};
KeyGuide {
lifecycle: k.lifecycle,
optional: k.optional,
guidance: k.guidance,
limit: k.limit.map(limit),
text,
example: k.example,
line: k.line,
key: k.key,
}
}
fn host_text(key: &str, words: &scaffold::Words<'_>) -> Option<String> {
(key == "description")
.then(|| words.description())
.flatten()
}
fn block_guide(b: derive::BlockGuide) -> BlockGuide {
BlockGuide {
kind: b.kind.name().to_string(),
rule_id: b.rule_id,
pos: b.pos,
optional: b.optional,
guidance: b.guidance,
limits: b
.limits
.into_iter()
.map(|l| RuleLimit {
rule: l.rule,
format: l.format,
rule_id: l.rule_id,
counts: l.counts.map(|c| match c {
Counts::Units => GuideCounts::Units,
Counts::Lines => GuideCounts::Lines,
}),
line: l.line,
limit: limit(l.limit),
})
.collect(),
labels: b
.labels
.into_iter()
.map(|l| RuleLabel {
rule: l.rule,
rule_id: l.rule_id,
line: l.line,
label: match l.label {
Label::Required => GuideLabel::Required,
Label::Optional => GuideLabel::Optional,
},
})
.collect(),
bans: b.bans,
text: b.text,
example: b.example,
blocks: b.blocks.into_iter().map(block_guide).collect(),
line: b.line,
}
}
fn limit(l: grammar::Limit) -> GuideLimit {
GuideLimit {
kind: match l.kind {
LimitKind::Soft => GuideLimitKind::Soft,
LimitKind::Hard => GuideLimitKind::Hard,
},
count: l.count,
when_at_limit: l.when_at_limit,
}
}
fn holds(doc: &Document, sections: &[Section], matches: &[Option<HeadingMatch>], i: usize) -> bool {
(0..doc.body.headings.len()).any(|h| takes(doc, sections, matches, i, h))
}
fn takes(
doc: &Document,
sections: &[Section],
matches: &[Option<HeadingMatch>],
i: usize,
h: usize,
) -> bool {
if !crate::check::headings::opens(doc, sections, matches, h, i) {
return false;
}
let headings = &doc.body.headings;
let Some(parent) = §ions[i].parent else {
return true;
};
(0..sections.len())
.filter(|&p| sections[p].rule_id.as_deref() == Some(parent.as_str()))
.any(|p| {
let level = sections[p].level;
(0..h)
.rev()
.find(|&j| headings[j].level <= level)
.is_some_and(|j| takes(doc, sections, matches, p, j))
})
}
#[cfg(test)]
mod tests;