elif-core 0.7.1

Core architecture foundation for the elif.rs LLM-friendly web framework
Documentation
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
451
452
//! Advanced Binding Examples for IOC Phase 4
//!
//! This module demonstrates the advanced binding features including:
//! - Interface-to-implementation mapping
//! - Named services and tagging
//! - Conditional binding (environment, features, profiles)
//! - Factory patterns and lazy initialization
//! - Generic type support
//! - Collection resolution

use crate::container::{AdvancedBindingBuilder, IocContainer, ServiceBinder, ServiceScope};
use crate::errors::CoreError;
use std::collections::HashMap;

// Example interfaces and implementations

/// Cache interface for different storage backends
pub trait Cache: Send + Sync {
    fn get(&self, key: &str) -> Option<String>;
    fn set(&self, key: &str, value: String) -> Result<(), String>;
    fn delete(&self, key: &str) -> Result<(), String>;
}

/// Redis cache implementation
#[derive(Default)]
pub struct RedisCache {
    _config: String,
}

impl Cache for RedisCache {
    fn get(&self, key: &str) -> Option<String> {
        println!("RedisCache: Getting key '{}'", key);
        Some(format!("redis_value_{}", key))
    }

    fn set(&self, key: &str, value: String) -> Result<(), String> {
        println!("RedisCache: Setting '{}' = '{}'", key, value);
        Ok(())
    }

    fn delete(&self, key: &str) -> Result<(), String> {
        println!("RedisCache: Deleting key '{}'", key);
        Ok(())
    }
}

/// In-memory cache implementation
#[derive(Default)]
pub struct MemoryCache {
    storage: HashMap<String, String>,
}

impl Cache for MemoryCache {
    fn get(&self, key: &str) -> Option<String> {
        println!("MemoryCache: Getting key '{}'", key);
        self.storage.get(key).cloned()
    }

    fn set(&self, key: &str, value: String) -> Result<(), String> {
        println!("MemoryCache: Setting '{}' = '{}'", key, value);
        // Note: In real implementation, we'd need interior mutability
        Ok(())
    }

    fn delete(&self, key: &str) -> Result<(), String> {
        println!("MemoryCache: Deleting key '{}'", key);
        Ok(())
    }
}

/// Hybrid cache that uses both Redis and Memory
#[derive(Default)]
pub struct HybridCache;

impl Cache for HybridCache {
    fn get(&self, key: &str) -> Option<String> {
        println!(
            "HybridCache: Getting key '{}' (checking memory first, then Redis)",
            key
        );
        Some(format!("hybrid_value_{}", key))
    }

    fn set(&self, key: &str, value: String) -> Result<(), String> {
        println!(
            "HybridCache: Setting '{}' = '{}' (both memory and Redis)",
            key, value
        );
        Ok(())
    }

    fn delete(&self, key: &str) -> Result<(), String> {
        println!(
            "HybridCache: Deleting key '{}' (from both memory and Redis)",
            key
        );
        Ok(())
    }
}

/// Email service interface
pub trait EmailService: Send + Sync {
    fn send_email(&self, to: &str, subject: &str, body: &str) -> Result<(), String>;
}

/// SMTP email service
#[derive(Default)]
pub struct SmtpEmailService;

impl EmailService for SmtpEmailService {
    fn send_email(&self, to: &str, subject: &str, body: &str) -> Result<(), String> {
        println!("SMTP: Sending email to '{}' with subject '{}'", to, subject);
        println!("Body: {}", body);
        Ok(())
    }
}

/// SendGrid email service
#[derive(Default)]
pub struct SendGridEmailService;

impl EmailService for SendGridEmailService {
    fn send_email(&self, to: &str, subject: &str, body: &str) -> Result<(), String> {
        println!(
            "SendGrid API: Sending email to '{}' with subject '{}'",
            to, subject
        );
        println!("Body: {}", body);
        Ok(())
    }
}

/// Storage interface
pub trait Storage: Send + Sync {
    fn store(&self, path: &str, data: &[u8]) -> Result<String, String>;
    fn retrieve(&self, path: &str) -> Result<Vec<u8>, String>;
}

/// Local file storage
#[derive(Default)]
pub struct LocalStorage;

impl Storage for LocalStorage {
    fn store(&self, path: &str, data: &[u8]) -> Result<String, String> {
        println!("LocalStorage: Storing {} bytes to '{}'", data.len(), path);
        Ok(format!("/local/{}", path))
    }

