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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
use std::collections::{HashMap, HashSet};

use crate::container::descriptor::{ServiceDescriptor, ServiceId};
use crate::container::scope::ServiceScope;
use crate::errors::CoreError;

/// Compile-time validation error types
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
    /// Service registration is missing
    MissingRegistration {
        service_type: String,
        required_by: String,
    },
    /// Circular dependency detected
    CircularDependency { cycle: Vec<String> },
    /// Lifetime compatibility issue
    LifetimeIncompatibility {
        service: String,
        service_lifetime: ServiceScope,
        dependency: String,
        dependency_lifetime: ServiceScope,
    },
    /// Interface without implementation
    UnboundInterface { interface: String },
    /// Multiple default implementations
    MultipleDefaults {
        interface: String,
        implementations: Vec<String>,
    },
    /// Invalid factory signature
    InvalidFactory { service: String, error: String },
    /// Missing auto-wire information
    MissingAutoWire { service: String },
}

impl ValidationError {
    /// Convert to CoreError for runtime use
    pub fn to_core_error(&self) -> CoreError {
        match self {
            ValidationError::MissingRegistration {
                service_type,
                required_by,
            } => CoreError::ServiceNotFound {
                service_type: format!("{} (required by {})", service_type, required_by),
            },
            ValidationError::CircularDependency { cycle } => CoreError::InvalidServiceDescriptor {
                message: format!("Circular dependency detected: {}", cycle.join(" -> ")),
            },
            ValidationError::LifetimeIncompatibility {
                service,
                service_lifetime,
                dependency,
                dependency_lifetime,
            } => CoreError::InvalidServiceDescriptor {
                message: format!(
                    "Lifetime incompatibility: {} ({:?}) depends on {} ({:?})",
                    service, service_lifetime, dependency, dependency_lifetime
                ),
            },
            ValidationError::UnboundInterface { interface } => CoreError::ServiceNotFound {
                service_type: format!("No implementation bound for interface {}", interface),
            },
            ValidationError::MultipleDefaults {
                interface,
                implementations,
            } => CoreError::InvalidServiceDescriptor {
                message: format!(
                    "Multiple default implementations for {}: {}",
                    interface,
                    implementations.join(", ")
                ),
            },
            ValidationError::InvalidFactory { service, error } => {
                CoreError::InvalidServiceDescriptor {
                    message: format!("Invalid factory for {}: {}", service, error),
                }
            }
            ValidationError::MissingAutoWire { service } => CoreError::InvalidServiceDescriptor {
                message: format!(
                    "Service {} is marked for auto-wiring but no constructor info available",
                    service
                ),
            },
        }
    }
}

/// Compile-time dependency validator
#[derive(Debug)]
pub struct DependencyValidator {
    dependency_graph: HashMap<ServiceId, Vec<ServiceId>>,
    interface_bindings: HashMap<String, Vec<ServiceId>>,
    service_ids: Vec<ServiceId>,
    service_lifetimes: HashMap<ServiceId, ServiceScope>,
}

impl DependencyValidator {
    /// Create a new validator from service descriptors
    pub fn new(descriptors: &[ServiceDescriptor]) -> Self {
        let mut dependency_graph = HashMap::new();
        let mut interface_bindings: HashMap<String, Vec<ServiceId>> = HashMap::new();
        let mut service_lifetimes = HashMap::new();

        // Build dependency graph, interface bindings, and store lifetimes
        for descriptor in descriptors {
            dependency_graph.insert(
                descriptor.service_id.clone(),
                descriptor.dependencies.clone(),
            );
            service_lifetimes.insert(
                descriptor.service_id.clone(),
                descriptor.lifetime,
            );

            // Track interface bindings (simplified - would need more metadata in real implementation)
            if let Some(interface_name) = descriptor.service_id.name.as_ref() {
                interface_bindings
                    .entry(interface_name.clone())
                    .or_default()
                    .push(descriptor.service_id.clone());
            }
        }

        let service_ids = descriptors.iter().map(|d| d.service_id.clone()).collect();

        Self {
            dependency_graph,
            interface_bindings,
            service_ids,
            service_lifetimes,
        }
    }

