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
//! Unified Router with hierarchical routing support
//!
//! This module provides a unified router that supports:
//! - **High-performance O(m) route matching** using matchit Radix Tree (m = path length)
//! - Nested routers with automatic prefix inheritance
//! - Namespace-based URL reversal
//! - Middleware and DI context propagation
//! - Integration with ViewSets, functions, and class-based views
//!
//! # Performance Characteristics
//!
//! The router uses [matchit](https://docs.rs/matchit) for O(m) route matching where m is the path length:
//! - Route lookup: O(m) - Independent of the number of registered routes
//! - Route compilation: O(n) - Done once at startup where n is the number of routes
//! - Memory: Efficient through Radix Tree's prefix sharing
//!
//! With 1000+ routes, matchit provides 3-5x better performance compared to naive O(n×m) linear search.
//!
//! # Implementation Details
//!
//! Each HTTP method has its own matchit router for optimal performance:
//! - `GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`, `OPTIONS`
//! - Routes are compiled lazily on first access (thread-safe with RwLock)
//! - Parameters are extracted directly from matchit's Params
//!
//! # Module Layout
//!
//! The implementation is split across focused submodules to keep each file
//! small and reviewable:
//!
//! - `types` — `MiddlewareInfo`, `RouteInfo`, `FunctionRoute`, `ViewRoute`,
//! `RouteHandler`, `RouteMatch`, and the `join_path` helper
//! - `builder` — constructors and builder-style configuration
//! (`new`, `with_prefix`, `with_namespace`, `with_di_context`,
//! `with_middleware`, `exclude`, `mount`, `group`)
//! - `registration` — route registration entry points
//! (`function`, `handler`, `route`, `viewset`, `endpoint`, `view`,
//! `with_route_middleware`)
//! - `compile` — matchit compilation and `validate_*` helpers
//! - `introspection` — read-only accessors, `get_all_routes`,
//! `register_all_routes`, `reverse`
//! - `dispatch` — `resolve`, `match_own_routes_*`, `path_exists_for_any_method`
//! - `router_impls` — `Debug`, `Default`, `Handler`, `RegisterViewSet`
//! - `handlers` — `FunctionHandler` and `ViewSetHandler` adapters
//! - `matching` — `path_matches` and `extract_params` utilities
//! - `global` — global router registry used by `showurls`
use crateUrlReverser;
use Router as MatchitRouter;
use InjectionContext;
use Middleware;
use HashMap;
use ;
use ;
pub use ;
pub use FunctionHandler;
pub use ;
pub use ;
/// Unified router with hierarchical routing support
///
/// Supports multiple API styles:
/// - FastAPI-style: Function-based routes
/// - DRF-style: ViewSets with automatic CRUD
/// - Django-style: Class-based views
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::ServerRouter;
/// use hyper::Method;
/// # use reinhardt_http::{Request, Response, Result};
///
/// # async fn example() -> Result<()> {
/// // Create a users sub-router
/// let users_router = ServerRouter::new()
/// .with_namespace("users")
/// .function("/export/", Method::GET, |_req| async { Ok(Response::ok()) });
///
/// // Verify users router has namespace
/// assert_eq!(users_router.namespace(), Some("users"));
///
/// // Create root router
/// let router = ServerRouter::new()
/// .with_prefix("/api/v1/")
/// .with_namespace("v1")
/// .function("/health/", Method::GET, |_req| async { Ok(Response::ok()) })
/// .mount("/users/", users_router);
///
/// // Verify root router configuration
/// assert_eq!(router.prefix(), "/api/v1/");
/// assert_eq!(router.namespace(), Some("v1"));
///
/// // Generated URLs:
/// // /api/v1/health/
/// // /api/v1/users/export/
/// # Ok(())
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(example()).unwrap();
/// ```