1pub mod params;
11pub mod router;
12pub mod group;
13
14pub use router::{Router as ElifRouter, Route, RouteBuilder};
15pub use params::{PathParams, RouteParam, ParamError, ParamType};
16pub use group::{RouteGroup, GroupBuilder};
17
18use axum::http::Method;
19use std::collections::HashMap;
20use serde::{Serialize, Deserialize};
21
22#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub enum HttpMethod {
25 GET,
26 POST,
27 PUT,
28 DELETE,
29 PATCH,
30 HEAD,
31 OPTIONS,
32 TRACE,
33}
34
35impl From<Method> for HttpMethod {
36 fn from(method: Method) -> Self {
37 match method {
38 Method::GET => HttpMethod::GET,
39 Method::POST => HttpMethod::POST,
40 Method::PUT => HttpMethod::PUT,
41 Method::DELETE => HttpMethod::DELETE,
42 Method::PATCH => HttpMethod::PATCH,
43 Method::HEAD => HttpMethod::HEAD,
44 Method::OPTIONS => HttpMethod::OPTIONS,
45 Method::TRACE => HttpMethod::TRACE,
46 _ => HttpMethod::GET, }
48 }
49}
50
51impl From<HttpMethod> for Method {
52 fn from(method: HttpMethod) -> Self {
53 match method {
54 HttpMethod::GET => Method::GET,
55 HttpMethod::POST => Method::POST,
56 HttpMethod::PUT => Method::PUT,
57 HttpMethod::DELETE => Method::DELETE,
58 HttpMethod::PATCH => Method::PATCH,
59 HttpMethod::HEAD => Method::HEAD,
60 HttpMethod::OPTIONS => Method::OPTIONS,
61 HttpMethod::TRACE => Method::TRACE,
62 }
63 }
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct RouteInfo {
69 pub name: Option<String>,
70 pub path: String,
71 pub method: HttpMethod,
72 pub params: Vec<String>,
73 pub group: Option<String>,
74}
75
76#[derive(Debug, Default)]
78pub struct RouteRegistry {
79 routes: HashMap<String, RouteInfo>,
80 named_routes: HashMap<String, String>, }
82
83impl RouteRegistry {
84 pub fn new() -> Self {
85 Self {
86 routes: HashMap::new(),
87 named_routes: HashMap::new(),
88 }
89 }
90
91 pub fn register(&mut self, route_id: String, info: RouteInfo) {
92 if let Some(ref name) = info.name {
93 self.named_routes.insert(name.clone(), route_id.clone());
94 }
95 self.routes.insert(route_id, info);
96 }
97
98 pub fn get(&self, route_id: &str) -> Option<&RouteInfo> {
99 self.routes.get(route_id)
100 }
101
102 pub fn get_by_name(&self, name: &str) -> Option<&RouteInfo> {
103 self.named_routes.get(name)
104 .and_then(|id| self.routes.get(id))
105 }
106
107 pub fn all_routes(&self) -> &HashMap<String, RouteInfo> {
108 &self.routes
109 }
110}