1use crate::Router;
6use std::marker::PhantomData;
7
8pub struct RouterBuilder<S = ()> {
10 router: Router<S>,
12 _marker: PhantomData<S>,
14}
15
16impl<S> RouterBuilder<S>
17where
18 S: Clone + Send + Sync + 'static,
19{
20 pub fn new() -> Self
22 where
23 S: Default,
24 {
25 Self::from_router(Router::with_state(S::default()))
26 }
27
28 pub fn from_router(router: Router<S>) -> Self {
30 Self { router, _marker: PhantomData }
31 }
32
33 pub fn nest<T>(mut self, prefix: &str, router: T) -> Self
35 where
36 T: Into<Router<S>>,
37 {
38 self.router = self.router.nest_service(prefix, router);
39 self
40 }
41
42 pub fn merge(mut self, other: Router<S>) -> Self {
44 self.router = self.router.merge(other);
45 self
46 }
47
48 pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
50 where
51 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
52 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
53 {
54 self.router.add_route(http::Method::GET, path, handler);
55 self
56 }
57
58 pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
60 where
61 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
62 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
63 {
64 self.router.add_route(http::Method::POST, path, handler);
65 self
66 }
67
68 pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
70 where
71 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
72 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
73 {
74 self.router.add_route(http::Method::PUT, path, handler);
75 self
76 }
77
78 pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
80 where
81 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
82 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
83 {
84 self.router.add_route(http::Method::DELETE, path, handler);
85 self
86 }
87
88 pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
90 where
91 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
92 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
93 {
94 self.router.add_route(http::Method::PATCH, path, handler);
95 self
96 }
97
98 pub fn options<H, T>(mut self, path: &str, handler: H) -> Self
100 where
101 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
102 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
103 {
104 self.router.add_route(http::Method::OPTIONS, path, handler);
105 self
106 }
107
108 pub fn head<H, T>(mut self, path: &str, handler: H) -> Self
110 where
111 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
112 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
113 {
114 self.router.add_route(http::Method::HEAD, path, handler);
115 self
116 }
117
118 pub fn trace<H, T>(mut self, path: &str, handler: H) -> Self
120 where
121 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
122 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
123 {
124 self.router.add_route(http::Method::TRACE, path, handler);
125 self
126 }
127
128 pub fn build(self) -> Router<S> {
130 self.router
131 }
132
133 pub fn into_inner(self) -> Router<S> {
135 self.build()
136 }
137}
138
139impl<S> Default for RouterBuilder<S>
140where
141 S: Clone + Send + Sync + 'static + Default,
142{
143 fn default() -> Self {
144 Self::new()
145 }
146}
147
148impl<S> From<Router<S>> for RouterBuilder<S>
149where
150 S: Clone + Send + Sync + 'static,
151{
152 fn from(router: Router<S>) -> Self {
153 Self::from_router(router)
154 }
155}
156
157impl<S> From<RouterBuilder<S>> for Router<S>
158where
159 S: Clone + Send + Sync + 'static,
160{
161 fn from(builder: RouterBuilder<S>) -> Self {
162 builder.build()
163 }
164}
165
166pub struct MethodRouter<S = ()> {
168 _marker: PhantomData<S>,
170}
171
172impl<S> Clone for MethodRouter<S> {
173 fn clone(&self) -> Self {
174 Self { _marker: PhantomData }
175 }
176}
177
178impl<S> Default for MethodRouter<S> {
179 fn default() -> Self {
180 Self { _marker: PhantomData }
181 }
182}
183
184pub fn get<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
186where
187 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
188 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
189 S: Clone + Send + Sync + 'static,
190{
191 move |router, path| {
192 router.add_route(http::Method::GET, path, handler);
193 }
194}
195
196pub fn post<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
198where
199 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
200 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
201 S: Clone + Send + Sync + 'static,
202{
203 move |router, path| {
204 router.add_route(http::Method::POST, path, handler);
205 }
206}
207
208pub fn put<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
210where
211 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
212 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
213 S: Clone + Send + Sync + 'static,
214{
215 move |router, path| {
216 router.add_route(http::Method::PUT, path, handler);
217 }
218}
219
220pub fn delete<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
222where
223 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
224 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
225 S: Clone + Send + Sync + 'static,
226{
227 move |router, path| {
228 router.add_route(http::Method::DELETE, path, handler);
229 }
230}
231
232pub fn patch<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
234where
235 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
236 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
237 S: Clone + Send + Sync + 'static,
238{
239 move |router, path| {
240 router.add_route(http::Method::PATCH, path, handler);
241 }
242}
243
244pub fn options<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
246where
247 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
248 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
249 S: Clone + Send + Sync + 'static,
250{
251 move |router, path| {
252 router.add_route(http::Method::OPTIONS, path, handler);
253 }
254}
255
256pub fn head<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
258where
259 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
260 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
261 S: Clone + Send + Sync + 'static,
262{
263 move |router, path| {
264 router.add_route(http::Method::HEAD, path, handler);
265 }
266}
267
268pub fn trace<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
270where
271 H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
272 T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
273 S: Clone + Send + Sync + 'static,
274{
275 move |router, path| {
276 router.add_route(http::Method::TRACE, path, handler);
277 }
278}
279
280pub fn health_check_router() -> Router {
282 Router::new()
283}