    fn retrieve(&self, path: &str) -> Result<Vec<u8>, String> {
        println!("LocalStorage: Retrieving from '{}'", path);
        Ok(b"local file content".to_vec())
    }
}

/// S3 cloud storage
#[derive(Default)]
pub struct S3Storage;

impl Storage for S3Storage {
    fn store(&self, path: &str, data: &[u8]) -> Result<String, String> {
        println!("S3Storage: Storing {} bytes to '{}'", data.len(), path);
        Ok(format!("https://bucket.s3.amazonaws.com/{}", path))
    }

    fn retrieve(&self, path: &str) -> Result<Vec<u8>, String> {
        println!("S3Storage: Retrieving from '{}'", path);
        Ok(b"s3 file content".to_vec())
    }
}

/// Example 1: Multiple implementations with environment-based selection
pub fn example_environment_based_binding() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Redis cache for production environment
    let redis_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("redis")
        .when_env("CACHE_PROVIDER", "redis")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, RedisCache>(redis_config);

    // Memory cache for development/test environment
    let memory_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("memory")
        .when_env("CACHE_PROVIDER", "memory")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, MemoryCache>(memory_config);

    // Hybrid cache as default
    let hybrid_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .as_default()
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, HybridCache>(hybrid_config);

    container.build()?;
    Ok(container)
}

/// Example 2: Feature flag based service selection
pub fn example_feature_flag_binding() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Cloud storage when cloud features are enabled
    let s3_config = AdvancedBindingBuilder::<dyn Storage>::new()
        .when_feature("cloud-storage")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Storage, S3Storage>(s3_config);

    // Local storage when cloud features are disabled
    let local_config = AdvancedBindingBuilder::<dyn Storage>::new()
        .when_not_feature("cloud-storage")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Storage, LocalStorage>(local_config);

    container.build()?;
    Ok(container)
}

/// Example 3: Profile-based configuration
pub fn example_profile_based_binding() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Production email service
    let smtp_config = AdvancedBindingBuilder::<dyn EmailService>::new()
        .named("production_email")
        .in_profile("production")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn EmailService, SmtpEmailService>(smtp_config);

    // Development email service
    let sendgrid_config = AdvancedBindingBuilder::<dyn EmailService>::new()
        .named("dev_email")
        .in_profile("development")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn EmailService, SendGridEmailService>(sendgrid_config);

    container.build()?;
    Ok(container)
}

/// Example 4: Custom condition binding
pub fn example_custom_condition_binding() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Use Redis cache if Redis URL is available
    let redis_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("conditional_cache")
        .when(|| std::env::var("REDIS_URL").is_ok())
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, RedisCache>(redis_config);

    // Fallback to memory cache
    let memory_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("fallback_cache")
        .when(|| std::env::var("REDIS_URL").is_err())
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, MemoryCache>(memory_config);

    container.build()?;
    Ok(container)
}

/// Example 5: Factory patterns and lazy initialization
pub fn example_factory_patterns() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Lazy-initialized expensive cache
    container.bind_lazy::<RedisCache, _, _>(|| {
        println!("Initializing expensive Redis connection...");
        std::thread::sleep(std::time::Duration::from_millis(10)); // Simulate setup time
        RedisCache::default()
    });

    // Factory with custom logic
    container.bind_factory::<dyn Cache, _, _>(|| {
        let cache_type = std::env::var("CACHE_TYPE").unwrap_or_else(|_| "memory".to_string());
        match cache_type.as_str() {
            "redis" => Ok(Box::new(RedisCache::default()) as Box<dyn Cache>),
            "memory" => Ok(Box::new(MemoryCache::default()) as Box<dyn Cache>),
            _ => Ok(Box::new(HybridCache) as Box<dyn Cache>),
        }
    });

    container.build()?;
    Ok(container)
}

/// Example 6: Collection bindings for plugin architecture
pub fn example_collection_binding() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Register multiple cache implementations as a collection
    container.bind_collection::<dyn Cache, _>(|collection| {
        collection
            .add::<RedisCache>()
            .add::<MemoryCache>()
            .add_named::<HybridCache>("hybrid");
    });

    // Register multiple storage providers
    container.bind_collection::<dyn Storage, _>(|collection| {
        collection.add::<LocalStorage>().add::<S3Storage>();
    });

    container.build()?;
    Ok(container)
}

