| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use crate::error::{Class, RouteError}; |
| 13 |
use crate::path::Pattern; |
| 14 |
use crate::request::{Method, Request}; |
| 15 |
use crate::response::Response; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
pub type Handler<S> = fn(&S, Request) -> Result<Response, RouteError>; |
| 34 |
|
| 35 |
|
| 36 |
struct Route<S> { |
| 37 |
method: Method, |
| 38 |
pattern: Pattern, |
| 39 |
handler: Handler<S>, |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub struct Router<S> { |
| 48 |
routes: Vec<Route<S>>, |
| 49 |
} |
| 50 |
|
| 51 |
impl<S> Router<S> { |
| 52 |
|
| 53 |
#[must_use] |
| 54 |
pub fn new() -> Self { |
| 55 |
Self { routes: Vec::new() } |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[must_use] |
| 60 |
pub fn get(self, path: &str, handler: Handler<S>) -> Self { |
| 61 |
self.route(Method::Get, path, handler) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
#[must_use] |
| 66 |
pub fn post(self, path: &str, handler: Handler<S>) -> Self { |
| 67 |
self.route(Method::Post, path, handler) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
#[must_use] |
| 72 |
pub fn delete(self, path: &str, handler: Handler<S>) -> Self { |
| 73 |
self.route(Method::Delete, path, handler) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#[must_use] |
| 78 |
pub fn put(self, path: &str, handler: Handler<S>) -> Self { |
| 79 |
self.route(Method::Put, path, handler) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#[must_use] |
| 91 |
pub fn route(mut self, method: Method, path: &str, handler: Handler<S>) -> Self { |
| 92 |
let pattern = Pattern::parse(path); |
| 93 |
|
| 94 |
assert!( |
| 95 |
!self |
| 96 |
.routes |
| 97 |
.iter() |
| 98 |
.any(|r| r.method == method && r.pattern == pattern), |
| 99 |
"route `{method} {path}` is registered twice" |
| 100 |
); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
let at = self |
| 105 |
.routes |
| 106 |
.partition_point(|r| r.pattern.specificity() >= pattern.specificity()); |
| 107 |
self.routes.insert( |
| 108 |
at, |
| 109 |
Route { |
| 110 |
method, |
| 111 |
pattern, |
| 112 |
handler, |
| 113 |
}, |
| 114 |
); |
| 115 |
self |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
pub fn handle(&self, state: &S, request: Request) -> Result<Response, RouteError> { |
| 126 |
let mut wrong_method = false; |
| 127 |
|
| 128 |
for route in &self.routes { |
| 129 |
let Some(captures) = route.pattern.match_path(&request.path) else { |
| 130 |
continue; |
| 131 |
}; |
| 132 |
if route.method != request.method { |
| 133 |
wrong_method = true; |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
return (route.handler)( |
| 138 |
state, |
| 139 |
Request { |
| 140 |
captures, |
| 141 |
..request |
| 142 |
}, |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
let (method, path) = (request.method, &request.path); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
Err(RouteError::new( |
| 154 |
Class::NotFound, |
| 155 |
if wrong_method { |
| 156 |
format!("no route for {method} {path}, though the path answers another method") |
| 157 |
} else { |
| 158 |
format!("no route for {method} {path}") |
| 159 |
}, |
| 160 |
)) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
pub fn routes(&self) -> impl Iterator<Item = (Method, &str)> { |
| 168 |
self.routes.iter().map(|r| (r.method, r.pattern.source())) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
#[must_use] |
| 173 |
pub fn len(&self) -> usize { |
| 174 |
self.routes.len() |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
#[must_use] |
| 179 |
pub fn is_empty(&self) -> bool { |
| 180 |
self.routes.is_empty() |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl<S> Default for Router<S> { |
| 185 |
fn default() -> Self { |
| 186 |
Self::new() |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl<S> std::fmt::Debug for Router<S> { |
| 191 |
|
| 192 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 193 |
f.debug_list() |
| 194 |
.entries( |
| 195 |
self.routes |
| 196 |
.iter() |
| 197 |
.map(|r| format!("{} {}", r.method, r.pattern.source())), |
| 198 |
) |
| 199 |
.finish() |
| 200 |
} |
| 201 |
} |
| 202 |
|