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
//! Request data extraction module
//!
//! This module provides functionality for extracting typed data from HTTP requests.
//! It includes extractors for common data formats and patterns:
//!
//! - Form data (`Form<T>`) - For `application/x-www-form-urlencoded` request bodies
//! - JSON data (`Json<T>`) - For `application/json` request bodies
//! - Query parameters (`Query<T>`) - For URL query strings
//! - Headers and other request metadata
//! - Raw request body as bytes or string
//!
//! # Core Concepts
//!
//! The module is built around the [`FromRequest`] trait, which defines how to extract
//! typed data from requests. Types implementing this trait can be used as parameters
//! in request handlers.
//!
//! # Common Extractors
//!
//! ## Form Data
//! ```no_run
//! # use serde::Deserialize;
//! # use micro_web::extract::Form;
//! #[derive(Deserialize)]
//! struct LoginForm {
//! username: String,
//! password: String,
//! }
//!
//! async fn handle_login(Form(form): Form<LoginForm>) {
//! println!("Login attempt from: {}", form.username);
//! }
//! ```
//!
//! ## JSON Data
//! ```no_run
//! # use serde::Deserialize;
//! # use micro_web::extract::Json;
//! #[derive(Deserialize)]
//! struct User {
//! name: String,
//! email: String,
//! }
//!
//! async fn create_user(Json(user): Json<User>) {
//! println!("Creating user: {}", user.name);
//! }
//! ```
//!
//! ## Query Parameters
//! ```no_run
//! # use serde::Deserialize;
//! # use micro_web::extract::Query;
//! #[derive(Deserialize)]
//! struct Pagination {
//! page: u32,
//! per_page: u32,
//! }
//!
//! async fn list_items(Query(params): Query<Pagination>) {
//! println!("Listing page {} with {} items", params.page, params.per_page);
//! }
//! ```
//!
//! # Optional Extraction
//!
//! All extractors can be made optional by wrapping them in `Option<T>`:
//!
//! ```
//! # use serde::Deserialize;
//! # use micro_web::extract::Json;
//! #[derive(Deserialize)]
//! struct UpdateUser {
//! name: Option<String>,
//! email: Option<String>,
//! }
//!
//! async fn update_user(Json(update): Json<UpdateUser>) {
//! if let Some(name) = update.name {
//! println!("Updating name to: {}", name);
//! }
//! }
//! ```
//!
//! # Multiple Extractors
//!
//! Multiple extractors can be combined using tuples:
//!
//! ```
//! # use serde::Deserialize;
//! # use micro_web::extract::{Json, Query};
//! # use http::Method;
//! #[derive(Deserialize)]
//! struct SearchParams {
//! q: String,
//! }
//!
//! #[derive(Deserialize)]
//! struct Payload {
//! data: String,
//! }
//!
//! async fn handler(
//! method: Method,
//! Query(params): Query<SearchParams>,
//! Json(payload): Json<Payload>,
//! ) {
//! // Access to method, query params, and JSON payload
//! }
//! ```
pub use FromRequest;
use Deserialize;
/// Represented as form data
///
/// when `post` as a `application/x-www-form-urlencoded`, we can use this struct to extract the form data,
/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
///
/// # Example
/// ```
/// # use serde::Deserialize;
/// # use micro_web::extract::Form;
/// # #[allow(dead_code)]
/// #[derive(Deserialize, Debug)]
/// struct Params {
/// name: String,
/// value: String,
/// }
///
/// pub async fn handle(Form(params) : Form<Params>) -> String {
/// format!("received params: {:?}", params)
/// }
/// ```
where
T: for<'de> + Send;
/// Represented as JSON data
///
/// when `post` as a `application/json`, we can use this struct to extract data,
/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
///
/// # Example
/// ```
/// # use serde::Deserialize;
/// # use micro_web::extract::Json;
/// # #[allow(dead_code)]
/// #[derive(Deserialize, Debug)]
/// struct Params {
/// name: String,
/// value: String,
/// }
///
/// pub async fn handle(Json(params) : Json<Params>) -> String {
/// format!("received params: {:?}", params)
/// }
/// ```
where
T: for<'de> + Send;
/// Represented as url query data
///
/// when request with url query, we can use this struct to extract query,
/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
///
/// # Example
/// ```
/// # use serde::Deserialize;
/// # use micro_web::extract::Query;
/// # #[allow(dead_code)]
/// #[derive(Deserialize, Debug)]
/// struct Params {
/// name: String,
/// value: String,
/// }
///
/// pub async fn handle(Query(params) : Query<Params>) -> String {
/// format!("received params: {:?}", params)
/// }
/// ```
where
T: for<'de> + Send;