elif-http 0.8.8

HTTP server core 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
//! Route conflict detection and validation system for bootstrap integration
//!
//! This module provides comprehensive route conflict detection during application startup,
//! building on the core route matching capabilities with enhanced diagnostics and
//! conflict resolution suggestions.

use crate::{
    bootstrap::BootstrapError,
    routing::{HttpMethod, RouteMatchError, RouteMatcher, RouteDefinition},
};
use std::collections::{HashMap, HashSet};
use thiserror::Error;

/// Errors that can occur during route validation
#[derive(Error, Debug)]
pub enum RouteValidationError {
    #[error("Route conflict detected")]
    ConflictDetected {
        conflicts: Vec<RouteConflict>,
    },
    #[error("Parameter type conflict in route {route}: {details}")]
    ParameterConflict {
        route: String,
        details: String,
    },
    #[error("Invalid route configuration: {message}")]
    InvalidConfiguration {
        message: String,
    },
    #[error("Route validation failed: {0}")]
    ValidationFailed(#[from] RouteMatchError),
}

/// Detailed information about a route conflict
#[derive(Debug, Clone)]
pub struct RouteConflict {
    pub route1: RouteInfo,
    pub route2: RouteInfo,
    pub conflict_type: ConflictType,
    pub resolution_suggestions: Vec<ConflictResolution>,
}

/// Information about a conflicting route
#[derive(Debug, Clone)]
pub struct RouteInfo {
    pub method: HttpMethod,
    pub path: String,
    pub controller: String,
    pub handler: String,
    pub middleware: Vec<String>,
    pub parameters: Vec<ParamDef>,
}

/// Types of route conflicts
#[derive(Debug, Clone)]
pub enum ConflictType {
    /// Exact path conflict (same method + path)
    Exact,
    /// Parameter type mismatch for same path pattern
    ParameterMismatch,
    /// Ambiguous route patterns that could match same request
    Ambiguous,
    /// Middleware incompatibility
    MiddlewareIncompatible,
}

/// Parameter definition for validation
#[derive(Debug, Clone)]
pub struct ParamDef {
    pub name: String,
    pub param_type: String,
    pub required: bool,
    pub constraints: Vec<String>,
}

/// Suggested resolutions for route conflicts
#[derive(Debug, Clone)]
pub enum ConflictResolution {
    MergePaths { suggestion: String },
    RenameParameter { from: String, to: String },
    DifferentControllerPaths { suggestion: String },
    MiddlewareConsolidation { suggestion: String },
    UseQueryParameters { suggestion: String },
    ReorderRoutes { suggestion: String },
}

/// Route validator for bootstrap integration
#[derive(Debug)]
pub struct RouteValidator {
    /// All registered routes for validation
    routes: HashMap<RouteKey, RouteRegistration>,
    /// Route matcher for conflict detection
    matcher: RouteMatcher,
    /// Enable detailed diagnostics
    enable_diagnostics: bool,
}

/// Key for identifying unique routes
#[derive(Hash, PartialEq, Eq, Debug, Clone)]
pub struct RouteKey {
    pub method: HttpMethod,
    pub path_pattern: String,
}

/// Registration information for a route
#[derive(Debug, Clone)]
pub struct RouteRegistration {
    pub controller: String,
    pub handler: String,
    pub middleware: Vec<String>,
    pub parameters: Vec<ParamDef>,
    pub definition: RouteDefinition,
}

impl RouteValidator {
    /// Create a new route validator
    pub fn new() -> Self {
        Self {
            routes: HashMap::new(),
            matcher: RouteMatcher::new(),
            enable_diagnostics: true,
        }
    }

    /// Enable or disable detailed diagnostics
    pub fn with_diagnostics(mut self, enable: bool) -> Self {
        self.enable_diagnostics = enable;
        self
    }

    /// Register a route for validation
    pub fn register_route(&mut self, registration: RouteRegistration) -> Result<(), RouteValidationError> {
        let route_key = RouteKey {
            method: registration.definition.method.clone(),
            path_pattern: registration.definition.path.clone(),
        };

        // Check for conflicts before adding
        if let Some(existing) = self.routes.get(&route_key) {
            let conflict = self.analyze_conflict(&registration, existing)?;
            return Err(RouteValidationError::ConflictDetected {
                conflicts: vec![conflict],
            });
        }

        // Add to route matcher for pattern conflict detection
        self.matcher.add_route(registration.definition.clone())
            .map_err(RouteValidationError::ValidationFailed)?;

        // Store registration
        self.routes.insert(route_key, registration);
        
        Ok(())
    }

