zipora 3.1.2

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Generic Factory Pattern Implementation
//!
//! Provides a flexible, type-safe factory system for object creation with automatic
//! registration, discovery, and efficient runtime creation. Inspired by production
//! infrastructure patterns while leveraging Rust's ownership and trait systems.

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::{Arc, RwLock, OnceLock};
use std::marker::PhantomData;
use crate::error::{ZiporaError, Result};

/// Type-erased creator function that can create objects of type T
pub type Creator<T> = Box<dyn Fn() -> Result<T> + Send + Sync>;

/// Registry for factory creators, providing thread-safe registration and creation
pub struct FactoryRegistry<T> {
    creators: RwLock<HashMap<String, Creator<T>>>,
    type_map: RwLock<HashMap<TypeId, String>>,
    _phantom: PhantomData<T>,
}

impl<T> Default for FactoryRegistry<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> FactoryRegistry<T> {
    /// Create a new factory registry
    pub fn new() -> Self {
        Self {
            creators: RwLock::new(HashMap::new()),
            type_map: RwLock::new(HashMap::new()),
            _phantom: PhantomData,
        }
    }

    /// Register a creator function with a given name
    pub fn register<F>(&self, name: &str, creator: F) -> Result<()>
    where
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        let mut creators = self.creators.write()
            .map_err(|_| ZiporaError::io_error("Failed to acquire write lock on creators"))?;
        
        if creators.contains_key(name) {
            return Err(ZiporaError::invalid_data(&format!("Creator '{}' already registered", name)));
        }
        
        creators.insert(name.to_string(), Box::new(creator));
        Ok(())
    }

    /// Register a creator function with automatic type name derivation
    pub fn register_type<U, F>(&self, creator: F) -> Result<()>
    where
        U: 'static,
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        let type_name = std::any::type_name::<U>();
        let type_id = TypeId::of::<U>();
        
        // Store type mapping
        {
            let mut type_map = self.type_map.write()
                .map_err(|_| ZiporaError::io_error("Failed to acquire write lock on type_map"))?;
            type_map.insert(type_id, type_name.to_string());
        }
        
        self.register(type_name, creator)
    }

    /// Create an object by name
    pub fn create(&self, name: &str) -> Result<T> {
        let creators = self.creators.read()
            .map_err(|_| ZiporaError::io_error("Failed to acquire read lock on creators"))?;
        
        let creator = creators.get(name)
            .ok_or_else(|| ZiporaError::not_found(&format!("Creator '{}' not found", name)))?;
        
        creator()
    }

    /// Create an object by type
    pub fn create_by_type<U: 'static>(&self) -> Result<T> {
        let type_id = TypeId::of::<U>();
        
        let type_map = self.type_map.read()
            .map_err(|_| ZiporaError::io_error("Failed to acquire read lock on type_map"))?;
        
        let type_name = type_map.get(&type_id)
            .ok_or_else(|| ZiporaError::not_found(&format!("Type '{}' not registered", std::any::type_name::<U>())))?.clone();
        
        drop(type_map); // Release lock before calling create
        self.create(&type_name)
    }

    /// List all registered creator names
    pub fn list_creators(&self) -> Result<Vec<String>> {
        let creators = self.creators.read()
            .map_err(|_| ZiporaError::io_error("Failed to acquire read lock on creators"))?;
        
        Ok(creators.keys().cloned().collect())
    }

    /// Get the number of registered creators
    pub fn creator_count(&self) -> Result<usize> {
        let creators = self.creators.read()
            .map_err(|_| ZiporaError::io_error("Failed to acquire read lock on creators"))?;
        
        Ok(creators.len())
    }

    /// Check if a creator is registered
    #[inline]
    pub fn contains(&self, name: &str) -> Result<bool> {
        let creators = self.creators.read()
            .map_err(|_| ZiporaError::io_error("Failed to acquire read lock on creators"))?;
        
        Ok(creators.contains_key(name))
    }
}

/// Global factory registry for a specific type
pub struct GlobalFactory<T> {
    registry: OnceLock<FactoryRegistry<T>>,
    _phantom: PhantomData<T>,
}

impl<T> Default for GlobalFactory<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> GlobalFactory<T> {
    /// Create a new global factory
    pub const fn new() -> Self {
        Self {
            registry: OnceLock::new(),
            _phantom: PhantomData,
        }
    }

