elif_http/routing/
router.rs

1//! Core routing functionality
2
3use super::{HttpMethod, RouteInfo, RouteRegistry, params::{ParamExtractor, ParamType}};
4use axum::{
5    Router as AxumRouter,
6    routing::{get, post, put, delete, patch},
7    handler::Handler,
8    response::IntoResponse,
9};
10use std::collections::HashMap;
11use std::sync::{Arc, Mutex};
12
13/// Main router for the elif.rs framework
14#[derive(Debug)]
15pub struct Router<S = ()> 
16where 
17    S: Clone + Send + Sync + 'static,
18{
19    axum_router: AxumRouter<S>,
20    registry: Arc<Mutex<RouteRegistry>>,
21    route_counter: Arc<Mutex<usize>>,
22}
23
24impl<S> Router<S>
25where
26    S: Clone + Send + Sync + 'static,
27{
28    /// Create a new router
29    pub fn new() -> Self {
30        Self {
31            axum_router: AxumRouter::new(),
32            registry: Arc::new(Mutex::new(RouteRegistry::new())),
33            route_counter: Arc::new(Mutex::new(0)),
34        }
35    }
36
37    /// Create a new router with state
38    pub fn with_state(state: S) -> Self {
39        Self {
40            axum_router: AxumRouter::new().with_state(state),
41            registry: Arc::new(Mutex::new(RouteRegistry::new())),
42            route_counter: Arc::new(Mutex::new(0)),
43        }
44    }
45
46    /// Generate a unique route ID
47    fn next_route_id(&self) -> String {
48        let mut counter = self.route_counter.lock().unwrap();
49        *counter += 1;
50        format!("route_{}", counter)
51    }
52
53    /// Register a route with the registry
54    fn register_route(&self, method: HttpMethod, path: &str, name: Option<String>) -> String {
55        let route_id = self.next_route_id();
56        let params = self.extract_param_names(path);
57        
58        let route_info = RouteInfo {
59            name: name.clone(),
60            path: path.to_string(),
61            method,
62            params,
63            group: None, // TODO: Support groups
64        };
65        
66        self.registry.lock().unwrap().register(route_id.clone(), route_info);
67        route_id
68    }
69
70    /// Extract parameter names from a route path
71    fn extract_param_names(&self, path: &str) -> Vec<String> {
72        path.split('/')
73            .filter_map(|segment| {
74                if segment.starts_with('{') && segment.ends_with('}') {
75                    Some(segment[1..segment.len()-1].to_string())
76                } else {
77                    None
78                }
79            })
80            .collect()
81    }
82
83    /// Add a GET route
84    pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
85    where
86        H: Handler<T, S>,
87        T: 'static,
88    {
89        self.register_route(HttpMethod::GET, path, None);
90        self.axum_router = self.axum_router.route(path, get(handler));
91        self
92    }
93
94    /// Add a POST route
95    pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
96    where
97        H: Handler<T, S>,
98        T: 'static,
99    {
100        self.register_route(HttpMethod::POST, path, None);
101        self.axum_router = self.axum_router.route(path, post(handler));
102        self
103    }
104
105    /// Add a PUT route
106    pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
107    where
108        H: Handler<T, S>,
109        T: 'static,
110    {
111        self.register_route(HttpMethod::PUT, path, None);
112        self.axum_router = self.axum_router.route(path, put(handler));
113        self
114    }
115
116    /// Add a DELETE route
117    pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
118    where
119        H: Handler<T, S>,
120        T: 'static,
121    {
122        self.register_route(HttpMethod::DELETE, path, None);
123        self.axum_router = self.axum_router.route(path, delete(handler));
124        self
125    }
126
127    /// Add a PATCH route
128    pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
129    where
130        H: Handler<T, S>,
131        T: 'static,
132    {
133        self.register_route(HttpMethod::PATCH, path, None);
134        self.axum_router = self.axum_router.route(path, patch(handler));
135        self
136    }
137
138    /// Merge another router
139    pub fn merge(mut self, other: AxumRouter<S>) -> Self {
140        self.axum_router = self.axum_router.merge(other);
141        self
142    }
143
144    /// Nest routes under a path prefix
145    pub fn nest(mut self, path: &str, router: AxumRouter<S>) -> Self {
146        self.axum_router = self.axum_router.nest(path, router);
147        self
148    }
149
150    /// Get the underlying Axum router
151    pub fn into_axum_router(self) -> AxumRouter<S> {
152        self.axum_router
153    }
154
155    /// Get route registry for introspection
156    pub fn registry(&self) -> Arc<Mutex<RouteRegistry>> {
157        Arc::clone(&self.registry)
158    }
159
160    /// Generate URL for a named route
161    pub fn url_for(&self, name: &str, params: &HashMap<String, String>) -> Option<String> {
162        let registry = self.registry.lock().unwrap();
163        if let Some(route) = registry.get_by_name(name) {
164            let mut url = route.path.clone();
165            for (key, value) in params {
166                url = url.replace(&format!("{{{}}}", key), value);
167            }
168            Some(url)
169        } else {
170            None
171        }
172    }
173}
174
175impl<S> Default for Router<S>
176where
177    S: Clone + Send + Sync + 'static,
178{
179    fn default() -> Self {
180        Self::new()
181    }
182}
183
184/// Builder for creating routes with additional metadata
185#[derive(Debug, Default)]
186pub struct RouteBuilder {
187    name: Option<String>,
188    param_types: HashMap<String, ParamType>,
189    middleware: Vec<String>, // Placeholder for future middleware support
190}
191
192impl RouteBuilder {
193    pub fn new() -> Self {
194        Self::default()
195    }
196
197    /// Set route name for URL generation
198    pub fn name(mut self, name: &str) -> Self {
199        self.name = Some(name.to_string());
200        self
201    }
202
203    /// Add parameter type specification
204    pub fn param(mut self, name: &str, param_type: ParamType) -> Self {
205        self.param_types.insert(name.to_string(), param_type);
206        self
207    }
208
209    /// Build the route configuration
210    pub fn build(self) -> Route {
211        Route {
212            name: self.name,
213            param_types: self.param_types,
214            middleware: self.middleware,
215        }
216    }
217}
218
219/// Route configuration
220#[derive(Debug)]
221pub struct Route {
222    pub name: Option<String>,
223    pub param_types: HashMap<String, ParamType>,
224    pub middleware: Vec<String>,
225}
226
227impl Route {
228    pub fn builder() -> RouteBuilder {
229        RouteBuilder::new()
230    }
231
232    /// Create parameter extractor for this route
233    pub fn param_extractor(&self) -> ParamExtractor {
234        let mut extractor = ParamExtractor::new();
235        for (name, param_type) in &self.param_types {
236            extractor = extractor.param(name, param_type.clone());
237        }
238        extractor
239    }
240}
241
242#[cfg(test)]
243mod tests {
244    use super::*;
245    use axum::response::Html;
246
247    async fn handler() -> Html<&'static str> {
248        Html("<h1>Hello, World!</h1>")
249    }
250
251    #[test]
252    fn test_router_creation() {
253        let router = Router::<()>::new()
254            .get("/", handler)
255            .post("/users", handler)
256            .get("/users/{id}", handler);
257        
258        let registry = router.registry();
259        let reg = registry.lock().unwrap();
260        assert_eq!(reg.all_routes().len(), 3);
261    }
262
263    #[test]
264    fn test_param_extraction() {
265        let router = Router::<()>::new();
266        let params = router.extract_param_names("/users/{id}/posts/{slug}");
267        assert_eq!(params, vec!["id", "slug"]);
268    }
269
270    #[test]
271    fn test_url_generation() {
272        let mut router = Router::<()>::new().get("/users/{id}/posts/{slug}", handler);
273        
274        // Manually add a named route to registry for testing
275        {
276            let mut registry = router.registry.lock().unwrap();
277            let route_info = RouteInfo {
278                name: Some("user.posts.show".to_string()),
279                path: "/users/{id}/posts/{slug}".to_string(),
280                method: HttpMethod::GET,
281                params: vec!["id".to_string(), "slug".to_string()],
282                group: None,
283            };
284            registry.register("test_route".to_string(), route_info);
285        }
286        
287        let mut params = HashMap::new();
288        params.insert("id".to_string(), "123".to_string());
289        params.insert("slug".to_string(), "hello-world".to_string());
290        
291        let url = router.url_for("user.posts.show", &params);
292        assert_eq!(url, Some("/users/123/posts/hello-world".to_string()));
293    }
294}