    /// Validate all registered routes for conflicts
    pub fn validate_all_routes(&self) -> Result<ValidationReport, RouteValidationError> {
        let mut conflicts = Vec::new();
        let mut warnings = Vec::new();

        // Check for parameter type conflicts
        self.check_parameter_conflicts(&mut conflicts);

        // Check for middleware incompatibilities
        self.check_middleware_conflicts(&mut warnings);

        // Check for performance issues
        self.check_performance_issues(&mut warnings);

        if !conflicts.is_empty() {
            return Err(RouteValidationError::ConflictDetected { conflicts });
        }

        Ok(ValidationReport {
            total_routes: self.routes.len(),
            conflicts: conflicts.len(),
            warnings: warnings.len(),
            performance_score: self.calculate_performance_score(),
            suggestions: self.generate_optimization_suggestions(),
        })
    }

    /// Generate detailed conflict report for diagnostics
    pub fn generate_conflict_report(&self, conflicts: &[RouteConflict]) -> String {
        let mut report = String::new();
        
        for (i, conflict) in conflicts.iter().enumerate() {
            if i > 0 {
                report.push_str("\n\n");
            }
            
            match conflict.conflict_type {
                ConflictType::Exact => {
                    report.push_str(&format!(
                        "Error: Duplicate route definition detected\n\n\
                         Route: {} {}\n\
                         Defined in:\n\
                         1. {}::{}\n\
                         2. {}::{}\n\n\
                         Resolution suggestions:",
                        conflict.route1.method.as_str(),
                        conflict.route1.path,
                        conflict.route1.controller,
                        conflict.route1.handler,
                        conflict.route2.controller,
                        conflict.route2.handler
                    ));
                }
                ConflictType::ParameterMismatch => {
                    report.push_str(&format!(
                        "Error: Route parameter type conflict\n\n\
                         Route pattern: {} {}\n\
                         Parameter conflicts:\n\
                         • {} expects different types\n\
                         • {} expects different types\n\n\
                         Resolution: Ensure all controllers use the same parameter types",
                        conflict.route1.method.as_str(),
                        conflict.route1.path,
                        conflict.route1.controller,
                        conflict.route2.controller
                    ));
                }
                ConflictType::Ambiguous => {
                    report.push_str(&format!(
                        "Error: Ambiguous route patterns detected\n\n\
                         Routes that could match the same request:\n\
                         1. {} {} ({})\n\
                         2. {} {} ({})\n\n\
                         Problem: These patterns could match the same request\n\n\
                         Resolution: Reorder routes or use more specific patterns",
                        conflict.route1.method.as_str(),
                        conflict.route1.path,
                        conflict.route1.controller,
                        conflict.route2.method.as_str(),
                        conflict.route2.path,
                        conflict.route2.controller
                    ));
                }
                ConflictType::MiddlewareIncompatible => {
                    report.push_str(&format!(
                        "Warning: Middleware incompatibility detected\n\n\
                         Route: {} {}\n\
                         Controllers with different middleware:\n\
                         • {}: {:?}\n\
                         • {}: {:?}\n\n\
                         Resolution: Consider consolidating middleware requirements",
                        conflict.route1.method.as_str(),
                        conflict.route1.path,
                        conflict.route1.controller,
                        conflict.route1.middleware,
                        conflict.route2.controller,
                        conflict.route2.middleware
                    ));
                }
            }

            // Add resolution suggestions
            for (j, suggestion) in conflict.resolution_suggestions.iter().enumerate() {
                report.push_str(&format!("\n  {}. {}", j + 1, self.format_suggestion(suggestion)));
            }
        }

        report
    }