    /// Validate all dependencies and return any errors
    pub fn validate(&self) -> Result<(), Vec<ValidationError>> {
        let mut errors = Vec::new();

        // Check for missing registrations
        errors.extend(self.validate_registrations());

        // Check for circular dependencies
        errors.extend(self.validate_circular_dependencies());

        // Check lifetime compatibility
        errors.extend(self.validate_lifetime_compatibility());

        // Check interface bindings
        errors.extend(self.validate_interface_bindings());

        // Check for multiple defaults
        errors.extend(self.validate_default_implementations());

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Validate that all dependencies are registered
    fn validate_registrations(&self) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let registered_services: HashSet<&ServiceId> = self.service_ids.iter().collect();

        for (service_id, dependencies) in &self.dependency_graph {
            for dependency in dependencies {
                if !registered_services.contains(dependency) {
                    errors.push(ValidationError::MissingRegistration {
                        service_type: dependency.type_name().to_string(),
                        required_by: service_id.type_name().to_string(),
                    });
                }
            }
        }

        errors
    }

    /// Validate there are no circular dependencies
    fn validate_circular_dependencies(&self) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();
        let mut path = Vec::new();

        for service_id in self.dependency_graph.keys() {
            if !visited.contains(service_id) {
                if let Some(cycle) =
                    self.detect_cycle(service_id, &mut visited, &mut rec_stack, &mut path)
                {
                    errors.push(ValidationError::CircularDependency { cycle });
                }
            }
        }

        errors
    }

    /// Detect cycles in dependency graph using DFS
    fn detect_cycle(
        &self,
        service_id: &ServiceId,
        visited: &mut HashSet<ServiceId>,
        rec_stack: &mut HashSet<ServiceId>,
        path: &mut Vec<String>,
    ) -> Option<Vec<String>> {
        visited.insert(service_id.clone());
        rec_stack.insert(service_id.clone());
        path.push(service_id.type_name().to_string());

        if let Some(dependencies) = self.dependency_graph.get(service_id) {
            for dependency in dependencies {
                if !visited.contains(dependency) {
                    if let Some(cycle) = self.detect_cycle(dependency, visited, rec_stack, path) {
                        return Some(cycle);
                    }
                } else if rec_stack.contains(dependency) {
                    // Found a back edge - cycle detected
                    let cycle_start = path
                        .iter()
                        .position(|name| name == dependency.type_name())
                        .unwrap_or(0);
                    let mut cycle = path[cycle_start..].to_vec();
                    cycle.push(dependency.type_name().to_string());
                    return Some(cycle);
                }
            }
        }

        rec_stack.remove(service_id);
        path.pop();
        None
    }

    /// Validate lifetime compatibility
    fn validate_lifetime_compatibility(&self) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        
        for (service_id, dependencies) in &self.dependency_graph {
            if let Some(&service_lifetime) = self.service_lifetimes.get(service_id) {
                for dependency_id in dependencies {
                    if let Some(&dependency_lifetime) = self.service_lifetimes.get(dependency_id) {
                        if !self.is_lifetime_compatible(service_lifetime, dependency_lifetime) {
                            errors.push(ValidationError::LifetimeIncompatibility {
                                service: service_id.type_name().to_string(),
                                service_lifetime,
                                dependency: dependency_id.type_name().to_string(),
                                dependency_lifetime,
                            });
                        }
                    }
                }
            }
        }
        
        errors
    }

    /// Check if service lifetime is compatible with dependency lifetime
    fn is_lifetime_compatible(&self, service: ServiceScope, dependency: ServiceScope) -> bool {
        match (service, dependency) {
            // Singleton can depend on anything
            (ServiceScope::Singleton, _) => true,

            // Scoped can depend on Singleton or Scoped, but not Transient
            (ServiceScope::Scoped, ServiceScope::Transient) => false,
            (ServiceScope::Scoped, _) => true,

            // Transient can depend on anything
            (ServiceScope::Transient, _) => true,
        }
    }

    /// Validate interface bindings
    fn validate_interface_bindings(&self) -> Vec<ValidationError> {
        let mut errors = Vec::new();

        // This would be more sophisticated in a real implementation
        // For now, just check if we have interfaces without implementations
        for (interface, implementations) in &self.interface_bindings {
            if implementations.is_empty() {
                errors.push(ValidationError::UnboundInterface {
                    interface: interface.clone(),
                });
            }
        }

        errors
    }

    /// Validate default implementations
    fn validate_default_implementations(&self) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let defaults_per_interface: HashMap<String, Vec<String>> = HashMap::new();

        // This would need additional metadata to track which services are marked as default
        // For now, this is a placeholder

        for (interface, implementations) in defaults_per_interface {
            if implementations.len() > 1 {
                errors.push(ValidationError::MultipleDefaults {
                    interface,
                    implementations,
                });
            }
        }

        errors
    }

    /// Get dependency graph for visualization
    pub fn dependency_graph(&self) -> &HashMap<ServiceId, Vec<ServiceId>> {
        &self.dependency_graph
    }

    /// Get topologically sorted services
    pub fn topological_sort(&self) -> Result<Vec<ServiceId>, ValidationError> {
        let mut visited = HashSet::new();
        let mut temp_visited = HashSet::new();
        let mut result = Vec::new();

        // Sort service IDs to ensure consistent ordering
        let mut service_ids: Vec<_> = self.dependency_graph.keys().cloned().collect();
        service_ids.sort_by(|a, b| a.type_name().cmp(b.type_name()));
        
        for service_id in service_ids {
            if !visited.contains(&service_id) {
                self.visit_for_topo_sort(&service_id, &mut visited, &mut temp_visited, &mut result)?
            }
        }

        // No reverse needed - DFS post-order already gives correct dependency order
        Ok(result)
    }

    /// Visit node for topological sorting
    fn visit_for_topo_sort(
        &self,
        service_id: &ServiceId,
        visited: &mut HashSet<ServiceId>,
        temp_visited: &mut HashSet<ServiceId>,
        result: &mut Vec<ServiceId>,
    ) -> Result<(), ValidationError> {
        if temp_visited.contains(service_id) {
            return Err(ValidationError::CircularDependency {
                cycle: vec![service_id.type_name().to_string()], // Simplified
            });
        }

        if visited.contains(service_id) {
            return Ok(());
        }

        temp_visited.insert(service_id.clone());

        if let Some(dependencies) = self.dependency_graph.get(service_id) {
            for dependency in dependencies {
                self.visit_for_topo_sort(dependency, visited, temp_visited, result)?;
            }
        }

        temp_visited.remove(service_id);
        visited.insert(service_id.clone());
        result.push(service_id.clone());

        Ok(())
    }
}

