use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationConfig {
pub level: ValidationLevel,
pub providers: Vec<String>,
pub custom_options: std::collections::HashMap<String, Vec<CustomOption>>,
pub suggestion_threshold: i64,
}
impl Default for ValidationConfig {
fn default() -> Self {
Self {
level: ValidationLevel::Normal,
providers: vec!["cargo".to_string(), "leptos".to_string()],
custom_options: std::collections::HashMap::new(),
suggestion_threshold: 30,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ValidationLevel {
Off,
Minimal,
Normal,
Strict,
}
impl ValidationLevel {
pub fn is_enabled(self) -> bool {
!matches!(self, Self::Off)
}
pub fn is_strict(self) -> bool {
matches!(self, Self::Strict)
}
pub fn allows_unknown(self) -> bool {
matches!(self, Self::Off | Self::Minimal | Self::Normal)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomOption {
pub name: String,
#[serde(rename = "type")]
pub option_type: String,
pub description: Option<String>,
pub values: Option<Vec<String>>,
pub conflicts_with: Option<Vec<String>>,
pub requires: Option<Vec<String>>,
}
impl ValidationConfig {
pub fn with_level(level: ValidationLevel) -> Self {
Self {
level,
..Default::default()
}
}
pub fn add_provider(mut self, provider: impl Into<String>) -> Self {
self.providers.push(provider.into());
self
}
pub fn with_suggestion_threshold(mut self, threshold: i64) -> Self {
self.suggestion_threshold = threshold;
self
}
pub fn is_provider_enabled(&self, provider: &str) -> bool {
self.providers.contains(&provider.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validation_level_methods() {
assert!(ValidationLevel::Normal.is_enabled());
assert!(!ValidationLevel::Off.is_enabled());
assert!(ValidationLevel::Strict.is_strict());
assert!(!ValidationLevel::Normal.is_strict());
assert!(ValidationLevel::Normal.allows_unknown());
assert!(!ValidationLevel::Strict.allows_unknown());
}
#[test]
fn test_config_defaults() {
let config = ValidationConfig::default();
assert_eq!(config.level, ValidationLevel::Normal);
assert!(config.providers.contains(&"cargo".to_string()));
}
#[test]
fn test_config_builder() {
let config = ValidationConfig::with_level(ValidationLevel::Strict)
.add_provider("dioxus")
.with_suggestion_threshold(50);
assert_eq!(config.level, ValidationLevel::Strict);
assert!(config.is_provider_enabled("dioxus"));
assert_eq!(config.suggestion_threshold, 50);
}
}