    /// Analyze conflict between two route registrations
    fn analyze_conflict(&self, route1: &RouteRegistration, route2: &RouteRegistration) -> Result<RouteConflict, RouteValidationError> {
        let route_info1 = RouteInfo {
            method: route1.definition.method.clone(),
            path: route1.definition.path.clone(),
            controller: route1.controller.clone(),
            handler: route1.handler.clone(),
            middleware: route1.middleware.clone(),
            parameters: route1.parameters.clone(),
        };

        let route_info2 = RouteInfo {
            method: route2.definition.method.clone(),
            path: route2.definition.path.clone(),
            controller: route2.controller.clone(),
            handler: route2.handler.clone(),
            middleware: route2.middleware.clone(),
            parameters: route2.parameters.clone(),
        };

        let conflict_type = if route1.definition.path == route2.definition.path {
            if self.parameters_conflict(&route1.parameters, &route2.parameters) {
                ConflictType::ParameterMismatch
            } else {
                ConflictType::Exact
            }
        } else {
            ConflictType::Ambiguous
        };

        let resolution_suggestions = self.generate_resolution_suggestions(&route_info1, &route_info2, &conflict_type);

        Ok(RouteConflict {
            route1: route_info1,
            route2: route_info2,
            conflict_type,
            resolution_suggestions,
        })
    }

    /// Check if parameters conflict between routes
    fn parameters_conflict(&self, params1: &[ParamDef], params2: &[ParamDef]) -> bool {
        for param1 in params1 {
            for param2 in params2 {
                if param1.name == param2.name && param1.param_type != param2.param_type {
                    return true;
                }
            }
        }
        false
    }

    /// Check for parameter conflicts across all routes
    fn check_parameter_conflicts(&self, _conflicts: &mut Vec<RouteConflict>) {
        let mut param_types: HashMap<String, (String, String)> = HashMap::new();
        
        for registration in self.routes.values() {
            for param in &registration.parameters {
                let key = format!("{}:{}", registration.definition.path, param.name);
                if let Some((existing_type, _existing_controller)) = param_types.get(&key) {
                    if existing_type != &param.param_type {
                        // Found parameter conflict - would need to create RouteConflict
                        // This is simplified for now
                    }
                } else {
                    param_types.insert(key, (param.param_type.clone(), registration.controller.clone()));
                }
            }
        }
    }

    /// Check for middleware conflicts
    fn check_middleware_conflicts(&self, warnings: &mut Vec<String>) {
        // Group routes by path pattern to check middleware consistency
        let mut path_middleware: HashMap<String, Vec<(String, Vec<String>)>> = HashMap::new();
        
        for registration in self.routes.values() {
            let path = &registration.definition.path;
            path_middleware
                .entry(path.clone())
                .or_default()
                .push((registration.controller.clone(), registration.middleware.clone()));
        }

        for (path, controllers) in path_middleware {
            if controllers.len() > 1 {
                let middleware_sets: HashSet<Vec<String>> = controllers.iter().map(|(_, mw)| mw.clone()).collect();
                if middleware_sets.len() > 1 {
                    warnings.push(format!(
                        "Inconsistent middleware for path {}: controllers have different middleware requirements",
                        path
                    ));
                }
            }
        }
    }

    /// Check for performance issues
    fn check_performance_issues(&self, warnings: &mut Vec<String>) {
        if self.routes.len() > 1000 {
            warnings.push("Large number of routes (>1000) may impact performance".to_string());
        }

        // Check for overly complex patterns
        for registration in self.routes.values() {
            let param_count = registration.parameters.len();
            if param_count > 5 {
                warnings.push(format!(
                    "Route {} has {} parameters, consider simplifying",
                    registration.definition.path,
                    param_count
                ));
            }
        }
    }

    /// Calculate overall performance score
    fn calculate_performance_score(&self) -> u32 {
        let base_score: u32 = 100;
        let route_penalty = (self.routes.len() / 100) as u32; // 1 point per 100 routes
        
        let complex_routes = self.routes.values()
            .filter(|r| r.parameters.len() > 3)
            .count() as u32;
        
        base_score.saturating_sub(route_penalty + complex_routes)
    }

    /// Generate optimization suggestions
    fn generate_optimization_suggestions(&self) -> Vec<String> {
        let mut suggestions = Vec::new();

        if self.routes.len() > 500 {
            suggestions.push("Consider grouping routes by modules for better organization".to_string());
        }

        suggestions
    }

