use crate::unified_quality::metrics::Metrics;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QualityEvent {
FileAdded { path: PathBuf, metrics: Metrics },
FileRemoved {
path: PathBuf,
last_metrics: Metrics,
},
MetricsUpdated {
path: PathBuf,
old_metrics: Metrics,
new_metrics: Metrics,
},
ThresholdViolated {
path: PathBuf,
violation: ThresholdViolation,
},
QualityImproved {
path: PathBuf,
old_score: f64,
new_score: f64,
},
QualityDegraded {
path: PathBuf,
old_score: f64,
new_score: f64,
},
BatchAnalysisComplete {
files_analyzed: usize,
violations_found: usize,
avg_quality_score: f64,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdViolation {
pub metric: String,
pub value: f64,
pub threshold: f64,
pub severity: ViolationSeverity,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ViolationSeverity {
Warning,
Error,
Critical,
}
impl QualityEvent {
#[must_use]
pub fn is_violation(&self) -> bool {
matches!(
self,
QualityEvent::ThresholdViolated { .. } | QualityEvent::QualityDegraded { .. }
)
}
#[must_use]
pub fn is_improvement(&self) -> bool {
matches!(self, QualityEvent::QualityImproved { .. })
}
#[must_use]
pub fn severity(&self) -> Option<ViolationSeverity> {
match self {
QualityEvent::ThresholdViolated { violation, .. } => Some(violation.severity.clone()),
QualityEvent::QualityDegraded {
old_score,
new_score,
..
} => {
let delta = old_score - new_score;
if delta > 0.3 {
Some(ViolationSeverity::Critical)
} else if delta > 0.1 {
Some(ViolationSeverity::Error)
} else {
Some(ViolationSeverity::Warning)
}
}
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(unused_imports)]
use std::time::SystemTime;
#[test]
fn test_event_is_violation() {
let event = QualityEvent::ThresholdViolated {
path: PathBuf::from("test.rs"),
violation: ThresholdViolation {
metric: "complexity".to_string(),
value: 25.0,
threshold: 20.0,
severity: ViolationSeverity::Error,
},
};
assert!(event.is_violation());
assert!(!event.is_improvement());
}
#[test]
fn test_event_severity() {
let event = QualityEvent::QualityDegraded {
path: PathBuf::from("test.rs"),
old_score: 0.9,
new_score: 0.5,
};
match event.severity() {
Some(ViolationSeverity::Critical) => (),
_ => panic!("Expected critical severity"),
}
}
}