fast_router/
lib.rs

1pub mod router;
2
3// use std::{
4//     borrow::BorrowMut,
5//     cell::Cell,
6//     collections::{HashMap, LinkedList},
7//     hash::Hash,
8//     ops::Deref,
9//     ptr::NonNull,
10//     str::FromStr,
11//     sync::Arc,
12//     vec,
13// };
14
15// type Handler = fn(&str);
16
17// #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
18// enum Method {
19//     TRACE,
20//     HEAD,
21//     GET,
22//     POST,
23//     PUT,
24//     PATCH,
25//     DELETE,
26//     OPTIONS,
27//     ANY,
28// }
29
30// impl From<Method> for String {
31//     fn from(v: Method) -> Self {
32//         format!("{:?}", v)
33//     }
34// }
35
36// impl From<&str> for Method {
37//     fn from(s: &str) -> Self {
38//         match s.to_uppercase().as_str() {
39//             "TRACE" => Self::TRACE,
40//             "HEAD" => Self::HEAD,
41//             "GET" => Self::GET,
42//             "POST" => Self::POST,
43//             "PUT" => Self::PUT,
44//             "PATCH" => Self::PATCH,
45//             "DELETE" => Self::DELETE,
46//             "OPTIONS" => Self::OPTIONS,
47//             "ANY" => Self::ANY,
48//             v => panic!("不存在这个类型:{}", v),
49//         }
50//     }
51// }
52
53// struct Router {
54//     routes: HashMap<String, HashMap<String, Route>>,
55// }
56
57// impl Router {
58//     fn new() -> Self {
59//         let mut routes: HashMap<String, HashMap<String, Route>> = HashMap::new();
60
61//         let k = Method::TRACE.into();
62
63//         routes.insert(k, HashMap::new());
64//         routes.insert(Method::HEAD.into(), HashMap::new());
65//         routes.insert(Method::GET.into(), HashMap::new());
66//         routes.insert(Method::POST.into(), HashMap::new());
67//         routes.insert(Method::PUT.into(), HashMap::new());
68//         routes.insert(Method::PATCH.into(), HashMap::new());
69//         routes.insert(Method::DELETE.into(), HashMap::new());
70//         routes.insert(Method::OPTIONS.into(), HashMap::new());
71//         routes.insert(Method::ANY.into(), HashMap::new());
72//         Router {
73//             routes,
74//         }
75//     }
76
77//     fn add(&mut self, method: Method, uri: &str, handler: Handler) {
78//         let nodes: Vec<&str> = uri.split('/').filter(|v| !v.is_empty()).collect();
79
80//         let k: String = method.into();
81//         let mut routes = self.routes.get_mut(&k).unwrap();
82
83//         for v in nodes {
84//             // 如果是已经存在的路由,就跳到下一个
85//             if let Some(v) = routes.get(v) {
86//                 routes = unsafe { v.children_routes.unwrap().as_mut() };
87//                 continue;
88//             }
89
90//             let route = Route::new(v);
91//             let v = Box::leak(Box::new(&route));
92//             let tr = unsafe { v.children_routes.unwrap().as_mut() };
93//             routes.insert(method.into(), route);
94//             routes = tr;
95//         }
96//     }
97// }
98
99// /// 路由节点
100// #[derive(Default)]
101// struct Route {
102//     node: String, // 节点名称, 之包含地址中的一段,例: /admin/login就是两个节点 node就保存 admin或者login
103//     children_routes: Option<NonNull<HashMap<String, Route>>>, // 下级分支节点
104//     handlers: Option<Vec<Handler>>, // 节点对应的处理器
105//     is_end: bool, // 是否是最后一个节点, true表示是最后一个节点
106// }
107
108// impl Route {
109//     fn new(node: &str) -> Self {
110//         let v: HashMap<String, Route> = HashMap::new();
111//         Route {
112//             node: node.to_string(),
113//             children_routes: Some(Box::leak(Box::new(v)).into()),
114//             handlers: None,
115//             is_end: false,
116//         }
117//     }
118// }
119
120// #[derive(Default)]
121// struct RouteGroup {
122//     route: Route,
123// }
124
125// impl RouteGroup {}
126
127// #[cfg(test)]
128// mod tests {
129//     use std::collections::HashMap;
130
131//     use crate::{Method, Route, Router};
132
133//     #[test]
134//     fn test_router_add() {
135
136//         let mut r = Router::new();
137
138 
139//         r.add(Method::GET, "/admin/user/123", |v| {});
140//         r.add(Method::GET, "/admin/name/123", |v| {});
141
142//         let k: String = Method::GET.into();
143//         let root = r.routes.get(&k).unwrap();
144//         println!("len:{:?}", root.len());
145
146//         let c = root.get("GET").unwrap();
147//         for v in c.children_routes{
148//             let v =unsafe {
149//                 v.as_ref()
150//             };
151//             for v in v{
152//                 println!("{:?}", v.0);
153//             }
154            
155//         }
156
157//         // fn show(r: &Route) {
158//         //     println!("{}", r.node);
159//         //     print!("\t");
160
161//         //     if let None = r.children_routes {
162//         //         return;
163//         //     }
164
165//         //     let cs = unsafe { r.children_routes.unwrap().as_mut() };
166//         //     for v in cs {
167//         //         show(v);
168//         //     }
169//         // }
170//         // for v in r.routes {
171//         //     for j in &v.1 {
172//         //         show(j);
173//         //     }
174//         // }
175//     }
176
177//     #[test]
178//     fn test() {
179//         let v: String = Method::DELETE.into();
180//         println!("{}", v);
181//     }
182// }