raz_validation/
validation.rs

1//! Validation configuration and levels
2
3use serde::{Deserialize, Serialize};
4
5/// Configuration for validation behavior
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ValidationConfig {
8    /// Validation level
9    pub level: ValidationLevel,
10
11    /// Enabled providers
12    pub providers: Vec<String>,
13
14    /// Custom options for specific commands
15    pub custom_options: std::collections::HashMap<String, Vec<CustomOption>>,
16
17    /// Minimum similarity score for suggestions
18    pub suggestion_threshold: i64,
19}
20
21impl Default for ValidationConfig {
22    fn default() -> Self {
23        Self {
24            level: ValidationLevel::Normal,
25            providers: vec!["cargo".to_string(), "leptos".to_string()],
26            custom_options: std::collections::HashMap::new(),
27            suggestion_threshold: 30,
28        }
29    }
30}
31
32/// Validation strictness levels
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "lowercase")]
35pub enum ValidationLevel {
36    /// No validation - allow any options
37    Off,
38
39    /// Minimal validation - only check known conflicts
40    Minimal,
41
42    /// Normal validation - validate known options, warn on unknown
43    Normal,
44
45    /// Strict validation - reject unknown options
46    Strict,
47}
48
49impl ValidationLevel {
50    /// Check if this level performs validation
51    pub fn is_enabled(self) -> bool {
52        !matches!(self, Self::Off)
53    }
54
55    /// Check if this level is strict
56    pub fn is_strict(self) -> bool {
57        matches!(self, Self::Strict)
58    }
59
60    /// Check if this level allows unknown options
61    pub fn allows_unknown(self) -> bool {
62        matches!(self, Self::Off | Self::Minimal | Self::Normal)
63    }
64}
65
66/// Custom option definition for configuration
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct CustomOption {
69    /// Option name
70    pub name: String,
71
72    /// Option type
73    #[serde(rename = "type")]
74    pub option_type: String,
75
76    /// Description
77    pub description: Option<String>,
78
79    /// Valid values (for enum types)
80    pub values: Option<Vec<String>>,
81
82    /// Conflicts with these options
83    pub conflicts_with: Option<Vec<String>>,
84
85    /// Requires these options
86    pub requires: Option<Vec<String>>,
87}
88
89impl ValidationConfig {
90    /// Create a new config with specified level
91    pub fn with_level(level: ValidationLevel) -> Self {
92        Self {
93            level,
94            ..Default::default()
95        }
96    }
97
98    /// Add a provider to the config
99    pub fn add_provider(mut self, provider: impl Into<String>) -> Self {
100        self.providers.push(provider.into());
101        self
102    }
103
104    /// Set suggestion threshold
105    pub fn with_suggestion_threshold(mut self, threshold: i64) -> Self {
106        self.suggestion_threshold = threshold;
107        self
108    }
109
110    /// Check if a provider is enabled
111    pub fn is_provider_enabled(&self, provider: &str) -> bool {
112        self.providers.contains(&provider.to_string())
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_validation_level_methods() {
122        assert!(ValidationLevel::Normal.is_enabled());
123        assert!(!ValidationLevel::Off.is_enabled());
124
125        assert!(ValidationLevel::Strict.is_strict());
126        assert!(!ValidationLevel::Normal.is_strict());
127
128        assert!(ValidationLevel::Normal.allows_unknown());
129        assert!(!ValidationLevel::Strict.allows_unknown());
130    }
131
132    #[test]
133    fn test_config_defaults() {
134        let config = ValidationConfig::default();
135        assert_eq!(config.level, ValidationLevel::Normal);
136        assert!(config.providers.contains(&"cargo".to_string()));
137    }
138
139    #[test]
140    fn test_config_builder() {
141        let config = ValidationConfig::with_level(ValidationLevel::Strict)
142            .add_provider("dioxus")
143            .with_suggestion_threshold(50);
144
145        assert_eq!(config.level, ValidationLevel::Strict);
146        assert!(config.is_provider_enabled("dioxus"));
147        assert_eq!(config.suggestion_threshold, 50);
148    }
149}