use super::*;
pub(super) const WINDOW: u64 = 131_072;
pub(super) fn record_debt(root: &Path, now: crate::debt::Measured) -> ExitCode {
match crate::debt::record(root, now) {
Ok(msg) => {
println!("{msg}");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("sherd: {e}");
ExitCode::from(1)
}
}
}
pub(super) fn coverage_cmd(
root: &Path,
mode: Option<&str>,
cargo: &str,
) -> ExitCode {
let Some(now) = crate::debt::coverage(root, cargo) else {
eprintln!(
"sherd: could not read a coverage total -- that is an ERROR, \
not a floor breach (sherd/tdd:V26)"
);
return ExitCode::from(2);
};
let Some(was) = crate::debt::recorded_floor(root) else {
let path = root.join(".coverage");
eprintln!("sherd: {}: no `lines` row to read", path.display());
return ExitCode::from(2);
};
if mode == Some("--record") {
return match crate::debt::record_coverage(root, now) {
Ok(msg) => {
println!("{msg}");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("sherd: {e}");
ExitCode::from(1)
}
};
}
let (ok, report) = crate::debt::coverage_verdict(now, was);
if ok {
println!("{report}");
return ExitCode::SUCCESS;
}
eprintln!("{report}");
ExitCode::from(1)
}
pub(super) fn debt_cmd(
root: &Path,
mode: Option<&str>,
cargo: &str,
) -> ExitCode {
let Some(now) = crate::debt::measure(root, cargo) else {
eprintln!(
"sherd: clippy did not COMPILE, so its count is not a \
measurement -- a target that fails to build emits no warnings \
at all (sherd/debt:V3)."
);
return ExitCode::from(2);
};
let Some(was) = crate::debt::recorded_ceilings(root) else {
let registry = root.join(".lint-debt");
if registry.is_file() {
eprintln!(
"sherd: {}: no `density` and `shape` rows to read",
registry.display()
);
} else {
eprintln!(
"sherd: {}: no ratchet here. `sherd debt` needs a `.lint-debt` \
carrying `density` and `shape`.",
registry.display()
);
}
return ExitCode::from(2);
};
if mode == Some("--record") {
return record_debt(root, now);
}
let (ok, report) = crate::debt::verdict(now, was);
if ok {
println!("{report}");
} else {
eprintln!("{report}");
}
if ok {
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}
pub(super) fn budget(root: &Path, dir: PathBuf) -> ExitCode {
let work = tokens::working(WINDOW);
println!(
"window {WINDOW} · entry {} · working {work}\n",
tokens::ENTRY_COST
);
let nodes = fed::discover(root);
let mut total = 0;
let mut examined = 0;
let mut over = 0;
for node in &nodes {
if !node.starts_with(&dir) {
continue;
}
let p = match lens::pack(root, node, lens::Depth::Rule) {
Ok(p) => p,
Err(e) => {
eprintln!("sherd: {}: {e}", node.display());
return ExitCode::from(1);
}
};
let ceiling = match lens::ceiling_for(root, node) {
Ok(c) => c,
Err(e) => {
eprintln!("sherd: {e}");
return ExitCode::from(1);
}
};
total += p.cost.tokens;
examined += 1;
let rel = node.strip_prefix(root).unwrap_or(node);
let name = if rel.as_os_str().is_empty() {
Path::new(".")
} else {
rel
};
let mark = match lens::verdict(p.cost.tokens, ceiling) {
lens::Verdict::Fits { .. } => String::new(),
lens::Verdict::Over { by } => {
over += 1;
format!(" OVER by {by}")
}
};
println!(
" {:<24} chain {:>6} tok ({} nodes) ceiling {ceiling:>6}{mark}",
name.display(),
p.cost.tokens,
p.chain.len()
);
}
println!(
"\n {examined} nodes examined · {total} tok if all chains loaded \
· {over} over ceiling"
);
if examined == 0 {
eprintln!("{}", no_node(root, &dir));
return ExitCode::from(2);
}
if tokens::Ceilings::load(root).is_ok_and(|c| c.is_cold()) {
println!(
" no .context-limits: ceilings are the {} tok default, and \
over is advisory until you set them",
tokens::DEFAULT_NODE
);
return ExitCode::SUCCESS;
}
if over > 0 {
ExitCode::from(1)
} else {
ExitCode::SUCCESS
}
}
pub(super) fn lens_cmd(
root: &Path,
dir: &Path,
depth: lens::Depth,
) -> ExitCode {
match lens::pack(root, dir, depth) {
Ok(p) => {
eprintln!("# chain: {} nodes · {}", p.chain.len(), p.cost);
for c in &p.children {
eprintln!(
"# -> {:<16} {} [not: {}]",
c.dir, c.owns, c.not_owns
);
}
print!("{}", p.text);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("sherd: {}: {e}", dir.display());
ExitCode::from(2)
}
}
}
pub(super) fn fed_cmd(dir: &Path) -> ExitCode {
let path = dir.join("SPEC.md");
let Ok(text) = std::fs::read_to_string(&path) else {
eprintln!("sherd: no SPEC.md at {}", dir.display());
return ExitCode::from(2);
};
let edges = fed::edges(&text);
for e in &edges {
println!("{:<16} {:<40} not: {}", e.dir, e.owns, e.not_owns);
}
if edges.is_empty() {
println!(
"{}: no §F table -- this node federates nothing. \
`sherd split` proposes where the boundaries are.",
dir.display()
);
}
ExitCode::SUCCESS
}
pub(super) fn route_cmd(root: &Path, query: &str) -> ExitCode {
match plan::route(root, query) {
plan::Route::Hit(node, why) => {
println!(
"{}\n matched: {}",
node.strip_prefix(root).unwrap_or(&node).display(),
why.join(", ")
);
ExitCode::SUCCESS
}
plan::Route::Miss => route_miss(root),
plan::Route::Ambiguous(nodes) => route_ambiguous(root, &nodes),
}
}
pub(super) fn route_miss(root: &Path) -> ExitCode {
if fed::discover(root).len() <= 1 {
println!(
"this repository has one node, so there is nothing to route to. \
`sherd init <dir>` starts a federation."
);
} else {
println!(
"no node matched. `sherd graph --table` lists what each one owns."
);
}
ExitCode::from(2)
}
pub(super) fn route_ambiguous(root: &Path, nodes: &[PathBuf]) -> ExitCode {
println!("ambiguous -- the query spans {} nodes:", nodes.len());
for n in nodes {
println!(" {}", n.strip_prefix(root).unwrap_or(n).display());
}
ExitCode::from(3)
}
#[cfg(test)]
#[path = "tests/measure.rs"]
mod tests;