    /// Get the global registry, initializing it if necessary
    fn registry(&self) -> &FactoryRegistry<T> {
        self.registry.get_or_init(|| FactoryRegistry::new())
    }

    /// Register a creator function globally
    pub fn register<F>(&self, name: &str, creator: F) -> Result<()>
    where
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        self.registry().register(name, creator)
    }

    /// Register a creator function with automatic type name derivation
    pub fn register_type<U, F>(&self, creator: F) -> Result<()>
    where
        U: 'static,
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        self.registry().register_type::<U, F>(creator)
    }

    /// Create an object by name
    pub fn create(&self, name: &str) -> Result<T> {
        self.registry().create(name)
    }

    /// Create an object by type
    pub fn create_by_type<U: 'static>(&self) -> Result<T> {
        self.registry().create_by_type::<U>()
    }

    /// List all registered creator names
    pub fn list_creators(&self) -> Result<Vec<String>> {
        self.registry().list_creators()
    }

    /// Get the number of registered creators
    pub fn creator_count(&self) -> Result<usize> {
        self.registry().creator_count()
    }

    /// Check if a creator is registered
    #[inline]
    pub fn contains(&self, name: &str) -> Result<bool> {
        self.registry().contains(name)
    }
}

/// Auto-registration helper for static initialization
pub struct AutoRegister<T> {
    _phantom: PhantomData<T>,
}

impl<T: Send + Sync + 'static> AutoRegister<T> {
    /// Register a creator function with the global factory
    pub fn new<F>(name: &str, creator: F) -> Self
    where
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        // SAFETY: Static initialization - panic on registration failure is appropriate
        // because the program cannot continue without successfully registered creators
        global_factory::<T>().register(name, creator)
            .expect(&format!("Failed to register creator '{}'", name));
        
        Self {
            _phantom: PhantomData,
        }
    }

    /// Register a creator function with automatic type name derivation
    pub fn new_type<U, F>(creator: F) -> Self
    where
        U: 'static,
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        // SAFETY: Static initialization - panic on registration failure is appropriate
        // because the program cannot continue without successfully registered type creators
        global_factory::<T>().register_type::<U, F>(creator)
            .expect(&format!("Failed to register creator for type '{}'", std::any::type_name::<U>()));
        
        Self {
            _phantom: PhantomData,
        }
    }
}

/// Get the global factory instance for type T
pub fn global_factory<T: Send + Sync + 'static>() -> &'static GlobalFactory<T> {
    use std::sync::Mutex;
    static FACTORIES: std::sync::LazyLock<Mutex<HashMap<TypeId, Box<dyn Any + Send + Sync>>>> =
        std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));

    let type_id = TypeId::of::<T>();

    // First, try to get an existing factory
    {
        let factories = FACTORIES.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(factory_any) = factories.get(&type_id) {
            let factory_ref = factory_any.downcast_ref::<&'static GlobalFactory<T>>()
                .expect("registered factory type");
            return *factory_ref;
        }
    }

    // If not found, create and leak a new one (lives for program duration)
    {
        let mut factories = FACTORIES.lock().unwrap_or_else(|e| e.into_inner());
        // Double-check pattern in case another thread created it while we were waiting for the lock
        if let Some(factory_any) = factories.get(&type_id) {
            let factory_ref = factory_any.downcast_ref::<&'static GlobalFactory<T>>()
                .expect("registered factory type");
            return *factory_ref;
        }

        // Box::leak gives us a genuinely &'static reference — no transmute needed
        let leaked: &'static GlobalFactory<T> = Box::leak(Box::new(GlobalFactory::<T>::new()));
        factories.insert(type_id, Box::new(leaked) as Box<dyn Any + Send + Sync>);
        leaked
    }
}

/// Trait for types that can be created by factories
pub trait Factoryable: Sized {
    /// Create an instance by name using the global factory
    fn create(name: &str) -> Result<Self>;
    
    /// List all available creator names
    fn list_creators() -> Result<Vec<String>>;
    
    /// Check if a creator is available
    fn has_creator(name: &str) -> Result<bool>;
}

/// Implement Factoryable for any type
impl<T: Send + Sync + 'static> Factoryable for T {
    fn create(name: &str) -> Result<Self> {
        global_factory::<T>().create(name)
    }
    
    fn list_creators() -> Result<Vec<String>> {
        global_factory::<T>().list_creators()
    }
    
    fn has_creator(name: &str) -> Result<bool> {
        global_factory::<T>().contains(name)
    }
}

