mod census;
mod cycle;
mod law;
mod roll;
use std::collections::HashSet;
pub use census::{Census, Standing};
pub use cycle::{condensation, tangles};
pub use law::dir_of;
pub use roll::{Dormant, Roll};
pub(crate) use law::inside;
use crate::ordinance::{Law, Ordinance};
use crate::survey::Survey;
#[derive(Clone)]
pub struct Finding {
pub law: Law,
pub path: String,
pub line: usize,
pub col: usize,
pub width: usize,
pub message: String,
pub subject: String,
}
pub struct Verdict {
pub package: String,
pub findings: Vec<Finding>,
pub ratified: Vec<(Finding, String)>,
pub stale: Vec<String>,
pub dormant: Vec<String>,
pub census: Census,
}
impl Verdict {
#[must_use]
pub fn ok(&self) -> bool {
self.findings.is_empty() && self.stale.is_empty()
}
}
#[must_use]
pub fn judge(survey: &Survey, ordinance: &Ordinance) -> Verdict {
let mut bench = Bench {
survey,
ordinance,
findings: Vec::new(),
ratified: Vec::new(),
stale: Vec::new(),
dormant: Vec::new(),
used: HashSet::new(),
};
bench.zones();
bench.seals();
bench.keeps();
bench.cycles();
bench.reach();
bench.uses();
bench.escapes();
let unused: Vec<String> = ordinance
.variances
.iter()
.filter(|v| !bench.used.contains(&(v.law, v.subject.clone())))
.map(|v| format!("variance {} {} ({}: {})", v.law, v.subject, v.source, v.reason))
.collect();
bench.stale.extend(unused);
let file =
ordinance.path.file_name().map_or_else(String::new, |n| n.to_string_lossy().into_owned());
let idle: Vec<String> = ordinance
.exclude
.iter()
.filter(|p| !survey.spent.contains(p.as_str()))
.map(|p| format!("exclude {p} ({file}: holds back no file)"))
.collect();
bench.stale.extend(idle);
Verdict {
package: ordinance.package.clone(),
census: census::take(survey, ordinance),
findings: bench.findings,
ratified: bench.ratified,
stale: bench.stale,
dormant: bench.dormant,
}
}
struct Bench<'a> {
survey: &'a Survey,
ordinance: &'a Ordinance,
findings: Vec<Finding>,
ratified: Vec<(Finding, String)>,
stale: Vec<String>,
dormant: Vec<String>,
used: HashSet<(Law, String)>,
}
impl Bench<'_> {
fn record(&mut self, law: Law, path: &str, line: usize, message: String, subject: String) {
let (col, width) = self.survey.span_at(path, line);
let finding = Finding {
law,
path: self.survey.rel(path),
line,
col,
width,
message,
subject: subject.clone(),
};
match self.ordinance.variance(law, &subject) {
Some(variance) => {
self.used.insert((law, subject));
self.ratified.push((finding, variance.reason.clone()));
}
None => self.findings.push(finding),
}
}
fn unwaivable(&mut self, law: Law, path: &str, message: String, subject: String) {
self.findings.push(Finding {
law,
path: self.survey.rel(path),
line: 1,
col: 1,
width: 1,
message,
subject,
});
}
}