micro_web/extract/
mod.rs

1//! Request data extraction module
2//!
3//! This module provides functionality for extracting typed data from HTTP requests.
4//! It includes extractors for common data formats and patterns:
5//!
6//! - Form data (`Form<T>`) - For `application/x-www-form-urlencoded` request bodies
7//! - JSON data (`Json<T>`) - For `application/json` request bodies  
8//! - Query parameters (`Query<T>`) - For URL query strings
9//! - Headers and other request metadata
10//! - Raw request body as bytes or string
11//!
12//! # Core Concepts
13//!
14//! The module is built around the [`FromRequest`] trait, which defines how to extract
15//! typed data from requests. Types implementing this trait can be used as parameters
16//! in request handlers.
17//!
18//! # Common Extractors
19//!
20//! ## Form Data
21//! ```no_run
22//! # use serde::Deserialize;
23//! # use micro_web::extract::Form;
24//! #[derive(Deserialize)]
25//! struct LoginForm {
26//!     username: String,
27//!     password: String,
28//! }
29//!
30//! async fn handle_login(Form(form): Form<LoginForm>) {
31//!     println!("Login attempt from: {}", form.username);
32//! }
33//! ```
34//!
35//! ## JSON Data
36//! ```no_run
37//! # use serde::Deserialize;
38//! # use micro_web::extract::Json;
39//! #[derive(Deserialize)]
40//! struct User {
41//!     name: String,
42//!     email: String,
43//! }
44//!
45//! async fn create_user(Json(user): Json<User>) {
46//!     println!("Creating user: {}", user.name);
47//! }
48//! ```
49//!
50//! ## Query Parameters
51//! ```no_run
52//! # use serde::Deserialize;
53//! # use micro_web::extract::Query;
54//! #[derive(Deserialize)]
55//! struct Pagination {
56//!     page: u32,
57//!     per_page: u32,
58//! }
59//!
60//! async fn list_items(Query(params): Query<Pagination>) {
61//!     println!("Listing page {} with {} items", params.page, params.per_page);
62//! }
63//! ```
64//!
65//! # Optional Extraction
66//!
67//! All extractors can be made optional by wrapping them in `Option<T>`:
68//!
69//! ```
70//! # use serde::Deserialize;
71//! # use micro_web::extract::Json;
72//! #[derive(Deserialize)]
73//! struct UpdateUser {
74//!     name: Option<String>,
75//!     email: Option<String>,
76//! }
77//!
78//! async fn update_user(Json(update): Json<UpdateUser>) {
79//!     if let Some(name) = update.name {
80//!         println!("Updating name to: {}", name);
81//!     }
82//! }
83//! ```
84//!
85//! # Multiple Extractors
86//!
87//! Multiple extractors can be combined using tuples:
88//!
89//! ```
90//! # use serde::Deserialize;
91//! # use micro_web::extract::{Json, Query};
92//! # use http::Method;
93//! #[derive(Deserialize)]
94//! struct SearchParams {
95//!     q: String,
96//! }
97//!
98//! #[derive(Deserialize)]
99//! struct Payload {
100//!     data: String,
101//! }
102//!
103//! async fn handler(
104//!     method: Method,
105//!     Query(params): Query<SearchParams>,
106//!     Json(payload): Json<Payload>,
107//! ) {
108//!     // Access to method, query params, and JSON payload
109//! }
110//! ```
111
112mod extract_body;
113mod extract_header;
114mod extract_tuple;
115mod extract_url;
116mod from_request;
117
118pub use from_request::FromRequest;
119use serde::Deserialize;
120
121/// Represented as form data
122///
123/// when `post` as a `application/x-www-form-urlencoded`, we can use this struct to extract the form data,
124/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
125///
126/// # Example
127/// ```
128/// # use serde::Deserialize;
129/// # use micro_web::extract::Form;
130/// # #[allow(dead_code)]
131/// #[derive(Deserialize, Debug)]
132/// struct Params {
133///     name: String,
134///     value: String,
135/// }
136///
137/// pub async fn handle(Form(params) : Form<Params>) -> String {
138///     format!("received params: {:?}", params)
139/// }
140/// ```
141#[derive(Debug)]
142pub struct Form<T>(pub T)
143where
144    T: for<'de> Deserialize<'de> + Send;
145
146/// Represented as JSON data
147///
148/// when `post` as a `application/json`, we can use this struct to extract data,
149/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
150///
151/// # Example
152/// ```
153/// # use serde::Deserialize;
154/// # use micro_web::extract::Json;
155/// # #[allow(dead_code)]
156/// #[derive(Deserialize, Debug)]
157/// struct Params {
158///     name: String,
159///     value: String,
160/// }
161///
162/// pub async fn handle(Json(params) : Json<Params>) -> String {
163///     format!("received params: {:?}", params)
164/// }
165/// ```
166#[derive(Debug)]
167pub struct Json<T>(pub T)
168where
169    T: for<'de> Deserialize<'de> + Send;
170
171/// Represented as url query data
172///
173/// when request with url query, we can use this struct to extract query,
174/// note: the struct must impl [`serde::Deserialize`] and [`Send`]
175///
176/// # Example
177/// ```
178/// # use serde::Deserialize;
179/// # use micro_web::extract::Query;
180/// # #[allow(dead_code)]
181/// #[derive(Deserialize, Debug)]
182/// struct Params {
183///     name: String,
184///     value: String,
185/// }
186///
187/// pub async fn handle(Query(params) : Query<Params>) -> String {
188///     format!("received params: {:?}", params)
189/// }
190/// ```
191#[derive(Debug)]
192pub struct Query<T>(pub T)
193where
194    T: for<'de> Deserialize<'de> + Send;