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