Skip to main content

fastskill_core/validation/
result.rs

1//! Shared validation result types for skill validation.
2
3use serde::{Deserialize, Serialize};
4
5/// Validation result
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ValidationResult {
8    /// Whether validation passed
9    pub is_valid: bool,
10
11    /// List of validation errors
12    pub errors: Vec<ValidationError>,
13
14    /// List of validation warnings
15    pub warnings: Vec<ValidationWarning>,
16
17    /// Validation score (0.0 to 1.0)
18    pub score: f64,
19}
20
21impl ValidationResult {
22    /// Create a valid result
23    pub fn valid() -> Self {
24        Self {
25            is_valid: true,
26            errors: Vec::new(),
27            warnings: Vec::new(),
28            score: 1.0,
29        }
30    }
31
32    /// Create an invalid result
33    pub fn invalid(error: &str) -> Self {
34        Self {
35            is_valid: false,
36            errors: vec![ValidationError {
37                field: "general".to_string(),
38                message: error.to_string(),
39                severity: ErrorSeverity::Error,
40            }],
41            warnings: Vec::new(),
42            score: 0.0,
43        }
44    }
45
46    /// Add an error
47    pub fn with_error(mut self, field: &str, message: &str, severity: ErrorSeverity) -> Self {
48        self.errors.push(ValidationError {
49            field: field.to_string(),
50            message: message.to_string(),
51            severity,
52        });
53        self.is_valid = false;
54        self.score = 0.0;
55        self
56    }
57
58    /// Add a warning
59    pub fn with_warning(mut self, field: &str, message: &str) -> Self {
60        self.warnings.push(ValidationWarning {
61            field: field.to_string(),
62            message: message.to_string(),
63        });
64        self
65    }
66
67    /// Calculate quality score based on errors and warnings
68    pub(crate) fn calculate_score(&mut self) {
69        let error_penalty = self.errors.len() as f64 * 0.3;
70        let warning_penalty = self.warnings.len() as f64 * 0.1;
71
72        self.score = (1.0 - error_penalty - warning_penalty).max(0.0);
73    }
74}
75
76/// Validation error details
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct ValidationError {
79    /// Field or component that failed validation
80    pub field: String,
81
82    /// Error message
83    pub message: String,
84
85    /// Error severity level
86    pub severity: ErrorSeverity,
87}
88
89/// Error severity levels
90#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
91pub enum ErrorSeverity {
92    /// Warning - not critical but should be addressed
93    Warning,
94    /// Error - validation failed but skill might still work
95    Error,
96    /// Critical - validation failed and skill cannot be used
97    Critical,
98}
99
100/// Validation warning details
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct ValidationWarning {
103    /// Field or component with warning
104    pub field: String,
105
106    /// Warning message
107    pub message: String,
108}