tinyhttp_internal/codegen/
route.rs1use crate::config::{Method, Route, ToResponse};
2use crate::request::Request;
3use crate::response::Response;
4
5#[cfg(test)]
6use std::any::Any;
7use std::net::TcpStream;
8
9#[derive(Clone, Debug)]
10pub struct BasicGetRoute {
11 path: Option<&'static str>,
12 method: Method,
13 wildcard: Option<String>,
14 is_args: Option<bool>,
15 get_body: Option<fn() -> Response>,
16 get_body_with: Option<fn(Request) -> Vec<u8>>,
17
18 get_body_with_res: Option<fn(Request) -> Response>,
19 is_ret_res: bool,
20}
21
22impl Default for BasicGetRoute {
23 fn default() -> Self {
24 BasicGetRoute {
25 path: None,
26 method: Method::GET,
27 wildcard: None,
28 is_args: None,
29 get_body: None,
30 get_body_with: None,
31 get_body_with_res: None,
32 is_ret_res: false,
33 }
34 }
35}
36
37impl ToResponse for BasicGetRoute {
38 fn to_res(&self, _res: Request, _sock: &mut TcpStream) -> Response {
39 self.get_body.unwrap()()
40 }
41}
42
43impl BasicGetRoute {
44 pub fn new() -> BasicGetRoute {
45 Default::default()
46 }
47 pub fn set_path(mut self, path: &'static str) -> Self {
48 self.path = Some(path);
49 self
50 }
51 pub fn set_method(mut self, method: Method) -> Self {
52 self.method = method;
53 self
54 }
55 pub fn set_wildcard(mut self, wildcard: String) -> Self {
56 self.wildcard = Some(wildcard);
57 self
58 }
59 pub fn set_is_args(mut self, is_args: bool) -> Self {
60 self.is_args = Some(is_args);
61 self
62 }
63 pub fn set_body(mut self, body: fn() -> Response) -> Self {
64 self.get_body = Some(body);
65 self
66 }
67 pub fn set_body_with(mut self, body: fn(Request) -> Vec<u8>) -> Self {
68 self.get_body_with = Some(body);
69 self
70 }
71 pub fn set_body_with_res(mut self, body: fn(Request) -> Response) -> Self {
72 self.get_body_with_res = Some(body);
73 self
74 }
75 pub fn set_is_ret_res(mut self, is_ret_res: bool) -> Self {
76 self.is_ret_res = is_ret_res;
77 self
78 }
79}
80
81impl Route for BasicGetRoute {
82 fn get_path(&self) -> &str {
83 self.path.unwrap()
84 }
85 fn get_method(&self) -> Method {
86 self.method
87 }
88 fn wildcard(&self) -> Option<String> {
89 self.wildcard.clone()
90 }
91 fn clone_dyn(&self) -> Box<dyn Route> {
92 Box::new(self.clone())
93 }
94
95 #[cfg(test)]
96 fn any(&self) -> &dyn Any {
97 self
98 }
99}
100
101#[derive(Clone, Debug)]
102pub struct GetRouteWithReq {
103 path: Option<&'static str>,
104 method: Method,
105 wildcard: Option<String>,
106 get_body: Option<fn(Request) -> Vec<u8>>,
107}
108
109impl Default for GetRouteWithReq {
110 fn default() -> Self {
111 GetRouteWithReq {
112 path: None,
113 method: Method::GET,
114 wildcard: None,
115 get_body: None,
116 }
117 }
118}
119
120impl ToResponse for GetRouteWithReq {
121 fn to_res(&self, res: Request, _sock: &mut TcpStream) -> Response {
122 Response::new()
123 .body(self.get_body().unwrap()(res))
124 .status_line("HTTP/1.1 200 OK\r\n")
125 .mime("text/plain")
126 }
127}
128
129impl GetRouteWithReq {
130 pub fn new() -> GetRouteWithReq {
131 Default::default()
132 }
133 pub fn set_path(mut self, path: &'static str) -> Self {
134 self.path = Some(path);
135 self
136 }
137 pub fn set_method(mut self, method: Method) -> Self {
138 self.method = method;
139 self
140 }
141 pub fn set_wildcard(mut self, wildcard: String) -> Self {
142 self.wildcard = Some(wildcard);
143 self
144 }
145 pub fn set_body(mut self, body: fn(Request) -> Vec<u8>) -> Self {
146 self.get_body = Some(body);
147 self
148 }
149
150 pub fn get_body(&self) -> Option<fn(Request) -> Vec<u8>> {
151 self.get_body
152 }
153}
154
155impl Route for GetRouteWithReq {
156 fn clone_dyn(&self) -> Box<dyn Route> {
157 Box::new(self.clone())
158 }
159 fn get_method(&self) -> Method {
160 self.method
161 }
162 fn get_path(&self) -> &str {
163 self.path.unwrap()
164 }
165 fn wildcard(&self) -> Option<String> {
166 self.wildcard.clone()
167 }
168 #[cfg(test)]
169 fn any(&self) -> &dyn Any {
170 self
171 }
172}
173
174#[derive(Clone, Debug)]
175pub struct GetRouteWithReqAndRes {
176 path: Option<&'static str>,
177 method: Method,
178 wildcard: Option<String>,
179 get_body: Option<fn(&mut Request, &mut TcpStream) -> Response>,
180}
181
182impl Default for GetRouteWithReqAndRes {
183 fn default() -> Self {
184 GetRouteWithReqAndRes {
185 path: None,
186 method: Method::GET,
187 wildcard: None,
188 get_body: None,
189 }
190 }
191}
192
193impl GetRouteWithReqAndRes {
194 pub fn new() -> GetRouteWithReqAndRes {
195 Default::default()
196 }
197 pub fn set_path(mut self, path: &'static str) -> Self {
198 self.path = Some(path);
199 self
200 }
201 pub fn set_method(mut self, method: Method) -> Self {
202 self.method = method;
203 self
204 }
205 pub fn set_wildcard(mut self, wildcard: String) -> Self {
206 self.wildcard = Some(wildcard);
207 self
208 }
209 pub fn set_body(mut self, body: fn(&'_ mut Request, &'_ mut TcpStream) -> Response) -> Self {
210 self.get_body = Some(body);
211 self
212 }
213
214 pub fn get_body(&self) -> Option<fn(&'_ mut Request, &'_ mut TcpStream) -> Response> {
215 self.get_body
216 }
217}
218
219impl ToResponse for GetRouteWithReqAndRes {
220 fn to_res(&self, mut req: Request, sock: &mut TcpStream) -> Response {
221 self.get_body().unwrap()(&mut req, sock)
222 }
223}
224
225impl Route for GetRouteWithReqAndRes {
226 fn clone_dyn(&self) -> Box<dyn Route> {
227 Box::new(self.clone())
228 }
229 fn get_method(&self) -> Method {
230 self.method
231 }
232 fn get_path(&self) -> &str {
233 self.path.unwrap()
234 }
235 fn wildcard(&self) -> Option<String> {
236 self.wildcard.clone()
237 }
238 #[cfg(test)]
239 fn any(&self) -> &dyn Any {
240 self
241 }
242}
243
244#[derive(Clone, Debug)]
245pub struct BasicPostRoute {
246 path: Option<&'static str>,
247 method: Method,
248 wildcard: Option<String>,
249 is_args: Option<bool>,
250 post_body: Option<fn() -> Response>,
251 post_body_with: Option<fn(Request) -> Vec<u8>>,
252 post_body_with_res: Option<fn(Request) -> Response>,
253 is_ret_res: bool,
254}
255
256impl Default for BasicPostRoute {
257 fn default() -> Self {
258 BasicPostRoute {
259 path: None,
260 method: Method::POST,
261 wildcard: None,
262 is_args: None,
263 post_body: None,
264 post_body_with: None,
265 post_body_with_res: None,
266 is_ret_res: false,
267 }
268 }
269}
270
271impl BasicPostRoute {
272 pub fn new() -> BasicPostRoute {
273 Default::default()
274 }
275 pub fn set_path(mut self, path: &'static str) -> Self {
276 self.path = Some(path);
277 self
278 }
279 pub fn set_method(mut self, method: Method) -> Self {
280 self.method = method;
281 self
282 }
283 pub fn set_wildcard(mut self, wildcard: String) -> Self {
284 self.wildcard = Some(wildcard);
285 self
286 }
287 pub fn set_is_args(mut self, is_args: bool) -> Self {
288 self.is_args = Some(is_args);
289 self
290 }
291 pub fn set_body(mut self, body: fn() -> Response) -> Self {
292 self.post_body = Some(body);
293 self
294 }
295 pub fn set_body_with(mut self, body: fn(Request) -> Vec<u8>) -> Self {
296 self.post_body_with = Some(body);
297 self
298 }
299 pub fn set_body_with_res(mut self, body: fn(Request) -> Response) -> Self {
300 self.post_body_with_res = Some(body);
301 self
302 }
303 pub fn set_is_ret_res(mut self, is_ret_res: bool) -> Self {
304 self.is_ret_res = is_ret_res;
305 self
306 }
307}
308
309impl ToResponse for BasicPostRoute {
310 fn to_res(&self, _req: Request, _sock: &mut TcpStream) -> Response {
311 self.post_body.unwrap()()
312 }
313}
314
315impl Route for BasicPostRoute {
316 fn get_path(&self) -> &str {
317 self.path.unwrap()
318 }
319 fn get_method(&self) -> Method {
320 self.method
321 }
322 fn wildcard(&self) -> Option<String> {
323 self.wildcard.clone()
324 }
325 fn clone_dyn(&self) -> Box<dyn Route> {
326 Box::new(self.clone())
327 }
328 #[cfg(test)]
329 fn any(&self) -> &dyn Any {
330 self
331 }
332}
333
334#[derive(Clone, Debug)]
335pub struct PostRouteWithReq {
336 path: Option<&'static str>,
337 method: Method,
338 wildcard: Option<String>,
339 post_body: Option<fn(Request) -> Vec<u8>>,
340}
341impl Default for PostRouteWithReq {
342 fn default() -> Self {
343 PostRouteWithReq {
344 path: None,
345 method: Method::POST,
346 wildcard: None,
347 post_body: None,
348 }
349 }
350}
351impl PostRouteWithReq {
352 pub fn new() -> PostRouteWithReq {
353 Default::default()
354 }
355
356 pub fn set_path(mut self, path: &'static str) -> Self {
357 self.path = Some(path);
358 self
359 }
360 pub fn set_method(mut self, method: Method) -> Self {
361 self.method = method;
362 self
363 }
364 pub fn set_wildcard(mut self, wildcard: String) -> Self {
365 self.wildcard = Some(wildcard);
366 self
367 }
368
369 pub fn set_body(mut self, body: fn(Request) -> Vec<u8>) -> Self {
370 self.post_body = Some(body);
371 self
372 }
373}
374impl ToResponse for PostRouteWithReq {
375 fn to_res(&self, req: Request, _sock: &mut TcpStream) -> Response {
376 Response::new()
377 .body(self.post_body.unwrap()(req))
378 .mime("text/plain")
379 .status_line("HTTP/1.1 200 OK\r\n")
380 }
381}
382
383impl Route for PostRouteWithReq {
384 fn clone_dyn(&self) -> Box<dyn Route> {
385 Box::new(self.clone())
386 }
387 fn get_method(&self) -> Method {
388 self.method
389 }
390 fn get_path(&self) -> &str {
391 self.path.unwrap()
392 }
393 fn wildcard(&self) -> Option<String> {
394 self.wildcard.clone()
395 }
396 #[cfg(test)]
397 fn any(&self) -> &dyn Any {
398 self
399 }
400}
401
402#[derive(Clone)]
403pub struct PostRouteWithReqAndRes {
404 path: Option<&'static str>,
405 method: Method,
406 wildcard: Option<String>,
407 post_body: Option<fn(&mut Request, &mut TcpStream) -> Response>,
408}
409
410unsafe impl Sync for PostRouteWithReqAndRes {}
411unsafe impl Send for PostRouteWithReqAndRes {}
412
413impl Default for PostRouteWithReqAndRes {
414 fn default() -> Self {
415 PostRouteWithReqAndRes {
416 path: None,
417 method: Method::POST,
418 wildcard: None,
419 post_body: None,
420 }
421 }
422}
423impl PostRouteWithReqAndRes {
424 pub fn new() -> PostRouteWithReqAndRes {
425 Default::default()
426 }
427
428 pub fn set_path(mut self, path: &'static str) -> Self {
429 self.path = Some(path);
430 self
431 }
432 pub fn set_method(mut self, method: Method) -> Self {
433 self.method = method;
434 self
435 }
436 pub fn set_wildcard(mut self, wildcard: String) -> Self {
437 self.wildcard = Some(wildcard);
438 self
439 }
440
441 pub fn set_body(mut self, body: fn(&'_ mut Request, &'_ mut TcpStream) -> Response) -> Self {
442 self.post_body = Some(body);
443 self
444 }
445}
446impl ToResponse for PostRouteWithReqAndRes {
447 fn to_res(&self, mut req: Request, sock: &mut TcpStream) -> Response {
448 self.post_body.unwrap()(&mut req, sock)
449 }
450}
451
452impl Route for PostRouteWithReqAndRes {
453 fn clone_dyn(&self) -> Box<dyn Route> {
454 Box::new(self.clone())
455 }
456 fn get_method(&self) -> Method {
457 self.method
458 }
459 fn get_path(&self) -> &str {
460 self.path.unwrap()
461 }
462 fn wildcard(&self) -> Option<String> {
463 self.wildcard.clone()
464 }
465 #[cfg(test)]
466 fn any(&self) -> &dyn Any {
467 self
468 }
469}