use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
pub is_valid: bool,
pub errors: Vec<ValidationError>,
pub warnings: Vec<ValidationWarning>,
pub score: f64,
}
impl ValidationResult {
pub fn valid() -> Self {
Self {
is_valid: true,
errors: Vec::new(),
warnings: Vec::new(),
score: 1.0,
}
}
pub fn invalid(error: &str) -> Self {
Self {
is_valid: false,
errors: vec![ValidationError {
field: "general".to_string(),
message: error.to_string(),
severity: ErrorSeverity::Error,
}],
warnings: Vec::new(),
score: 0.0,
}
}
pub fn with_error(mut self, field: &str, message: &str, severity: ErrorSeverity) -> Self {
self.errors.push(ValidationError {
field: field.to_string(),
message: message.to_string(),
severity,
});
self.is_valid = false;
self.score = 0.0;
self
}
pub fn with_warning(mut self, field: &str, message: &str) -> Self {
self.warnings.push(ValidationWarning {
field: field.to_string(),
message: message.to_string(),
});
self
}
pub(crate) fn calculate_score(&mut self) {
let error_penalty = self.errors.len() as f64 * 0.3;
let warning_penalty = self.warnings.len() as f64 * 0.1;
self.score = (1.0 - error_penalty - warning_penalty).max(0.0);
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationError {
pub field: String,
pub message: String,
pub severity: ErrorSeverity,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ErrorSeverity {
Warning,
Error,
Critical,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationWarning {
pub field: String,
pub message: String,
}