raz_validation/
validation.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ValidationConfig {
8 pub level: ValidationLevel,
10
11 pub providers: Vec<String>,
13
14 pub custom_options: std::collections::HashMap<String, Vec<CustomOption>>,
16
17 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "lowercase")]
35pub enum ValidationLevel {
36 Off,
38
39 Minimal,
41
42 Normal,
44
45 Strict,
47}
48
49impl ValidationLevel {
50 pub fn is_enabled(self) -> bool {
52 !matches!(self, Self::Off)
53 }
54
55 pub fn is_strict(self) -> bool {
57 matches!(self, Self::Strict)
58 }
59
60 pub fn allows_unknown(self) -> bool {
62 matches!(self, Self::Off | Self::Minimal | Self::Normal)
63 }
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct CustomOption {
69 pub name: String,
71
72 #[serde(rename = "type")]
74 pub option_type: String,
75
76 pub description: Option<String>,
78
79 pub values: Option<Vec<String>>,
81
82 pub conflicts_with: Option<Vec<String>>,
84
85 pub requires: Option<Vec<String>>,
87}
88
89impl ValidationConfig {
90 pub fn with_level(level: ValidationLevel) -> Self {
92 Self {
93 level,
94 ..Default::default()
95 }
96 }
97
98 pub fn add_provider(mut self, provider: impl Into<String>) -> Self {
100 self.providers.push(provider.into());
101 self
102 }
103
104 pub fn with_suggestion_threshold(mut self, threshold: i64) -> Self {
106 self.suggestion_threshold = threshold;
107 self
108 }
109
110 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}