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
//! YAHF is an web framework for Rust focused on developer experience, extensibility, and
//! performance.
//!
//! >
//! >
//! > **Nightly until [`RPITIT`](https://releases.rs/docs/1.75.0/) is stable**
//! >
//! >
//! ---
//!
//! # Table of Contents
//! - [Features](#features)
//! - [Example](#example)
//! - [Routing](#routing)
//! - [Handlers](#handlers)
//! - [Extensability](#extensability)
//! - [Middleware](#middleware)
//! - [Examples](#examples)
//!
//! # Features
//!
//! - Macro free Routing API
//! - Predictable error handling
//! - Native serialization and deserialization built into the handler
//! - Friendly syntax
//!
//! # Example
//!
//! The `Hello world` of YAHF is:
//!
//! ```rust,no_run
//! use yahf::server::Server;
//!
//! #[tokio::main]
//! async fn main() {
//! let server = Server::new().get(
//! "/",
//! || async { "Hello world".to_string() },
//! &(),
//! &String::with_capacity(0),
//! );
//!
//! server
//! .listen(([127, 0, 0, 1], 8000).into())
//! .await
//! .unwrap();
//! }
//!
//! ```
//!
//! # Routing
//!
//! [`Router`](router::Router) is used to bind handlers to paths.\
//!
//! ```no_run
//! use yahf::router::Router;
//!
//! // Router
//! let router = Router::new()
//! .get("/", root_get, &(), &())
//! .get("/foo", foo_get, &(), &())
//! .post("/foo", foo_post, &(), &())
//! .delete("/foo/bar", bar_delete, &(), &());
//!
//! // calls respectively each of these handlers
//!
//! async fn root_get() {}
//! async fn foo_get() {}
//! async fn foo_post() {}
//! async fn bar_delete() {}
//!
//! # async {
//! # yahf::server::Server::new().router(router);
//! # };
//! ```
//!
//! [`Server`](server::Server) shares these features from [`Router`](router::Router)
//!
//! # Handlers
//!
//! On YAHF, a [`handler`] is a async function that is used to handle a `Route`. An acceptable
//! `handler` implements the trait [`Runner`](handler::Runner). By default, these signatures are
//! supported:
//!
//! ```no_run
//! # use serde::Serialize;
//! # use serde::Deserialize;
//! # use yahf::result::Result;
//! use yahf::request::Request;
//! use yahf::response::Response;
//! # #[derive(Serialize, Deserialize)]
//! # struct ResponseBody { second_value: u64 };
//! # #[derive(Serialize, Deserialize)]
//! # struct RequestBody { first_value: u64 }
//!
//! async fn handler1() -> ResponseBody
//! # {todo!()}
//! async fn handler2() -> Response<ResponseBody>
//! # {todo!()}
//! async fn handler3(req: RequestBody) -> ResponseBody
//! # {todo!()}
//! async fn handler4(req: Request<RequestBody>) -> ResponseBody
//! # {todo!()}
//! async fn handler5(req: RequestBody) -> Response<ResponseBody>
//! # {todo!()}
//! async fn handler6(req: Request<RequestBody>) -> Response<ResponseBody>
//! # {todo!()}
//! async fn handler7() -> Result<ResponseBody>
//! # {todo!()}
//! async fn handler8() -> Result<Response<ResponseBody>>
//! # {todo!()}
//! async fn handler9(req: Result<RequestBody>) -> Result<ResponseBody>
//! # {todo!()}
//! async fn handler10(req: Result<Request<RequestBody>>) -> Result<ResponseBody>
//! # {todo!()}
//! async fn handler11(req: Result<RequestBody>) -> Result<Response<ResponseBody>>
//! # {todo!()}
//! async fn handler12(req: Result<Request<RequestBody>>) -> Result<Response<ResponseBody>>
//! # {todo!()}
//! ```
//!
//! All these signatures comes from the implementations of [`RunnerInput`](runner_input::RunnerInput) and [`RunnerOutput`](runner_output::RunnerOutput).
//!
//! # Extensability
//!
//! YAHF `handlers` are modular by design. A `handler` is decomposed into four modules: a body [`deserializer`](deserializer::BodyDeserializer),
//! a body [`serializer`](serializer::BodySerializer), [`arguments`](runner_input::RunnerInput), and a [`response`](runner_output::RunnerOutput).
//! These modules are glued together using the [`Runner`](handler::Runner) trait. Adding new
//! functionality to the handlers is just a matter of implementing one of these traits. For more
//! details, check the trait docs
//!
//! # Middleware
//!
//! [`Middleware`](middleware) are async functions that will run previously or after a
//! `handler`. These can really useful when combined with a [`Router`](router::Router) or a
//! [`Server`](server::Server) to reuse logic and create `"pipelines"`.
//!
//! ```rust
//! use serde::Deserialize;
//! use serde::Serialize;
//! use yahf::handler::Json;
//! use yahf::request::Request;
//! use yahf::result::Result;
//! use yahf::response::Response;
//! use yahf::router::Router;
//! use yahf::server::Server;
//!
//!# use std::time;
//!# use std::time::UNIX_EPOCH;
//!# #[derive(Debug, Deserialize, Serialize)]
//! struct ComputationBody
//!# {
//!# value: u32,
//!# }
//!
//! // Print the time, the method, and the path from the Request
//! async fn log_middleware(req: Result<Request<String>>) -> Result<Request<String>>
//!# {
//!# match req.into_inner() {
//!# Ok(req) => {
//!# println!(
//!# "{} - {} - {}",
//!# time::SystemTime::now()
//!# .duration_since(UNIX_EPOCH)
//!# .expect("Negative time")
//!# .as_millis(),
//!# req.method().as_str(),
//!# req.uri().path()
//!# );
//!#
//!# Ok(req).into()
//!# }
//!# Err(err) => Err(err).into(),
//!# }
//!# }
//!
//! // Handle any possible errors
//! async fn log_error(res: Result<Response<String>>) -> Result<Response<String>>
//!# {
//!# match res.into_inner() {
//!# Err(err) => {
//!# println!(
//!# "{} - {}",
//!# time::SystemTime::now()
//!# .duration_since(UNIX_EPOCH)
//!# .expect("Negative time")
//!# .as_millis(),
//!# err.code(),
//!# );
//!# Err(err).into()
//!# }
//!# ok => ok.into(),
//!# }
//!# }
//!
//! // Compute something using the ComputationBody
//! async fn some_computation(req: ComputationBody) -> ComputationBody
//!# {
//!# ComputationBody {
//!# value: req.value + 1,
//!# }
//!# }
//!
//! // Set a [`Router`](router::Router) with both `Middlewares`.
//! // The route `/` will become: `log_middleware -> some_computation -> log_middleware`
//! let router = Router::new()
//! .pre(log_middleware)
//! .after(log_error)
//! .get("/", some_computation, &Json::new(), &Json::new());
//!
//! # async {
//! # yahf::server::Server::new().router(router);
//! # };
//! ```
//!
//! More of this example [here](https://github.com/lucasduartesobreira/yahf/blob/main/examples/router_example/main.rs)
//!
//! # Examples
//!
//! The repo includes [illustrative examples](https://github.com/lucasduartesobreira/yahf/tree/main/examples) demonstrating the integration of all the components
//!