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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! HTTP request method predicates.
//!
//! Most predicates in this module are implemented for both [`Method`] and
//! [`Request`].
//!
//! The implementations for method are useful with projection combinators like
//! those found in the [`on`] module, while the implementations for request
//! make the common case straightforward.
//!
//! [`Method`] is the only contextual predicate in this module. When evaluated
//! against an input type, it returns a [contextual error] that can be into a
//! response with a `405` status code.
//!
//! ## Method Predicates and Method Switches
//!
//! The predicates in this module are not routing primitives.
//!
//! A method predicate classifies a request by method. When used with
//! [`barrier`], [`filter`], or [`flat_map`], it acts as stateless higher-order
//! middleware that decides whether the remaining subtree should be entered,
//! skipped, or denied.
//!
//! In contrast, the router's method [`Switch`] selects between terminal
//! middleware branches. Functions such as [`get`], [`post`], and [`put`] build
//! a decision tree for a responder attached to a route.
//!
//! Use a method predicate when middleware should be applied conditionally
//! before a responder is reached:
//!
//! ```
//! # use via::{Next, Request, Router, guard};
//! # use via::guard::method;
//! # async fn cache(request: Request, next: Next) -> via::Result {
//! # next.call(request).await
//! # }
//! let router = Router::new(|mut home| {
//! home.middleware(guard::filter(method::is_safe(), cache));
//! });
//! ```
//!
//! Prefer a router switch when different HTTP methods should be handled by
//! different terminal middleware:
//!
//! ```
//! # mod users {
//! # use via::{Next, Request};
//! # pub async fn show(_: Request, _: Next) -> via::Result { todo!() }
//! # pub async fn create(_: Request, _: Next) -> via::Result { todo!() }
//! # }
//! #
//! # use via::Router;
//! #
//! let router = Router::new(|home| {
//! // Start defining the descendants of "/".
//! let mut path = home.prefix();
//!
//! path.push("/users").assign(
//! via::get(users::show)
//! .post(users::create)
//! .or_deny()
//! );
//! });
//! ```
//!
//! [`Method`]: http::Method
//! [`Request`]: crate::Request
//! [`Switch`]: crate::router::Switch
//! [`barrier`]: crate::guard::barrier
//! [contextual error]: crate::guard::error::MethodNotAllowed
//! [`filter`]: crate::guard::filter
//! [`flat_map`]: crate::guard::flat_map
//! [`get`]: crate::get
//! [`on`]: mod@crate::guard::on
//! [`post`]: crate::post
//! [`put`]: crate::put
use MethodNotAllowed;
use ;
use crateRequest;
/// Match request methods that are
/// ["idempotent"](https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods).
///
/// This predicate succeeds when [`Method::is_idempotent`] returns `true`.
///
/// Idempotent methods may be repeated without changing the intended effect of
/// the request. This is useful for middleware that should only run when retry
/// or replay semantics are acceptable.
///
/// [`Method::is_idempotent`]: http::Method::is_idempotent
;
/// Match `GET`, `HEAD`, `OPTIONS`, and `TRACE` requests.
///
/// This predicate succeeds when [`Method::is_safe`] returns `true`.
/// ["Safe"](https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods)
/// methods are read-only by convention.
///
/// [`Method::is_safe`]: http::Method::is_safe
;
/// Match requests made with the provided method argument.
///
/// When evaluated against a [`Method`] this predicate behaves like a boolean.
/// When evaluated against a [`Request`], this predicate can return a
/// [contextual error] when the request method does not match.
///
/// [`Method`]: http::Method
/// [`Request`]: crate::Request
/// [contextual error]: crate::guard::error::MethodNotAllowed
}
)+
};
}
/// Succeeds for request methods that are
/// ["idempotent"](https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods).
///
/// This predicate succeeds for methods where [`Method::is_idempotent`] returns
/// `true`.
///
/// # Example
///
/// ```
/// use http::Method;
/// use via::guard::{Predicate, method};
///
/// assert!(method::is_idempotent().cmp(&Method::GET).is_ok());
/// assert!(method::is_idempotent().cmp(&Method::PUT).is_ok());
///
/// assert!(method::is_idempotent().cmp(&Method::POST).is_err());
/// ```
///
/// [`Method::is_idempotent`]: http::Method::is_idempotent
/// Succeeds for request methods that are not
/// ["safe"](https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods).
///
/// # Example
///
/// ```
/// use http::Method;
/// use via::guard::{Predicate, method};
///
/// assert!(method::is_mutation().cmp(&Method::POST).is_ok());
/// assert!(method::is_mutation().cmp(&Method::PATCH).is_ok());
///
/// assert!(method::is_mutation().cmp(&Method::GET).is_err());
/// ```
///
/// [`is_safe`]: crate::guard::method::is_safe
/// Succeeds for `GET`, `HEAD`, `OPTIONS`, and `TRACE` requests.
///
/// ["Safe"](https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods)
/// methods are read-only by convention.
///
/// # Example
///
/// ```
/// use http::Method;
/// use via::guard::{Predicate, method};
///
/// assert!(method::is_safe().cmp(&Method::GET).is_ok());
/// assert!(method::is_safe().cmp(&Method::HEAD).is_ok());
///
/// assert!(method::is_safe().cmp(&Method::POST).is_err());
/// ```
methods!