    /// Generate resolution suggestions for conflicts
    fn generate_resolution_suggestions(&self, _route1: &RouteInfo, _route2: &RouteInfo, conflict_type: &ConflictType) -> Vec<ConflictResolution> {
        match conflict_type {
            ConflictType::Exact => vec![
                ConflictResolution::DifferentControllerPaths { 
                    suggestion: "Use different base paths like /api/users vs /api/admin/users".to_string() 
                },
                ConflictResolution::MergePaths { 
                    suggestion: "Merge functionality into a single controller".to_string() 
                },
                ConflictResolution::UseQueryParameters { 
                    suggestion: "Use query parameters instead: GET /api/users/{id}?admin=true".to_string() 
                },
            ],
            ConflictType::ParameterMismatch => vec![
                ConflictResolution::RenameParameter { 
                    from: "id".to_string(), 
                    to: "user_id".to_string() 
                },
            ],
            ConflictType::Ambiguous => vec![
                ConflictResolution::ReorderRoutes { 
                    suggestion: "Reorder routes to put more specific patterns first".to_string() 
                },
            ],
            ConflictType::MiddlewareIncompatible => vec![
                ConflictResolution::MiddlewareConsolidation { 
                    suggestion: "Consolidate middleware requirements across controllers".to_string() 
                },
            ],
        }
    }

    /// Format a resolution suggestion for display
    fn format_suggestion(&self, suggestion: &ConflictResolution) -> String {
        match suggestion {
            ConflictResolution::MergePaths { suggestion } => suggestion.clone(),
            ConflictResolution::RenameParameter { from, to } => {
                format!("Rename parameter '{}' to '{}'", from, to)
            },
            ConflictResolution::DifferentControllerPaths { suggestion } => suggestion.clone(),
            ConflictResolution::MiddlewareConsolidation { suggestion } => suggestion.clone(),
            ConflictResolution::UseQueryParameters { suggestion } => suggestion.clone(),
            ConflictResolution::ReorderRoutes { suggestion } => suggestion.clone(),
        }
    }
}

/// Report generated after route validation
#[derive(Debug)]
pub struct ValidationReport {
    pub total_routes: usize,
    pub conflicts: usize,
    pub warnings: usize,
    pub performance_score: u32,
    pub suggestions: Vec<String>,
}

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

/// Convert RouteValidationError to BootstrapError for integration
impl From<RouteValidationError> for BootstrapError {
    fn from(err: RouteValidationError) -> Self {
        BootstrapError::RouteRegistrationFailed {
            message: format!("Route validation failed: {}", err),
        }
    }
}

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

    /// Helper to create test route registration
    fn create_test_route(
        controller: &str,
        handler: &str,
        method: HttpMethod,
        path: &str,
        params: Vec<ParamDef>,
    ) -> RouteRegistration {
        RouteRegistration {
            controller: controller.to_string(),
            handler: handler.to_string(),
            middleware: Vec::new(),
            parameters: params,
            definition: RouteDefinition {
                id: format!("{}::{}", controller, handler),
                method,
                path: path.to_string(),
            },
        }
    }

    #[test]
    fn test_successful_route_registration() {
        let mut validator = RouteValidator::new();
        
        let route = create_test_route(
            "UserController",
            "get_user",
            HttpMethod::GET,
            "/api/users/{id}",
            vec![ParamDef {
                name: "id".to_string(),
                param_type: "u32".to_string(),
                required: true,
                constraints: vec!["int".to_string()],
            }]
        );

        let result = validator.register_route(route);
        assert!(result.is_ok(), "Route registration should succeed");
        
        let report = validator.validate_all_routes().unwrap();
        assert_eq!(report.total_routes, 1);
        assert_eq!(report.conflicts, 0);
    }

    #[test]
    fn test_exact_route_conflict_detection() {
        let mut validator = RouteValidator::new();
        
        // Register first route
        let route1 = create_test_route(
            "UserController",
            "get_user",
            HttpMethod::GET,
            "/api/users/{id}",
            vec![ParamDef {
                name: "id".to_string(),
                param_type: "u32".to_string(),
                required: true,
                constraints: vec!["int".to_string()],
            }]
        );
        validator.register_route(route1).unwrap();

        // Try to register conflicting route
        let route2 = create_test_route(
            "AdminController",
            "get_admin_user",
            HttpMethod::GET,
            "/api/users/{id}",  // Same path!
            vec![ParamDef {
                name: "id".to_string(),
                param_type: "u32".to_string(),
                required: true,
                constraints: vec!["int".to_string()],
            }]
        );

        let result = validator.register_route(route2);
        assert!(result.is_err(), "Conflicting route should be rejected");
        
        match result.unwrap_err() {
            RouteValidationError::ConflictDetected { conflicts } => {
                assert_eq!(conflicts.len(), 1);
                assert!(matches!(conflicts[0].conflict_type, ConflictType::Exact));
            },
            _ => panic!("Expected ConflictDetected error"),
        }
    }

