Skip to main content

voce_validator/
errors.rs

1//! Typed validation error definitions.
2//!
3//! Every validation diagnostic includes a severity, error code, human-readable
4//! message, node path, and the pass that produced it. Error codes are prefixed
5//! by category (e.g., `A11Y001`, `SEC003`, `SEO002`).
6
7use std::fmt;
8
9/// Severity level for validation results.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Severity {
12    /// Blocks compilation. Must be fixed.
13    Error,
14    /// Reported but compilation proceeds.
15    Warning,
16}
17
18impl fmt::Display for Severity {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Severity::Error => write!(f, "error"),
22            Severity::Warning => write!(f, "warning"),
23        }
24    }
25}
26
27/// A single validation diagnostic.
28#[derive(Debug, Clone)]
29pub struct Diagnostic {
30    /// Severity level.
31    pub severity: Severity,
32    /// Error code (e.g., "A11Y001", "SEC003", "STR001").
33    pub code: String,
34    /// Human-readable message describing the issue and how to fix it.
35    pub message: String,
36    /// Path to the offending node (e.g., "/root/children/2/semantic").
37    pub node_path: String,
38    /// Validation pass that produced this diagnostic.
39    pub pass: String,
40    /// Optional fix suggestion.
41    pub hint: Option<String>,
42}
43
44impl fmt::Display for Diagnostic {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(
47            f,
48            "{} [{}] {}: {} (at {})",
49            self.severity, self.pass, self.code, self.message, self.node_path
50        )
51    }
52}
53
54/// Result of running all validation passes on an IR blob.
55#[derive(Debug, Default)]
56pub struct ValidationResult {
57    /// All diagnostics from all passes.
58    pub diagnostics: Vec<Diagnostic>,
59}
60
61impl ValidationResult {
62    /// Returns true if any diagnostic has Error severity.
63    pub fn has_errors(&self) -> bool {
64        self.diagnostics
65            .iter()
66            .any(|d| d.severity == Severity::Error)
67    }
68
69    /// Count of Error-severity diagnostics.
70    pub fn error_count(&self) -> usize {
71        self.diagnostics
72            .iter()
73            .filter(|d| d.severity == Severity::Error)
74            .count()
75    }
76
77    /// Count of Warning-severity diagnostics.
78    pub fn warning_count(&self) -> usize {
79        self.diagnostics
80            .iter()
81            .filter(|d| d.severity == Severity::Warning)
82            .count()
83    }
84}