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
//! Declarative routing macro.
//!
//! The [`routes!`] macro builds an [`crate::state::AppWithState`],
//! [`crate::async_state::AsyncAppWithState`], or any other builder that
//! exposes `.get()`, `.post()`, `.put()`, `.patch()`, and `.delete()` methods.
/// Build a routing app from a declarative table.
///
/// Syntax:
/// ```text
/// routes! {
/// <builder>,
/// METHOD "path" => handler,
/// ...
/// }
/// ```
///
/// `METHOD` must be one of `GET`, `POST`, `PUT`, `PATCH`, or `DELETE` (all
/// caps). The builder receives one `.method(path, handler)` call per entry,
/// chained in declaration order.
///
/// Handlers may be named functions or closures — anything accepted by the
/// corresponding builder method. A trailing comma after the last route is
/// optional.
///
/// # Example — stateful sync app
///
/// ```rust
/// use rust_web_server::app::App;
/// use rust_web_server::core::New;
/// use rust_web_server::routes;
/// use rust_web_server::request::Request;
/// use rust_web_server::router::PathParams;
/// use rust_web_server::server::ConnectionInfo;
/// use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
///
/// struct Db;
///
/// // AppWithState<S> passes &S (not &Arc<S>) to the handler.
/// fn list_users(
/// _req: &Request,
/// _params: &PathParams,
/// _conn: &ConnectionInfo,
/// _state: &Db,
/// ) -> Response {
/// let mut r = Response::new();
/// r.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
/// r.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
/// r
/// }
///
/// fn create_user(
/// _req: &Request,
/// _params: &PathParams,
/// _conn: &ConnectionInfo,
/// _state: &Db,
/// ) -> Response {
/// let mut r = Response::new();
/// r.status_code = *STATUS_CODE_REASON_PHRASE.n201_created.status_code;
/// r.reason_phrase = STATUS_CODE_REASON_PHRASE.n201_created.reason_phrase.to_string();
/// r
/// }
///
/// let app = routes! {
/// App::with_state(Db),
/// GET "/users" => list_users,
/// POST "/users" => create_user,
/// };
/// ```
///
/// # Example — inline closures
///
/// ```rust
/// use rust_web_server::app::App;
/// use rust_web_server::core::New;
/// use rust_web_server::routes;
/// use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
///
/// let app = routes! {
/// App::with_state(42u32),
/// GET "/ping" => |_req, _params, _conn, _state: &u32| {
/// let mut r = Response::new();
/// r.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
/// r.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
/// r
/// },
/// };
/// ```
;
=> ;
=> ;
=> ;
=> ;
}