use serde::{Deserialize, Serialize};
use super::{Diagnostic, Severity};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ValidationReport {
pub diagnostics: Vec<Diagnostic>,
}
impl ValidationReport {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}
pub fn extend(&mut self, other: ValidationReport) {
self.diagnostics.extend(other.diagnostics);
}
pub fn is_valid(&self) -> bool {
!self
.diagnostics
.iter()
.any(|d| d.severity == Severity::Error)
}
pub fn errors(&self) -> impl Iterator<Item = &Diagnostic> {
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Error)
}
pub fn warnings(&self) -> impl Iterator<Item = &Diagnostic> {
self.diagnostics
.iter()
.filter(|d| d.severity == Severity::Warning)
}
pub fn error_count(&self) -> usize {
self.errors().count()
}
pub fn warning_count(&self) -> usize {
self.warnings().count()
}
pub fn sort_deterministic(&mut self) {
self.diagnostics.sort_by(|a, b| {
a.id.cmp(&b.id)
.then_with(|| a.object_ref.cmp(&b.object_ref))
.then_with(|| a.message.cmp(&b.message))
});
}
}