/// Container validator for runtime validation
#[derive(Debug)]
pub struct ContainerValidator;

impl ContainerValidator {
    /// Create a new container validator
    pub fn new() -> Self {
        Self
    }

    /// Validate container configuration
    pub fn validate_container(
        &self,
        descriptors: &[ServiceDescriptor],
    ) -> Result<ValidationReport, Vec<ValidationError>> {
        let validator = DependencyValidator::new(descriptors);

        match validator.validate() {
            Ok(()) => {
                let topo_order = validator.topological_sort().map_err(|e| vec![e])?;

                Ok(ValidationReport {
                    is_valid: true,
                    service_count: descriptors.len(),
                    dependency_count: validator
                        .dependency_graph
                        .values()
                        .map(|deps| deps.len())
                        .sum(),
                    resolution_order: topo_order,
                    errors: vec![],
                    warnings: vec![],
                })
            }
            Err(errors) => Err(errors),
        }
    }

    /// Validate and provide warnings for potential issues
    pub fn validate_with_warnings(&self, descriptors: &[ServiceDescriptor]) -> ValidationReport {
        let validator = DependencyValidator::new(descriptors);
        let mut warnings = Vec::new();

        match validator.validate() {
            Ok(()) => {
                // Check for potential warnings
                warnings.extend(self.check_performance_warnings(descriptors));
                warnings.extend(self.check_design_warnings(descriptors));

                let topo_order = validator.topological_sort().unwrap_or_else(|_| vec![]);

                ValidationReport {
                    is_valid: true,
                    service_count: descriptors.len(),
                    dependency_count: validator
                        .dependency_graph
                        .values()
                        .map(|deps| deps.len())
                        .sum(),
                    resolution_order: topo_order,
                    errors: vec![],
                    warnings,
                }
            }
            Err(errors) => ValidationReport {
                is_valid: false,
                service_count: descriptors.len(),
                dependency_count: 0,
                resolution_order: vec![],
                errors,
                warnings,
            },
        }
    }

