raz_validation/
lib.rs

1//! Smart options validation system for raz
2//!
3//! This crate provides a pluggable validation system that can validate command-line options
4//! for different tools and frameworks (cargo, leptos, dioxus, etc.) with helpful error messages
5//! and suggestions.
6
7pub mod error;
8pub mod provider;
9pub mod registry;
10pub mod suggestion;
11pub mod validation;
12
13pub use error::{ValidationError, ValidationResult};
14pub use provider::{OptionDef, OptionProvider, OptionValueType, ValueValidator};
15pub use registry::ValidationRegistry;
16pub use validation::{ValidationConfig, ValidationLevel};
17
18use std::collections::HashMap;
19
20/// Main validation engine
21pub struct ValidationEngine {
22    registry: ValidationRegistry,
23    config: ValidationConfig,
24}
25
26impl Default for ValidationEngine {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl ValidationEngine {
33    /// Create a new validation engine with default providers
34    pub fn new() -> Self {
35        let mut registry = ValidationRegistry::new();
36
37        // Register built-in providers
38        registry.register_provider(Box::new(provider::cargo::CargoProvider::new()));
39
40        Self {
41            registry,
42            config: ValidationConfig::default(),
43        }
44    }
45
46    /// Create with custom configuration
47    pub fn with_config(config: ValidationConfig) -> Self {
48        let mut engine = Self::new();
49        engine.config = config;
50        engine
51    }
52
53    /// Validate a single option for a given command
54    pub fn validate_option(
55        &self,
56        command: &str,
57        option: &str,
58        value: Option<&str>,
59    ) -> ValidationResult<()> {
60        if self.config.level == ValidationLevel::Off {
61            return Ok(());
62        }
63
64        self.registry
65            .validate_option(command, option, value, &self.config)
66    }
67
68    /// Validate multiple options for a command
69    pub fn validate_options(
70        &self,
71        command: &str,
72        options: &HashMap<String, Option<String>>,
73    ) -> ValidationResult<()> {
74        for (option, value) in options {
75            self.validate_option(command, option, value.as_deref())?;
76        }
77        Ok(())
78    }
79
80    /// Get suggestions for a misspelled option
81    pub fn suggest_option(&self, command: &str, option: &str) -> Vec<String> {
82        self.registry.suggest_option(command, option)
83    }
84
85    /// Get all valid options for a command
86    pub fn get_options(&self, command: &str) -> Vec<OptionDef> {
87        self.registry.get_options(command)
88    }
89
90    /// Register a new provider
91    pub fn register_provider(&mut self, provider: Box<dyn OptionProvider>) {
92        self.registry.register_provider(provider);
93    }
94}