use super::{Finding, FixOp, Report, Severity};
pub(crate) const LISTED_WARNINGS: [&str; 2] = [STALE_PART, ABSENT_PART];
pub(crate) const STALE_PART: &str = "CLI-4_AC-40";
pub(crate) const ABSENT_PART: &str = "CLI-4_AC-42";
pub fn message(f: &Finding) -> String {
format!("{} ({})", f.message, f.criterion)
}
pub fn text(report: &Report, warnings: bool) -> String {
let mut out = String::new();
for fix in &report.fixes {
match fix {
FixOp::Moved { from, to } => out.push_str(&format!("moved {from} -> {to}\n")),
FixOp::Created { path } => out.push_str(&format!("created {path}\n")),
FixOp::Rewrote { path } => out.push_str(&format!("rewrote {path}\n")),
}
}
let mut findings: Vec<_> = report.errors.iter().collect();
findings.extend(
report
.warnings
.iter()
.filter(|f| warnings || LISTED_WARNINGS.contains(&f.criterion.as_str())),
);
let listed = findings.len() - report.errors.len();
if listed > 0 {
findings.sort_by(|a, b| (&a.path, a.line).cmp(&(&b.path, b.line)));
}
for f in findings {
let severity = match f.severity {
Severity::Error => "error",
Severity::Warning => "warning",
};
let message = message(f);
match f.line {
Some(line) => out.push_str(&format!("{}:{line}: {severity}: {message}\n", f.path)),
None => out.push_str(&format!("{}: {severity}: {message}\n", f.path)),
}
}
if !out.is_empty() {
out.push('\n');
}
let warnings = match report.warnings.len() {
0 => "0 warnings".to_string(),
all => format!("{listed} of {all} warnings listed"),
};
out.push_str(&format!(
"{}, {warnings}\n",
tramlines_core::text::count(report.errors.len(), "error", "errors")
));
out
}