little_web/route/
router.rs1use std::borrow::BorrowMut;
2use std::fmt::Display;
3
4use serde::{Serialize, Deserialize};
5
6use crate::{Method, handlers,handler, parser::{HttpRequest, HttpResponse}, Handler};
7
8#[derive(Debug)]
9pub struct Node {
10 pub path:String,
11 pub handlers: handlers,
12 }
14
15impl Node {
16 pub fn new(path:String, handlers: handlers) -> Node {
17 return Node { path: path, handlers: handlers }
18 }
19}
20
21impl Clone for Node {
22 fn clone(&self) -> Self {
23 Self { path: self.path.clone(), handlers: self.handlers.clone() }
25 }
26}
27
28
29#[derive(Debug)]
30pub struct MethodTree {
31 method: Method,
32 nodes: Vec<Node>,
33}
34
35impl Clone for MethodTree {
36 fn clone(&self) -> Self {
37 Self { method: self.method.clone(), nodes: self.nodes.clone() }
38 }
39}
40
41
42
43impl MethodTree {
44 pub fn new(method: Method) -> MethodTree {
45 let nodes = vec![];
46 return MethodTree { method: method, nodes: nodes }
47 }
48
49
50 pub fn addRoute(&mut self, path: String, handlers: handlers) {
51 let mut start = 0;
52 let end = path.len();
53 if end == 0 {
54 panic!("can`t add a empty path")
55 }
56 for s in path.chars() {
57 if start == 0 && s != '/' {
58 panic!("path {} is not start with /", path)
59 }
60 if s != '/' {
61 if s == ':' || s == '*' {
62 panic!("path {} is invalid", path)
63 }
64 }
65 start += 1;
66 }
67
68 let path = path.trim_end_matches("/");
69
70 for node in &self.nodes {
71 if node.path == path {
72 panic!("cant use same path with same method")
73 }
74 }
75 self.nodes.insert(0, Node::new(path.to_owned(), handlers));
76
77 }
79
80 pub fn get<'a>(&'a self,path:String) ->Option<&'a Node> {
81 for node in &self.nodes {
82 if node.path == path {
83 return Some(node);
84 }
85 }
86 return None
87 }
88
89
90}
91
92
93#[derive(Debug)]
94pub struct MethodTrees{
95 data: Vec<MethodTree>
96}
97
98impl Clone for MethodTrees {
99 fn clone(&self) -> Self {
100 Self { data: self.data.clone() }
101 }
102}
103
104
105impl MethodTrees {
106
107 pub fn new() -> MethodTrees {
108 return MethodTrees{data:vec![]};
109 }
110
111
112 pub fn addRoute(&mut self,method:Method, path: String, handlers: handlers) {
113 let end = self.data.len();
114 let mut index = end + 1;
115 for i in 0..end {
116 let a:&MethodTree = self.data.get(i).unwrap();
117 if a.method == method {
118 index = i;
119 break;
120 }
121 }
122
123 if index > end {
124 let mut tree = MethodTree::new(method);
125 tree.addRoute(path, handlers);
126 self.data.push(tree);
127
128 }else{
129 let tree:&mut MethodTree = self.data.get_mut(index).unwrap();
130 tree.addRoute(path, handlers);
131 }
132
133 }
134
135 pub fn get<'a>(&'a self,method:Method) ->Option<&'a MethodTree> {
136 for tree in &self.data {
137 if tree.method == method {
138 return Some(tree);
140 }
141 }
142 return None
143 }
144
145}
146
147
148
149pub struct RouteInfo {
150 pub method: Method,
151 pub path: String,
152 pub handlerFunc: handler,
153}
154
155impl RouteInfo {
156 pub fn new<T>(method: Method, path: &str, handlerFunc: T)->RouteInfo
157 where T: Handler<HttpRequest, Output = HttpResponse>
158 {
159 let handlerFunc = Box::new(handlerFunc);
160 let path = path.to_owned();
161 return RouteInfo{method,path, handlerFunc};
162 }
163
164}
165