Skip to main content

lib3mf_core/validation/
report.rs

1use serde::{Deserialize, Serialize};
2
3/// Severity level of a validation finding.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum ValidationSeverity {
6    /// A spec violation or structural error that prevents correct interpretation.
7    Error,
8    /// A non-fatal issue that may affect interoperability or quality.
9    Warning,
10    /// Informational note (advisory, not a problem).
11    Info,
12}
13
14/// A single validation finding with severity, code, message, and optional context.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct ValidationItem {
17    /// Severity level of this finding.
18    pub severity: ValidationSeverity,
19    /// Unique numeric code identifying the type of validation failure.
20    pub code: u32, // Unique error code
21    /// Human-readable description of the issue.
22    pub message: String,
23    /// Optional suggested fix or remediation.
24    pub suggestion: Option<String>,
25    /// Optional context string identifying which object/resource is affected (e.g., `"Object 5"`).
26    pub context: Option<String>, // e.g., "Object 5"
27}
28
29/// Collection of validation findings produced by `Model::validate()`.
30#[derive(Debug, Clone, Default, Serialize, Deserialize)]
31pub struct ValidationReport {
32    /// All findings from the validation run.
33    pub items: Vec<ValidationItem>,
34}
35
36impl ValidationReport {
37    /// Creates a new empty `ValidationReport`.
38    pub fn new() -> Self {
39        Self::default()
40    }
41
42    /// Adds an error-severity finding to the report.
43    pub fn add_error(&mut self, code: u32, msg: impl Into<String>) {
44        self.items.push(ValidationItem {
45            severity: ValidationSeverity::Error,
46            code,
47            message: msg.into(),
48            suggestion: None,
49            context: None,
50        });
51    }
52
53    /// Adds a warning-severity finding to the report.
54    pub fn add_warning(&mut self, code: u32, msg: impl Into<String>) {
55        self.items.push(ValidationItem {
56            severity: ValidationSeverity::Warning,
57            code,
58            message: msg.into(),
59            suggestion: None,
60            context: None,
61        });
62    }
63
64    /// Adds an info-severity finding to the report.
65    pub fn add_info(&mut self, code: u32, msg: impl Into<String>) {
66        self.items.push(ValidationItem {
67            severity: ValidationSeverity::Info,
68            code,
69            message: msg.into(),
70            suggestion: None,
71            context: None,
72        });
73    }
74
75    /// Returns `true` if the report contains any error-severity findings.
76    pub fn has_errors(&self) -> bool {
77        self.items
78            .iter()
79            .any(|i| i.severity == ValidationSeverity::Error)
80    }
81}