Skip to main content

fraiseql_core/validation/
elo_rust_integration.rs

1//! Elo Rust target integration for compiled validators.
2//!
3//! Provides infrastructure for compiling Elo expressions to Rust validators,
4//! caching compiled validators, and executing them with <1µs latency targets.
5//!
6//! Elo is an expression language by Bernard Lambeau: <https://elo-lang.org/>
7
8use std::{collections::HashMap, sync::Arc};
9
10use parking_lot::RwLock;
11
12/// Configuration for the Rust validator registry
13#[derive(Debug, Clone)]
14pub struct RustValidatorRegistryConfig {
15    /// Enable ELO Rust validator compilation and caching
16    pub enabled:          bool,
17    /// Cache compiled validators for reuse
18    pub cache_validators: bool,
19    /// Maximum number of validators to cache
20    pub max_cache_size:   usize,
21}
22
23impl Default for RustValidatorRegistryConfig {
24    fn default() -> Self {
25        Self {
26            enabled:          true,
27            cache_validators: true,
28            max_cache_size:   1000,
29        }
30    }
31}
32
33/// A single ELO Rust validator with generated code
34#[derive(Debug, Clone)]
35pub struct EloRustValidator {
36    /// Name/identifier of the validator
37    pub name:           String,
38    /// ELO expression source
39    pub elo_expression: String,
40    /// Generated Rust code (if compiled)
41    pub generated_code: Option<String>,
42}
43
44/// Registry for managing ELO Rust validators
45#[derive(Clone)]
46pub struct RustValidatorRegistry {
47    config:     Arc<RustValidatorRegistryConfig>,
48    validators: Arc<RwLock<HashMap<String, EloRustValidator>>>,
49}
50
51impl RustValidatorRegistry {
52    /// Create a new validator registry with the given configuration
53    #[must_use]
54    pub fn new(config: RustValidatorRegistryConfig) -> Self {
55        Self {
56            config:     Arc::new(config),
57            validators: Arc::new(RwLock::new(HashMap::new())),
58        }
59    }
60
61    /// Register a new validator
62    pub fn register(&self, validator: EloRustValidator) {
63        let mut validators = self.validators.write();
64        validators.insert(validator.name.clone(), validator);
65    }
66
67    /// Get a registered validator by name
68    #[must_use]
69    pub fn get(&self, name: &str) -> Option<EloRustValidator> {
70        let validators = self.validators.read();
71        validators.get(name).cloned()
72    }
73
74    /// Check if a validator exists
75    #[must_use]
76    pub fn exists(&self, name: &str) -> bool {
77        let validators = self.validators.read();
78        validators.contains_key(name)
79    }
80
81    /// Remove a validator by name
82    pub fn remove(&self, name: &str) {
83        let mut validators = self.validators.write();
84        validators.remove(name);
85    }
86
87    /// Get the number of registered validators
88    #[must_use]
89    pub fn count(&self) -> usize {
90        let validators = self.validators.read();
91        validators.len()
92    }
93
94    /// Check if registry is empty
95    #[must_use]
96    pub fn is_empty(&self) -> bool {
97        let validators = self.validators.read();
98        validators.is_empty()
99    }
100
101    /// List all validators
102    #[must_use]
103    pub fn list_all(&self) -> Vec<EloRustValidator> {
104        let validators = self.validators.read();
105        validators.values().cloned().collect()
106    }
107
108    /// Clear all validators
109    pub fn clear(&self) {
110        let mut validators = self.validators.write();
111        validators.clear();
112    }
113
114    /// Check if registry is enabled
115    #[must_use]
116    pub fn is_enabled(&self) -> bool {
117        self.config.enabled
118    }
119
120    /// Get configuration reference
121    #[must_use]
122    pub fn config(&self) -> &RustValidatorRegistryConfig {
123        &self.config
124    }
125}