#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
Skipped,
Pass,
Warn,
Error,
}
impl Severity {
pub fn as_str(self) -> &'static str {
match self {
Self::Skipped => "skip",
Self::Pass => "pass",
Self::Warn => "warn",
Self::Error => "fail",
}
}
}
#[derive(Debug, Clone)]
pub struct CheckResult {
#[allow(dead_code)]
pub check_id: String,
pub check_name: String,
pub severity: Severity,
pub message: String,
pub suggestion: Option<String>,
pub location: Option<(String, Option<usize>)>,
}
impl CheckResult {
pub fn pass(check_id: &str, check_name: &str, message: impl Into<String>) -> Self {
Self {
check_id: check_id.to_string(),
check_name: check_name.to_string(),
severity: Severity::Pass,
message: message.into(),
suggestion: None,
location: None,
}
}
pub fn warn(
check_id: &str,
check_name: &str,
message: impl Into<String>,
suggestion: impl Into<String>,
) -> Self {
Self {
check_id: check_id.to_string(),
check_name: check_name.to_string(),
severity: Severity::Warn,
message: message.into(),
suggestion: Some(suggestion.into()),
location: None,
}
}
pub fn fail(
check_id: &str,
check_name: &str,
message: impl Into<String>,
suggestion: impl Into<String>,
) -> Self {
Self {
check_id: check_id.to_string(),
check_name: check_name.to_string(),
severity: Severity::Error,
message: message.into(),
suggestion: Some(suggestion.into()),
location: None,
}
}
pub fn skipped(check_id: &str, check_name: &str, message: impl Into<String>) -> Self {
Self {
check_id: check_id.to_string(),
check_name: check_name.to_string(),
severity: Severity::Skipped,
message: message.into(),
suggestion: None,
location: None,
}
}
pub fn is_critical_failure(&self) -> bool {
matches!(self.severity, Severity::Error)
}
}
#[derive(Debug, Default)]
pub struct VerifyReport {
pub results: Vec<CheckResult>,
}
impl VerifyReport {
pub fn push(&mut self, r: CheckResult) {
self.results.push(r);
}
pub fn has_critical_failure(&self) -> bool {
self.results.iter().any(CheckResult::is_critical_failure)
}
pub fn counts(&self) -> (usize, usize, usize, usize) {
let mut pass = 0;
let mut warn = 0;
let mut fail = 0;
let mut skip = 0;
for r in &self.results {
match r.severity {
Severity::Pass => pass += 1,
Severity::Warn => warn += 1,
Severity::Error => fail += 1,
Severity::Skipped => skip += 1,
}
}
(pass, warn, fail, skip)
}
}