/// Example 7: Complex multi-condition binding
pub fn example_complex_conditions() -> Result<IocContainer, CoreError> {
    let mut container = IocContainer::new();

    // Complex cache configuration for production with Redis
    let production_redis_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("production_cache")
        .in_profile("production")
        .when_env("CACHE_PROVIDER", "redis")
        .when_feature("high-performance")
        .when(|| std::env::var("REDIS_CLUSTER_NODES").is_ok())
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, RedisCache>(production_redis_config);

    // Staging configuration
    let staging_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("staging_cache")
        .in_profile("staging")
        .when_env("CACHE_PROVIDER", "hybrid")
        .with_lifetime(ServiceScope::Singleton)
        .config();
    container.with_implementation::<dyn Cache, HybridCache>(staging_config);

    // Development fallback
    let dev_config = AdvancedBindingBuilder::<dyn Cache>::new()
        .named("dev_cache")
        .in_profile("development")
        .config();
    container.with_implementation::<dyn Cache, MemoryCache>(dev_config);

    container.build()?;
    Ok(container)
}

/// Example usage demonstration
pub fn demonstrate_advanced_binding_features() -> Result<(), CoreError> {
    println!("=== Advanced Binding Features Demo ===\n");

    // Example 1: Environment-based binding
    println!("1. Environment-based binding:");
    std::env::set_var("CACHE_PROVIDER", "redis");
    let container1 = example_environment_based_binding()?;
    if let Ok(cache) = container1.resolve_named::<RedisCache>("redis") {
        cache.set("test_key", "test_value".to_string()).ok();
    }
    std::env::remove_var("CACHE_PROVIDER");

    // Example 2: Feature flag binding
    println!("\n2. Feature flag binding:");
    std::env::set_var("FEATURE_CLOUD-STORAGE", "1");
    let container2 = example_feature_flag_binding()?;
    if let Ok(storage) = container2.resolve::<S3Storage>() {
        storage.store("test.txt", b"test data").ok();
    }
    std::env::remove_var("FEATURE_CLOUD-STORAGE");

    // Example 3: Profile-based binding
    println!("\n3. Profile-based binding:");
    std::env::set_var("PROFILE", "production");
    let container3 = example_profile_based_binding()?;
    if let Ok(email) = container3.resolve_named::<SmtpEmailService>("production_email") {
        email
            .send_email("user@example.com", "Test", "Hello World")
            .ok();
    }
    std::env::remove_var("PROFILE");

    // Example 4: Factory patterns
    println!("\n4. Factory patterns:");
    let container4 = example_factory_patterns()?;
    if let Ok(cache) = container4.resolve::<RedisCache>() {
        cache.set("lazy_key", "lazy_value".to_string()).ok();
    }

    // Example 5: Service statistics
    println!("\n5. Service statistics:");
    let stats = container4.get_statistics();
    println!("Total services: {}", stats.total_services);
    println!("Singleton services: {}", stats.singleton_services);
    println!("Cached instances: {}", stats.cached_instances);

    // Example 6: Service validation
    println!("\n6. Service validation:");
    match container4.validate_all_services() {
        Ok(()) => println!("All services are valid!"),
        Err(errors) => println!("Validation errors: {}", errors.len()),
    }

    println!("\n=== Demo Complete ===");
    Ok(())
}

#[cfg(test)]
mod example_tests {
    use super::*;
    use serial_test::serial;

    #[test]
    #[serial]
    fn test_environment_based_example() {
        std::env::set_var("CACHE_PROVIDER", "memory");
        let result = example_environment_based_binding();
        assert!(result.is_ok());
        std::env::remove_var("CACHE_PROVIDER");
    }

    #[test]
    fn test_feature_flag_example() {
        let result = example_feature_flag_binding();
        assert!(result.is_ok());
    }

    #[test]
    #[serial]
    fn test_profile_based_example() {
        std::env::set_var("PROFILE", "development");
        let result = example_profile_based_binding();
        assert!(result.is_ok());
        std::env::remove_var("PROFILE");
    }

    #[test]
    fn test_factory_patterns_example() {
        let result = example_factory_patterns();
        assert!(result.is_ok());
    }

    #[test]
    #[serial]
    fn test_demonstration() {
        let result = demonstrate_advanced_binding_features();
        assert!(result.is_ok());
    }
}