    /// Check for performance-related warnings
    fn check_performance_warnings(
        &self,
        descriptors: &[ServiceDescriptor],
    ) -> Vec<ValidationWarning> {
        let mut warnings = Vec::new();

        // Check for too many transient services
        let transient_count = descriptors
            .iter()
            .filter(|d| d.lifetime == ServiceScope::Transient)
            .count();

        if transient_count > descriptors.len() / 2 {
            warnings.push(ValidationWarning::PerformanceConcern {
                issue: format!("High number of transient services ({}/{}). Consider using singleton or scoped lifetimes.", transient_count, descriptors.len()),
            });
        }

        // Check for deeply nested dependencies
        // TODO: Implement depth checking

        warnings
    }

    /// Check for design-related warnings
    fn check_design_warnings(&self, descriptors: &[ServiceDescriptor]) -> Vec<ValidationWarning> {
        let mut warnings = Vec::new();

        // Check for services with too many dependencies
        for descriptor in descriptors {
            if descriptor.dependencies.len() > 5 {
                warnings.push(ValidationWarning::DesignConcern {
                    service: descriptor.service_id.type_name().to_string(),
                    issue: format!(
                        "Service has {} dependencies. Consider breaking it down.",
                        descriptor.dependencies.len()
                    ),
                });
            }
        }

        warnings
    }
}

impl Default for ContainerValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// Validation warning types
#[derive(Debug, Clone)]
pub enum ValidationWarning {
    /// Performance-related concern
    PerformanceConcern { issue: String },
    /// Design-related concern
    DesignConcern { service: String, issue: String },
    /// Best practice violation
    BestPracticeViolation {
        service: String,
        violation: String,
        suggestion: String,
    },
}

/// Validation report containing results and recommendations
#[derive(Debug)]
pub struct ValidationReport {
    pub is_valid: bool,
    pub service_count: usize,
    pub dependency_count: usize,
    pub resolution_order: Vec<ServiceId>,
    pub errors: Vec<ValidationError>,
    pub warnings: Vec<ValidationWarning>,
}

