Skip to main content

ass_editor/utils/validator/
result.rs

1//! Aggregated validation results.
2//!
3//! Defines `ValidationResult`, which collects validation issues along with
4//! summary statistics and optional timing/caching metadata.
5
6use super::{ValidationIssue, ValidationSeverity};
7
8#[cfg(not(feature = "std"))]
9use alloc::{
10    format,
11    string::{String, ToString},
12    vec::Vec,
13};
14
15#[cfg(feature = "std")]
16use std::time::Instant;
17
18/// Validation results with caching and statistics
19#[derive(Debug, Clone)]
20pub struct ValidationResult {
21    /// All validation issues found
22    pub issues: Vec<ValidationIssue>,
23
24    /// Time taken for validation in microseconds
25    #[cfg(feature = "std")]
26    pub validation_time_us: u64,
27
28    /// Whether the document passed validation
29    pub is_valid: bool,
30
31    /// Number of warnings found
32    pub warning_count: usize,
33
34    /// Number of errors found
35    pub error_count: usize,
36
37    /// Validation timestamp for cache invalidation
38    #[cfg(feature = "std")]
39    pub timestamp: Instant,
40}
41
42impl ValidationResult {
43    /// Create a new validation result
44    pub fn new(issues: Vec<ValidationIssue>) -> Self {
45        let warning_count = issues
46            .iter()
47            .filter(|i| i.severity == ValidationSeverity::Warning)
48            .count();
49        let error_count = issues.iter().filter(|i| i.is_error()).count();
50        let is_valid = error_count == 0;
51
52        Self {
53            issues,
54            #[cfg(feature = "std")]
55            validation_time_us: 0,
56            is_valid,
57            warning_count,
58            error_count,
59            #[cfg(feature = "std")]
60            timestamp: Instant::now(),
61        }
62    }
63
64    /// Filter issues by severity
65    pub fn issues_with_severity(&self, min_severity: ValidationSeverity) -> Vec<&ValidationIssue> {
66        self.issues
67            .iter()
68            .filter(|i| i.severity >= min_severity)
69            .collect()
70    }
71
72    /// Get summary statistics
73    pub fn summary(&self) -> String {
74        if self.is_valid {
75            if self.warning_count > 0 {
76                format!("{} warnings", self.warning_count)
77            } else {
78                "Valid".to_string()
79            }
80        } else {
81            format!(
82                "{} errors, {} warnings",
83                self.error_count, self.warning_count
84            )
85        }
86    }
87}