use super::*;
pub(super) fn validate(root: &Path) -> ExitCode {
let nodes = fed::discover(root);
let structural = validate_specs(&nodes);
let edges = validate_edges(root);
let over = validate_ceilings(root, &nodes);
let drift = validate_drift(root);
println!(
"\n {} nodes · {structural} structural · {edges} edge · \
{over} over ceiling · {drift} drifted",
nodes.len()
);
if structural + edges + over + drift == 0 {
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}
pub(super) fn validate_specs(nodes: &[PathBuf]) -> usize {
let mut bad: usize = 0;
for node in nodes {
let path = node.join("SPEC.md");
let Ok(text) = std::fs::read_to_string(&path) else {
continue;
};
for v in spec::check(&text) {
println!("{}:{}: {v}", path.display(), v.line);
bad = bad.saturating_add(1);
}
}
bad
}
pub(super) fn validate_drift(root: &Path) -> usize {
if !root.join(".sherd-slices").exists() {
println!("slice: no registry (none required)");
return 0;
}
match slice::drifted(root) {
Ok(drifted) => report_drift(&drifted),
Err(e) => {
println!("slice: {e}");
1
}
}
}
pub(super) fn report_drift(drifted: &[PathBuf]) -> usize {
for p in drifted {
println!("{}: slice drifted from its source", p.display());
}
drifted.len()
}
pub(super) fn validate_edges(root: &Path) -> usize {
let mut bad: usize = 0;
for node in fed::discover(root) {
let Ok(text) = std::fs::read_to_string(node.join("SPEC.md")) else {
continue;
};
let edges = fed::edges(&text);
for e in fed::depth_violations(&edges) {
println!("{}: edge to `{}` skips a level", node.display(), e.dir);
bad = bad.saturating_add(1);
}
}
bad
}
pub(super) fn validate_ceilings(root: &Path, nodes: &[PathBuf]) -> usize {
let over = nodes.iter().filter(|node| over_ceiling(root, node)).count();
if over > 0 && tokens::Ceilings::load(root).is_ok_and(|c| c.is_cold()) {
println!(
" ({over} over the {} tok default; set .context-limits to gate it)",
tokens::DEFAULT_NODE
);
return 0;
}
over
}
pub(super) fn over_ceiling(root: &Path, node: &Path) -> bool {
let (Ok(pack), Ok(ceiling)) = (
lens::pack(root, node, lens::Depth::Rule),
lens::ceiling_for(root, node),
) else {
return false;
};
if pack.cost.tokens > ceiling {
println!(
"{}: chain {} tok over its ceiling of {ceiling}",
node.display(),
pack.cost.tokens
);
return true;
}
false
}
pub(super) fn dangling_citations(root: &Path, text: &str) -> Vec<String> {
let mut out = Vec::new();
for c in spec::citations(text) {
let target = if c.owner == "." {
root.join("SPEC.md")
} else {
root.join(&c.owner).join("SPEC.md")
};
let Ok(owner_spec) = std::fs::read_to_string(&target) else {
out.push(format!(
"{}: sherd/spec:V7: `{}:{}` names no node -- \
a citation is a path from the root, `.` for root itself",
c.line, c.owner, c.id
));
continue;
};
if !spec::declares(&owner_spec, &c.id) {
out.push(format!(
"{}: sherd/spec:V7: `{}:{}` resolves to no row",
c.line, c.owner, c.id
));
}
}
out
}
pub(super) fn file_ceilings(root: &Path) -> Vec<String> {
let mut out = Vec::new();
for f in fed::rust_files(root) {
let Ok(src) = std::fs::read_to_string(&f) else {
continue;
};
let rel_path = f.strip_prefix(root).unwrap_or(&f);
let (impl_r, tests_r) = halves(rel_path, src);
let rel = rel_path.display().to_string();
for (half, text, ceiling) in [
("code", impl_r, crate::debt::CEILING_FILE),
("tests", tests_r, crate::debt::CEILING_TEST),
] {
let n = tokens::count(&text).tokens;
if n > ceiling {
out.push(format!(
"{rel}: sherd/V50: {half} {n} tok over {ceiling} -- \
the node carries more than one worker can hold (judgment)"
));
}
}
}
out
}
fn halves(rel: &Path, src: String) -> (String, String) {
if rel.components().any(|c| c.as_os_str() == "tests") {
(String::new(), src)
} else {
crate::code::split_regions(&src)
}
}
pub(super) fn check_node(
root: &Path,
node: &Path,
path: &Path,
text: &str,
) -> usize {
let mut bad = 0usize;
for v in spec::check(text) {
println!("{}:{}: {v}", path.display(), v.line);
bad = bad.saturating_add(1);
}
for (id, cause) in spec::unreflected_bugs(text) {
println!(
"{}: sherd/spec:V4: {id} names no invariant -- `{cause}` \
will recur (advisory)",
path.display()
);
}
for d in dangling_citations(root, text) {
println!("{}:{d}", path.display());
bad = bad.saturating_add(1);
}
for (id, task) in spec::completed_tasks(text) {
println!(
"{}: sherd/fed:V9: {id} is done -- `{task}` is history, and §T \
states remaining work (advisory)",
path.display()
);
}
bad.saturating_add(check_federation(node, path, text))
}
pub(super) fn check_federation(node: &Path, path: &Path, text: &str) -> usize {
let edges = fed::edges(text);
let (dupes, missing) = fed::find_exhaustive_violations(&edges, node);
let mut bad = 0usize;
for e in dupes {
println!(
"{}: sherd/fed:V12: `{}` named twice in §F -- descent is ambiguous",
path.display(),
e.dir
);
bad = bad.saturating_add(1);
}
for m in missing {
let name = m.file_name().unwrap_or_default().to_string_lossy();
println!(
"{}: sherd/fed:V11: `{name}/` exists on disk with no §F row -- \
unreachable by descent (advisory)",
path.display()
);
}
bad
}
pub(super) fn check(root: &Path) -> ExitCode {
let nodes = fed::discover(root);
let mut bad: usize = 0;
for node in &nodes {
let path = node.join("SPEC.md");
let Ok(text) = std::fs::read_to_string(&path) else {
continue;
};
bad = bad.saturating_add(check_node(root, node, &path, &text));
}
let over = file_ceilings(root);
for v in &over {
println!("{v}");
}
println!(
"\n {} .rs files measured against V50 · {} over ceiling (advisory)",
fed::rust_files(root).len(),
over.len()
);
println!("\n {} nodes examined · {bad} violations", nodes.len());
if bad > 0 {
ExitCode::from(1)
} else {
ExitCode::SUCCESS
}
}
pub(super) fn review_cmd(root: &Path, rev: &str) -> ExitCode {
match crate::review::commit(root, rev) {
Ok(fs) if fs.is_empty() => {
println!(
"{rev}: no findings (checked: {})",
crate::review::RULES.join(", ")
);
ExitCode::SUCCESS
}
Ok(fs) => {
for (file, f) in &fs {
println!(
"{}: sherd/review:{}: {}",
file.display(),
f.rule,
f.detail
);
}
println!("\n {} finding(s) -- ADVISORY. Read the diff.", fs.len());
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("sherd: {e}");
ExitCode::from(2)
}
}
}
#[cfg(test)]
#[path = "tests/check.rs"]
mod tests;