Skip to main content

dtcs/diagnostics/
report.rs

1//! Aggregated diagnostic report.
2
3use super::{Diagnostic, Severity};
4
5/// Collection of diagnostics produced during parsing or validation.
6#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
7pub struct DiagnosticReport {
8    /// Diagnostics emitted during processing.
9    pub diagnostics: Vec<Diagnostic>,
10}
11
12/// Alias aligned with implementation guides.
13pub type ValidationReport = DiagnosticReport;
14
15impl DiagnosticReport {
16    /// Creates an empty report.
17    #[must_use]
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Appends a diagnostic.
23    pub fn push(&mut self, diagnostic: Diagnostic) {
24        self.diagnostics.push(diagnostic);
25    }
26
27    /// Merges another report into this one.
28    pub fn merge(&mut self, other: DiagnosticReport) {
29        self.diagnostics.extend(other.diagnostics);
30    }
31
32    /// Returns `true` when no error-level diagnostics are present.
33    #[must_use]
34    pub fn is_valid(&self) -> bool {
35        !self.diagnostics.iter().any(|d| d.severity.is_error())
36    }
37
38    /// Returns diagnostics at or above the given severity.
39    #[must_use]
40    pub fn with_min_severity(&self, min: Severity) -> Vec<&Diagnostic> {
41        self.diagnostics
42            .iter()
43            .filter(|d| d.severity >= min)
44            .collect()
45    }
46
47    /// Returns error-level diagnostics.
48    #[must_use]
49    pub fn errors(&self) -> Vec<&Diagnostic> {
50        self.with_min_severity(Severity::Error)
51    }
52}