1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::Router;
use trillium::{http_types::Method, Handler};
macro_rules! method_ref {
($fn_name:ident, $method:ident) => {
method_ref!(
$fn_name,
$method,
concat!(
"Registers a handler for the ",
stringify!($fn_name),
" http method.
```
# use trillium::Conn;
# use trillium_router::Router;
let router = Router::build(|mut router| {
router.",
stringify!($fn_name),
"(\"/some/route\", |conn: Conn| async move {
conn.ok(\"success\")
});
});
use trillium_testing::{methods::",
stringify!($fn_name),
", assert_ok, assert_not_handled};
assert_ok!(",
stringify!($fn_name),
"(\"/some/route\").on(&router), \"success\");
assert_not_handled!(",
stringify!($fn_name),
"(\"/other/route\").on(&router));
```
"
)
);
};
($fn_name:ident, $method:ident, $doc_comment:expr) => {
#[doc = $doc_comment]
pub fn $fn_name(&mut self, path: &'static str, handler: impl Handler) {
self.0.add(path, Method::$method, handler);
}
};
}
#[derive(Debug)]
pub struct RouterRef<'r>(&'r mut Router);
impl<'r> RouterRef<'r> {
method_ref!(get, Get);
method_ref!(post, Post);
method_ref!(put, Put);
method_ref!(delete, Delete);
method_ref!(patch, Patch);
pub fn any(&mut self, path: &'static str, handler: impl Handler) {
self.0.add_any(path, handler)
}
pub(crate) fn new(router: &'r mut Router) -> Self {
Self(router)
}
}