use super::*;
pub(super) fn verdict(bad: usize) -> ExitCode {
if bad == 0 {
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}
pub(super) fn microlith_findings(path: &Path, text: &str) -> Vec<Finding> {
spec::check(text)
.into_iter()
.map(|v| {
Finding::new(path, &format!("microlith/{}", v.rule), v.msg)
.at(v.line)
})
.collect()
}
fn dangling(root: &Path, text: &str) -> Vec<(usize, 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((
c.line,
format!(
"`{}:{}` names no node -- \
a citation is a path from the root, `.` for root itself",
c.owner, c.id
),
));
continue;
};
if !spec::declares(&owner_spec, &c.id) {
out.push((
c.line,
format!("`{}:{}` resolves to no row", c.owner, c.id),
));
}
}
out
}
pub(super) fn file_findings(root: &Path) -> Vec<Finding> {
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);
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(
Finding::new(
rel_path,
"sherd/V50",
format!(
"{half} {n} tok over {ceiling} -- the node \
carries more than one worker can hold (judgment)"
),
)
.advisory(),
);
}
}
}
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 node_findings(
root: &Path,
node: &Path,
path: &Path,
text: &str,
) -> Vec<Finding> {
let mut out = microlith_findings(path, text);
for (id, cause) in spec::unreflected_bugs(text) {
out.push(
Finding::new(
path,
"sherd/spec:V4",
format!(
"{id} names no invariant -- `{cause}` will recur (advisory)"
),
)
.advisory(),
);
}
for (line, msg) in dangling(root, text) {
out.push(Finding::new(path, "sherd/spec:V7", msg).at(line));
}
for (id, task) in spec::completed_tasks(text) {
out.push(
Finding::new(
path,
"sherd/fed:V9",
format!(
"{id} is done -- `{task}` is history, and §T states \
remaining work (advisory)"
),
)
.advisory(),
);
}
out.extend(federation_findings(node, path, text));
out.extend(why_findings(node, text));
out
}
fn why_findings(node: &Path, text: &str) -> Vec<Finding> {
let path = node.join("SPEC.why.md");
let Ok(why) = std::fs::read_to_string(&path) else {
return Vec::new();
};
let (missing, orphan) = spec::why_gaps(text, &why);
let missing = missing.iter().map(|id| {
Finding::new(
&path,
"sherd/V44",
format!(
"{id} has no row -- rationale is kept by reference, and `-` \
is an answer"
),
)
});
let orphan = orphan.iter().map(|id| {
Finding::new(
&path,
"sherd/V44",
format!("{id} answers no rule in SPEC.md"),
)
});
missing.chain(orphan).collect()
}
fn federation_findings(node: &Path, path: &Path, text: &str) -> Vec<Finding> {
let edges = fed::edges(text);
let (dupes, missing) = fed::find_exhaustive_violations(&edges, node);
let dupes = dupes.into_iter().map(|e| {
Finding::new(
path,
"sherd/fed:V12",
format!("`{}` named twice in §F -- descent is ambiguous", e.dir),
)
});
let missing = missing.into_iter().map(|m| {
let name = m.file_name().unwrap_or_default().to_string_lossy();
Finding::new(
path,
"sherd/fed:V11",
format!(
"`{name}/` exists on disk with no §F row -- unreachable by \
descent (advisory)"
),
)
.advisory()
});
dupes.chain(missing).collect()
}
pub(super) fn unread(path: &Path, e: &std::io::Error) -> Finding {
Finding::new(
path,
"sherd/V48",
format!(
"cannot read -- {e}. a node discovered & not read is a failure, \
not a node with nothing wrong"
),
)
}
pub(super) struct CheckReport {
pub nodes: usize,
pub findings: Vec<Finding>,
pub files: Vec<Finding>,
pub rs_files: usize,
}
pub(super) fn check_report(root: &Path) -> CheckReport {
let nodes = fed::discover(root);
let mut findings = Vec::new();
for node in &nodes {
let path = node.join("SPEC.md");
match std::fs::read_to_string(&path) {
Ok(text) => {
findings.extend(node_findings(root, node, &path, &text))
}
Err(e) => findings.push(unread(&path, &e)),
}
}
CheckReport {
nodes: nodes.len(),
findings,
files: file_findings(root),
rs_files: fed::rust_files(root).len(),
}
}
#[cfg(test)]
pub(super) fn check(root: &Path) -> ExitCode {
check_as(root, false)
}
pub(super) fn check_as(root: &Path, json: bool) -> ExitCode {
let c = check_report(root);
let bad = fatal(&c.findings);
if json {
println!("{}", check_json(root, &c));
return verdict(bad);
}
print_findings(&c.findings);
print_findings(&c.files);
println!(
"\n {} .rs files measured against V50 · {} over ceiling (advisory)",
c.rs_files,
c.files.len()
);
println!("\n {} nodes examined · {bad} violations", c.nodes);
verdict(bad)
}
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)]
pub(super) fn dangling_citations(root: &Path, text: &str) -> Vec<String> {
dangling(root, text)
.into_iter()
.map(|(line, msg)| format!("{line}: sherd/spec:V7: {msg}"))
.collect()
}
#[cfg(test)]
pub(super) fn file_ceilings(root: &Path) -> Vec<String> {
file_findings(root).iter().map(Finding::text).collect()
}
#[cfg(test)]
#[path = "tests/check.rs"]
mod tests;