fraiseql_core/validation/
elo_rust_integration.rs1use std::{collections::HashMap, sync::Arc};
9
10use parking_lot::RwLock;
11
12#[derive(Debug, Clone)]
14pub struct RustValidatorRegistryConfig {
15 pub enabled: bool,
17 pub cache_validators: bool,
19 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#[derive(Debug, Clone)]
35pub struct EloRustValidator {
36 pub name: String,
38 pub elo_expression: String,
40 pub generated_code: Option<String>,
42}
43
44#[derive(Clone)]
46pub struct RustValidatorRegistry {
47 config: Arc<RustValidatorRegistryConfig>,
48 validators: Arc<RwLock<HashMap<String, EloRustValidator>>>,
49}
50
51impl RustValidatorRegistry {
52 #[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 pub fn register(&self, validator: EloRustValidator) {
63 let mut validators = self.validators.write();
64 validators.insert(validator.name.clone(), validator);
65 }
66
67 #[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 #[must_use]
76 pub fn exists(&self, name: &str) -> bool {
77 let validators = self.validators.read();
78 validators.contains_key(name)
79 }
80
81 pub fn remove(&self, name: &str) {
83 let mut validators = self.validators.write();
84 validators.remove(name);
85 }
86
87 #[must_use]
89 pub fn count(&self) -> usize {
90 let validators = self.validators.read();
91 validators.len()
92 }
93
94 #[must_use]
96 pub fn is_empty(&self) -> bool {
97 let validators = self.validators.read();
98 validators.is_empty()
99 }
100
101 #[must_use]
103 pub fn list_all(&self) -> Vec<EloRustValidator> {
104 let validators = self.validators.read();
105 validators.values().cloned().collect()
106 }
107
108 pub fn clear(&self) {
110 let mut validators = self.validators.write();
111 validators.clear();
112 }
113
114 #[must_use]
116 pub fn is_enabled(&self) -> bool {
117 self.config.enabled
118 }
119
120 #[must_use]
122 pub fn config(&self) -> &RustValidatorRegistryConfig {
123 &self.config
124 }
125}