impl ValidationReport {
    /// Generate a human-readable report
    pub fn to_string(&self) -> String {
        let mut report = String::new();

        report.push_str("Container Validation Report\n");
        report.push_str("==========================\n\n");

        report.push_str(&format!(
            "Status: {}\n",
            if self.is_valid { "VALID" } else { "INVALID" }
        ));
        report.push_str(&format!("Services: {}\n", self.service_count));
        report.push_str(&format!("Dependencies: {}\n", self.dependency_count));
        report.push_str(&format!(
            "Resolution Order: {} services\n\n",
            self.resolution_order.len()
        ));

        if !self.errors.is_empty() {
            report.push_str("ERRORS:\n");
            for (i, error) in self.errors.iter().enumerate() {
                report.push_str(&format!("  {}. {:?}\n", i + 1, error));
            }
            report.push('\n');
        }

        if !self.warnings.is_empty() {
            report.push_str("WARNINGS:\n");
            for (i, warning) in self.warnings.iter().enumerate() {
                report.push_str(&format!("  {}. {:?}\n", i + 1, warning));
            }
            report.push('\n');
        }

        if self.is_valid {
            report.push_str("✅ Container configuration is valid and ready for use.\n");
        } else {
            report.push_str("❌ Container configuration has errors that must be fixed.\n");
        }

        report
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::container::descriptor::{ServiceActivationStrategy, ServiceDescriptor};
    use std::any::{Any, TypeId};

    fn create_test_descriptor(
        type_name: &str,
        lifetime: ServiceScope,
        deps: Vec<&str>,
    ) -> ServiceDescriptor {
        let service_id = ServiceId {
            type_id: TypeId::of::<()>(),
            type_name: "test_service",
            name: Some(type_name.to_string()),
        };

        let dependencies: Vec<ServiceId> = deps
            .iter()
            .map(|dep| ServiceId {
                type_id: TypeId::of::<()>(),
                type_name: "test_service",
                name: Some(dep.to_string()),
            })
            .collect();

        ServiceDescriptor {
            service_id,
            implementation_id: TypeId::of::<()>(),
            lifetime,
            dependencies,
            activation_strategy: ServiceActivationStrategy::Factory(Box::new(|| {
                Ok(Box::new(()) as Box<dyn Any + Send + Sync>)
            })),
        }
    }

    #[test]
    fn test_valid_dependencies() {
        let descriptors = vec![
            create_test_descriptor("ServiceA", ServiceScope::Singleton, vec![]),
            create_test_descriptor("ServiceB", ServiceScope::Singleton, vec!["ServiceA"]),
            create_test_descriptor(
                "ServiceC",
                ServiceScope::Transient,
                vec!["ServiceA", "ServiceB"],
            ),
        ];

        let validator = DependencyValidator::new(&descriptors);
        let result = validator.validate();

        assert!(result.is_ok());
    }

    #[test]
    fn test_missing_dependency() {
        let descriptors = vec![create_test_descriptor(
            "ServiceA",
            ServiceScope::Singleton,
            vec!["MissingService"],
        )];

        let validator = DependencyValidator::new(&descriptors);
        let result = validator.validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert_eq!(errors.len(), 1);
        assert!(matches!(
            errors[0],
            ValidationError::MissingRegistration { .. }
        ));
    }

    #[test]
    fn test_circular_dependency() {
        let descriptors = vec![
            create_test_descriptor("ServiceA", ServiceScope::Singleton, vec!["ServiceB"]),
            create_test_descriptor("ServiceB", ServiceScope::Singleton, vec!["ServiceA"]),
        ];

        let validator = DependencyValidator::new(&descriptors);
        let result = validator.validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(errors
            .iter()
            .any(|e| matches!(e, ValidationError::CircularDependency { .. })));
    }

    #[test]
    fn test_lifetime_incompatibility() {
        let descriptors = vec![
            create_test_descriptor(
                "SingletonService",
                ServiceScope::Singleton,
                vec!["TransientService"],
            ),
            create_test_descriptor("TransientService", ServiceScope::Transient, vec![]),
            create_test_descriptor(
                "ScopedService",
                ServiceScope::Scoped,
                vec!["TransientService"],
            ),
        ];

        let validator = DependencyValidator::new(&descriptors);
        let result = validator.validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        // Scoped depending on Transient should be an error
        assert!(errors
            .iter()
            .any(|e| matches!(e, ValidationError::LifetimeIncompatibility { .. })));
    }

    #[test]
    fn test_topological_sort() {
        let descriptors = vec![
            create_test_descriptor(
                "ServiceC",
                ServiceScope::Singleton,
                vec!["ServiceA", "ServiceB"],
            ),
            create_test_descriptor("ServiceB", ServiceScope::Singleton, vec!["ServiceA"]),
            create_test_descriptor("ServiceA", ServiceScope::Singleton, vec![]),
        ];

        let validator = DependencyValidator::new(&descriptors);
        let topo_order = validator.topological_sort().unwrap();

        // ServiceA should come first (no dependencies)
        // ServiceB should come second (depends on A)
        // ServiceC should come last (depends on A and B)
        let names: Vec<String> = topo_order
            .iter()
            .map(|id| id.name.as_ref().unwrap().clone())
            .collect();

        assert_eq!(names[0], "ServiceA");
        assert_eq!(names[1], "ServiceB");
        assert_eq!(names[2], "ServiceC");
    }

    #[test]
    fn test_container_validator() {
        let descriptors = vec![
            create_test_descriptor("ServiceA", ServiceScope::Singleton, vec![]),
            create_test_descriptor("ServiceB", ServiceScope::Transient, vec!["ServiceA"]),
        ];

        let validator = ContainerValidator::new();
        let report = validator.validate_with_warnings(&descriptors);

        assert!(report.is_valid);
        assert_eq!(report.service_count, 2);
        assert_eq!(report.resolution_order.len(), 2);
    }
}