1use 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
27pub type Request<T = Body> = http::Request<T>;
30
31mod private {
32 #[derive(Debug, Clone, Copy)]
33 pub enum ViaParts {}
34
35 #[derive(Debug, Clone, Copy)]
36 pub enum ViaRequest {}
37}
38
39#[rustversion::attr(
51 since(1.78),
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 )
55)]
56pub trait FromRequestParts<S>: Sized {
57 type Rejection: IntoResponse;
60
61 fn from_request_parts(
63 parts: &mut Parts,
64 state: &S,
65 ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
66}
67
68#[rustversion::attr(
80 since(1.78),
81 diagnostic::on_unimplemented(
82 note = "Function argument is not a valid axum extractor. \nSee `https://docs.rs/axum/0.8/axum/extract/index.html` for details",
83 )
84)]
85pub trait FromRequest<S, M = private::ViaRequest>: Sized {
86 type Rejection: IntoResponse;
89
90 fn from_request(
92 req: Request,
93 state: &S,
94 ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
95}
96
97impl<S, T> FromRequest<S, private::ViaParts> for T
98where
99 S: Send + Sync,
100 T: FromRequestParts<S>,
101{
102 type Rejection = <Self as FromRequestParts<S>>::Rejection;
103
104 async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
105 let (mut parts, _) = req.into_parts();
106 Self::from_request_parts(&mut parts, state).await
107 }
108}
109
110impl<S, T> FromRequestParts<S> for Result<T, T::Rejection>
111where
112 T: FromRequestParts<S>,
113 S: Send + Sync,
114{
115 type Rejection = Infallible;
116
117 async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
118 Ok(T::from_request_parts(parts, state).await)
119 }
120}
121
122impl<S, T> FromRequest<S> for Result<T, T::Rejection>
123where
124 T: FromRequest<S>,
125 S: Send + Sync,
126{
127 type Rejection = Infallible;
128
129 async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
130 Ok(T::from_request(req, state).await)
131 }
132}