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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use ;
type BoxedResultFut<'x> = ;
/// The wrapper for use async function or closure as a middleware.
///
/// This is the type when you use macro [`m`] , **Do Not** use this type directly!
///
/// [`m`]: ../macro.m.html
/// Writer middleware easily.
///
/// It's a macro to let you easily write middleware use closure and syntax like JavaScript's
/// arrow function, or convert a async fn to a middleware use the `m!(async_func_name)` syntax.
///
/// It returns a [`M`] instance, which implement [`Middleware`] trait.
///
/// ## Examples
///
/// ### Convert a async function to middleware
///
/// ```
/// # use amiya::{Context, Result, m};
/// async fn response(mut ctx: Context<'_, ()>) -> Result {
/// ctx.next().await?;
/// ctx.resp.set_body("Hello world");
/// Ok(())
/// }
///
/// let app = amiya::new().uses(m!(response));
/// ```
///
/// ### Convert a block to middleware
///
/// Syntax: `<Context parameter name> [: Extra data type] => { <your code> }`
///
/// Default extra data type is `()`, same bellow.
///
/// ```
/// # use amiya::m;
/// // | this `: ()` can be omitted
/// // v
/// let app = amiya::new().uses(m!(ctx: () => {
/// ctx.next().await?;
/// ctx.resp.set_body("Hello world");
/// Ok(())
/// }));
/// ```
///
/// ### Convert a expr to middleware
///
/// Syntax: `<Context parameter name> [: Extra data type] => <The expr>`
///
/// ```
/// # use amiya::{Context, Result, m};
/// async fn response(msg: &'static str, mut ctx: Context<'_, ()>) -> Result {
/// ctx.next().await?;
/// ctx.resp.set_body(msg);
/// Ok(())
/// }
///
/// let app = amiya::new().uses(m!(ctx => response("Hello World", ctx).await));
/// ```
///
/// ### Convert statements to middleware
///
/// Syntax: `<Context parameter name> [: Extra data type] => <statements>`
///
/// ```
/// # use amiya::m;
/// let app = amiya::new().uses(m!(ctx => ctx.resp.set_body("Hello World");));
/// ```
///
/// Notice you do not return a value here, because a `Ok(())` is auto added.
///
/// This is expand to:
///
/// ```text
/// ctx.resp.set_body("Hello World");
/// Ok(())
/// ```
///
/// [`M`]: middleware/struct.M.html
/// [`Middleware`]: middleware/trait.Middleware.html