/// Macro for convenient factory registration
#[macro_export]
macro_rules! register_factory {
    ($type:ty, $name:expr, $creator:expr) => {
        static _FACTORY_REG: $crate::dev_infrastructure::factory::AutoRegister<$type> = 
            $crate::dev_infrastructure::factory::AutoRegister::new($name, $creator);
    };
}

/// Macro for factory registration with automatic type name
#[macro_export]
macro_rules! register_factory_type {
    ($impl_type:ty, $trait_type:ty, $creator:expr) => {
        static _FACTORY_REG: $crate::dev_infrastructure::factory::AutoRegister<$trait_type> = 
            $crate::dev_infrastructure::factory::AutoRegister::new_type::<$impl_type, _>($creator);
    };
}

/// Builder pattern for complex object creation
pub struct FactoryBuilder<T> {
    name: String,
    registry: Arc<FactoryRegistry<T>>,
}

impl<T> FactoryBuilder<T> {
    /// Create a new factory builder
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            registry: Arc::new(FactoryRegistry::new()),
        }
    }

    /// Add a creator to the builder
    pub fn with_creator<F>(self, name: &str, creator: F) -> Result<Self>
    where
        F: Fn() -> Result<T> + Send + Sync + 'static,
    {
        self.registry.register(name, creator)?;
        Ok(self)
    }

    /// Build the factory and return the registry
    pub fn build(self) -> Arc<FactoryRegistry<T>> {
        self.registry
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Test types
    struct TestObject {
        value: i32,
    }

    impl TestObject {
        fn new(value: i32) -> Self {
            Self { value }
        }
    }

    trait TestTrait {
        fn get_value(&self) -> i32;
    }

    impl TestTrait for TestObject {
        fn get_value(&self) -> i32 {
            self.value
        }
    }

    #[test]
    fn test_factory_registry_basic() {
        let registry = FactoryRegistry::new();
        
        // Register a creator
        registry.register("test", || Ok(TestObject::new(42))).unwrap();
        
        // Create an object
        let obj = registry.create("test").unwrap();
        assert_eq!(obj.value, 42);
        
        // Check registry properties
        assert_eq!(registry.creator_count().unwrap(), 1);
        assert!(registry.contains("test").unwrap());
        assert!(!registry.contains("nonexistent").unwrap());
        
        let creators = registry.list_creators().unwrap();
        assert_eq!(creators.len(), 1);
        assert!(creators.contains(&"test".to_string()));
    }

    #[test]
    fn test_factory_registry_type_registration() {
        let registry = FactoryRegistry::new();
        
        // Register by type
        registry.register_type::<TestObject, _>(|| Ok(TestObject::new(99))).unwrap();
        
        // Create by type
        let obj = registry.create_by_type::<TestObject>().unwrap();
        assert_eq!(obj.value, 99);
    }

    #[test]
    fn test_global_factory() {
        let factory = global_factory::<TestObject>();
        
        // Register a creator
        factory.register("global_test", || Ok(TestObject::new(123))).unwrap();
        
        // Create an object
        let obj = factory.create("global_test").unwrap();
        assert_eq!(obj.value, 123);
        
        // Test Factoryable trait
        TestObject::create("global_test").unwrap();
        assert!(TestObject::has_creator("global_test").unwrap());
    }

    #[test]
    fn test_factory_builder() {
        let factory = FactoryBuilder::new("test_factory")
            .with_creator("builder_test", || Ok(TestObject::new(456))).unwrap()
            .build();
        
        let obj = factory.create("builder_test").unwrap();
        assert_eq!(obj.value, 456);
    }

    #[test]
    fn test_factory_errors() {
        let registry = FactoryRegistry::new();
        
        // Test creating non-existent object
        assert!(registry.create("nonexistent").is_err());
        
        // Test duplicate registration
        registry.register("duplicate", || Ok(TestObject::new(1))).unwrap();
        assert!(registry.register("duplicate", || Ok(TestObject::new(2))).is_err());
    }

    #[test]
    fn test_trait_objects() {
        let registry = FactoryRegistry::<Box<dyn TestTrait>>::new();
        
        registry.register("trait_obj", || {
            Ok(Box::new(TestObject::new(789)) as Box<dyn TestTrait>)
        }).unwrap();
        
        let obj = registry.create("trait_obj").unwrap();
        assert_eq!(obj.get_value(), 789);
    }
}