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 #[allow(clippy::empty_enums)] #[derive(Debug, Clone, Copy)]
34 pub enum ViaParts {}
35
36 #[allow(clippy::empty_enums)] #[derive(Debug, Clone, Copy)]
38 pub enum ViaRequest {}
39}
40
41#[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 type Rejection: IntoResponse;
59
60 fn from_request_parts(
62 parts: &mut Parts,
63 state: &S,
64 ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
65}
66
67#[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 type Rejection: IntoResponse;
85
86 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}