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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Thread-safe registry for custom scalar implementations.
//!
//! This module provides a global registry for managing custom scalar implementations
//! at runtime, allowing applications to register their own scalar types.
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use super::custom_scalar::CustomScalar;
/// Thread-safe registry for custom scalar implementations.
///
/// Uses `Arc<RwLock<HashMap>>` for concurrent read access with exclusive write access.
pub struct CustomScalarRegistry {
scalars: Arc<RwLock<HashMap<String, Arc<dyn CustomScalar>>>>,
}
impl CustomScalarRegistry {
/// Create a new custom scalar registry.
#[must_use]
pub fn new() -> Self {
Self {
scalars: Arc::new(RwLock::new(HashMap::new())),
}
}
/// Register a custom scalar implementation.
///
/// # Arguments
///
/// * `scalar` - The scalar implementation to register
///
/// # Errors
///
/// Returns an error if a scalar with the same name is already registered.
///
/// # Example
///
/// ```
/// use fraiseql_core::validation::{CustomScalarRegistry, CustomScalar};
/// use fraiseql_core::error::Result;
/// use serde_json::Value;
/// use std::sync::Arc;
///
/// #[derive(Debug)]
/// struct Email;
/// impl CustomScalar for Email {
/// fn name(&self) -> &str { "Email" }
/// fn serialize(&self, v: &Value) -> Result<Value> { Ok(v.clone()) }
/// fn parse_value(&self, v: &Value) -> Result<Value> { Ok(v.clone()) }
/// fn parse_literal(&self, v: &Value) -> Result<Value> { Ok(v.clone()) }
/// }
///
/// let registry = CustomScalarRegistry::new();
/// registry.register(Arc::new(Email)).unwrap();
/// assert!(registry.has_scalar("Email").unwrap());
/// ```
pub fn register(&self, scalar: Arc<dyn CustomScalar>) -> crate::error::Result<()> {
let name = scalar.name().to_string();
let mut scalars = self.scalars.write().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire write lock on scalar registry")
})?;
if scalars.contains_key(&name) {
return Err(crate::error::FraiseQLError::validation(format!(
"Scalar \"{}\" is already registered",
name
)));
}
scalars.insert(name, scalar);
Ok(())
}
/// Get a registered scalar by name.
///
/// Returns `None` if the scalar is not registered.
///
/// # Errors
///
/// Returns `FraiseQLError::Internal` if the read lock cannot be acquired.
pub fn get_scalar(&self, name: &str) -> crate::error::Result<Option<Arc<dyn CustomScalar>>> {
let scalars = self.scalars.read().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire read lock on scalar registry")
})?;
Ok(scalars.get(name).cloned())
}
/// Check if a scalar is registered.
///
/// # Errors
///
/// Returns `FraiseQLError::Internal` if the read lock cannot be acquired.
pub fn has_scalar(&self, name: &str) -> crate::error::Result<bool> {
let scalars = self.scalars.read().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire read lock on scalar registry")
})?;
Ok(scalars.contains_key(name))
}
/// Get all registered scalar names.
///
/// # Errors
///
/// Returns `FraiseQLError::Internal` if the read lock cannot be acquired.
pub fn get_scalar_names(&self) -> crate::error::Result<Vec<String>> {
let scalars = self.scalars.read().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire read lock on scalar registry")
})?;
Ok(scalars.keys().cloned().collect())
}
/// Unregister a scalar by name (useful for testing).
///
/// # Errors
///
/// Returns `FraiseQLError::Internal` if the write lock cannot be acquired.
pub fn unregister(&self, name: &str) -> crate::error::Result<()> {
let mut scalars = self.scalars.write().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire write lock on scalar registry")
})?;
scalars.remove(name);
Ok(())
}
/// Clear all registered scalars (useful for testing).
///
/// # Errors
///
/// Returns `FraiseQLError::Internal` if the write lock cannot be acquired.
pub fn clear(&self) -> crate::error::Result<()> {
let mut scalars = self.scalars.write().map_err(|_| {
crate::error::FraiseQLError::internal("Failed to acquire write lock on scalar registry")
})?;
scalars.clear();
Ok(())
}
}
impl Default for CustomScalarRegistry {
fn default() -> Self {
Self::new()
}
}
impl Clone for CustomScalarRegistry {
fn clone(&self) -> Self {
Self {
scalars: Arc::clone(&self.scalars),
}
}
}