use super::record::Diagnostic;
use crate::config::Level;
#[derive(Debug)]
pub struct Report<Id> {
pub diagnostics: Vec<Diagnostic<Id>>,
pub error_count: usize,
pub warning_count: usize,
}
impl<Id> Default for Report<Id> {
fn default() -> Self {
Self {
diagnostics: Vec::new(),
error_count: 0,
warning_count: 0,
}
}
}
impl<Id> Report<Id> {
#[must_use]
pub fn from_diagnostics(diagnostics: Vec<Diagnostic<Id>>) -> Self {
let error_count = diagnostics
.iter()
.filter(|d| d.level == Level::Error)
.count();
let warning_count = diagnostics
.iter()
.filter(|d| d.level == Level::Warn)
.count();
Self {
diagnostics,
error_count,
warning_count,
}
}
#[must_use]
pub fn exit_code(&self) -> i32 {
i32::from(self.error_count > 0)
}
#[must_use]
pub fn summary(&self, clean: &str) -> String {
match (self.error_count, self.warning_count) {
(0, 0) => clean.to_owned(),
(errs, 0) => format!("{errs} error{}", plural(errs)),
(0, warns) => format!("{warns} warning{}", plural(warns)),
(errs, warns) => format!(
"{} error{} · {} warning{}",
errs,
plural(errs),
warns,
plural(warns)
),
}
}
}
const fn plural(count: usize) -> &'static str {
if count == 1 { "" } else { "s" }
}