use std::fmt::Write as _;
use super::{Finding, FixOp, Report, Severity};
pub(crate) const LISTED_WARNINGS: [&str; 4] = [
STALE_PART,
ABSENT_PART,
crate::formatter::WARNING,
LEFTOVER_PACKS,
];
pub(crate) const STALE_PART: &str = "CLI-4_AC-40";
pub(crate) const ABSENT_PART: &str = "CLI-4_AC-42";
pub(crate) const LEFTOVER_PACKS: &str = "CLI-4_AC-104";
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 } => {
let _ = writeln!(out, "moved {from} -> {to}");
}
FixOp::Created { path } => {
let _ = writeln!(out, "created {path}");
}
FixOp::Rewrote { path } => {
let _ = writeln!(out, "rewrote {path}");
}
}
}
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) => {
let _ = writeln!(out, "{}:{line}: {severity}: {message}", f.path);
}
None => {
let _ = writeln!(out, "{}: {severity}: {message}", 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"),
};
let _ = writeln!(
out,
"{}, {warnings}",
tramlines_core::text::count(report.errors.len(), "error", "errors")
);
out
}