Skip to main content

axum_core/extract/
mod.rs

1//! Types and traits for extracting data from requests.
2//!
3//! See [`axum::extract`] for more details.
4//!
5//! [`axum::extract`]: https://docs.rs/axum/0.8/axum/extract/index.html
6
7use crate::{body::Body, response::IntoResponse};
8use http::request::Parts;
9use std::convert::Infallible;
10use std::future::Future;
11
12pub mod rejection;
13
14mod default_body_limit;
15mod from_ref;
16mod option;
17mod request_parts;
18mod tuple;
19
20pub(crate) use self::default_body_limit::DefaultBodyLimitKind;
21pub use self::{
22    default_body_limit::DefaultBodyLimit,
23    from_ref::FromRef,
24    option::{OptionalFromRequest, OptionalFromRequestParts},
25};
26
27/// Type alias for [`http::Request`] whose body type defaults to [`Body`], the most common body
28/// type used with axum.
29pub type Request<T = Body> = http::Request<T>;
30
31mod private {
32    #[allow(clippy::empty_enums)] // We cannot use `!` yet in our MSRV.
33    #[derive(Debug, Clone, Copy)]
34    pub enum ViaParts {}
35
36    #[allow(clippy::empty_enums)] // We cannot use `!` yet in our MSRV.
37    #[derive(Debug, Clone, Copy)]
38    pub enum ViaRequest {}
39}
40
41/// Types that can be created from request parts.
42///
43/// Extractors that implement `FromRequestParts` cannot consume the request body and can thus be
44/// run in any order for handlers.
45///
46/// If your extractor needs to consume the request body then you should implement [`FromRequest`]
47/// and not [`FromRequestParts`].
48///
49/// See [`axum::extract`] for more general docs about extractors.
50///
51/// [`axum::extract`]: https://docs.rs/axum/0.8/axum/extract/index.html
52#[diagnostic::on_unimplemented(
53    note = "Function argument is not a valid axum extractor. \nSee `https://docs.rs/axum/0.8/axum/extract/index.html` for details"
54)]
55pub trait FromRequestParts<S>: Sized {
56    /// If the extractor fails it'll use this "rejection" type. A rejection is
57    /// a kind of error that can be converted into a response.
58    type Rejection: IntoResponse;
59
60    /// Perform the extraction.
61    fn from_request_parts(
62        parts: &mut Parts,
63        state: &S,
64    ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
65}
66
67/// Types that can be created from requests.
68///
69/// Extractors that implement `FromRequest` can consume the request body and can thus only be run
70/// once for handlers.
71///
72/// If your extractor doesn't need to consume the request body then you should implement
73/// [`FromRequestParts`] and not [`FromRequest`].
74///
75/// See [`axum::extract`] for more general docs about extractors.
76///
77/// [`axum::extract`]: https://docs.rs/axum/0.8/axum/extract/index.html
78#[diagnostic::on_unimplemented(
79    note = "Function argument is not a valid axum extractor. \nSee `https://docs.rs/axum/0.8/axum/extract/index.html` for details"
80)]
81pub trait FromRequest<S, M = private::ViaRequest>: Sized {
82    /// If the extractor fails it'll use this "rejection" type. A rejection is
83    /// a kind of error that can be converted into a response.
84    type Rejection: IntoResponse;
85
86    /// Perform the extraction.
87    fn from_request(
88        req: Request,
89        state: &S,
90    ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
91}
92
93impl<S, T> FromRequest<S, private::ViaParts> for T
94where
95    S: Send + Sync,
96    T: FromRequestParts<S>,
97{
98    type Rejection = <Self as FromRequestParts<S>>::Rejection;
99
100    fn from_request(
101        req: Request,
102        state: &S,
103    ) -> impl Future<Output = Result<Self, Self::Rejection>> {
104        let (mut parts, _) = req.into_parts();
105        async move { Self::from_request_parts(&mut parts, state).await }
106    }
107}
108
109impl<S, T> FromRequestParts<S> for Result<T, T::Rejection>
110where
111    T: FromRequestParts<S>,
112    S: Send + Sync,
113{
114    type Rejection = Infallible;
115
116    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
117        Ok(T::from_request_parts(parts, state).await)
118    }
119}
120
121impl<S, T> FromRequest<S> for Result<T, T::Rejection>
122where
123    T: FromRequest<S>,
124    S: Send + Sync,
125{
126    type Rejection = Infallible;
127
128    async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
129        Ok(T::from_request(req, state).await)
130    }
131}