    #[test]
    fn test_parameter_conflict_detection() {
        let validator = RouteValidator::new();
        
        let route1 = create_test_route(
            "UserController",
            "get_user",
            HttpMethod::GET,
            "/api/users/{id}",
            vec![ParamDef {
                name: "id".to_string(),
                param_type: "u32".to_string(),
                required: true,
                constraints: vec!["int".to_string()],
            }]
        );

        let route2 = create_test_route(
            "AdminController", 
            "get_admin_user",
            HttpMethod::GET,
            "/api/users/{id}",
            vec![ParamDef {
                name: "id".to_string(),
                param_type: "String".to_string(), // Different type!
                required: true,
                constraints: vec!["string".to_string()],
            }]
        );

        // Check if parameters conflict
        let conflicts = validator.parameters_conflict(&route1.parameters, &route2.parameters);
        assert!(conflicts, "Parameters with same name but different types should conflict");
    }

    #[test]
    fn test_conflict_report_generation() {
        let validator = RouteValidator::new();
        
        let route_info1 = RouteInfo {
            method: HttpMethod::GET,
            path: "/api/users/{id}".to_string(),
            controller: "UserController".to_string(),
            handler: "get_user".to_string(),
            middleware: Vec::new(),
            parameters: Vec::new(),
        };

        let route_info2 = RouteInfo {
            method: HttpMethod::GET,
            path: "/api/users/{id}".to_string(),
            controller: "AdminController".to_string(),
            handler: "get_admin_user".to_string(),
            middleware: Vec::new(),
            parameters: Vec::new(),
        };

        let conflict = RouteConflict {
            route1: route_info1,
            route2: route_info2,
            conflict_type: ConflictType::Exact,
            resolution_suggestions: vec![
                ConflictResolution::DifferentControllerPaths {
                    suggestion: "Use different paths".to_string()
                }
            ],
        };

        let report = validator.generate_conflict_report(&[conflict]);
        
        assert!(report.contains("Duplicate route definition detected"));
        assert!(report.contains("UserController::get_user"));
        assert!(report.contains("AdminController::get_admin_user"));
        assert!(report.contains("Resolution suggestions"));
    }

    #[test]
    fn test_validation_report_generation() {
        let mut validator = RouteValidator::new();
        
        // Register multiple routes
        for i in 0..5 {
            let route = create_test_route(
                &format!("Controller{}", i),
                "handler",
                HttpMethod::GET,
                &format!("/api/resource{}/{}", i, "{id}"),
                vec![ParamDef {
                    name: "id".to_string(),
                    param_type: "u32".to_string(),
                    required: true,
                    constraints: vec!["int".to_string()],
                }]
            );
            validator.register_route(route).unwrap();
        }

        let report = validator.validate_all_routes().unwrap();
        
        assert_eq!(report.total_routes, 5);
        assert_eq!(report.conflicts, 0);
        assert!(report.performance_score > 0);
    }

    #[test]
    fn test_performance_scoring() {
        let validator = RouteValidator::new();
        
        // Empty validator should have perfect score
        let score = validator.calculate_performance_score();
        assert_eq!(score, 100);
    }

    #[test]
    fn test_resolution_suggestions() {
        let validator = RouteValidator::new();
        
        let route1 = RouteInfo {
            method: HttpMethod::GET,
            path: "/api/users".to_string(),
            controller: "UserController".to_string(),
            handler: "list".to_string(),
            middleware: Vec::new(),
            parameters: Vec::new(),
        };

        let route2 = RouteInfo {
            method: HttpMethod::GET,
            path: "/api/users".to_string(),
            controller: "AdminController".to_string(),
            handler: "list_admin".to_string(),
            middleware: Vec::new(),
            parameters: Vec::new(),
        };

        let suggestions = validator.generate_resolution_suggestions(&route1, &route2, &ConflictType::Exact);
        
        assert!(!suggestions.is_empty());
        assert!(matches!(suggestions[0], ConflictResolution::DifferentControllerPaths { .. }));
    }
}