use super::{Finding, FixOp, Report, Severity};
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();
if warnings {
findings.extend(report.warnings.iter());
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)),
}
}
let listed = if warnings { report.warnings.len() } else { 0 };
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
}