rama_http/service/web/endpoint/extract/
option.rs1use rama_core::Context;
2
3use crate::service::web::endpoint::IntoResponse;
4use crate::{Request, dep::http::request::Parts};
5
6use super::{FromRequest, FromRequestContextRefPair};
7
8pub trait OptionalFromRequestContextRefPair<S>: Sized + Send + Sync + 'static {
11 type Rejection: IntoResponse;
15
16 fn from_request_context_ref_pair(
18 ctx: &Context<S>,
19 parts: &Parts,
20 ) -> impl Future<Output = Result<Option<Self>, Self::Rejection>> + Send;
21}
22
23pub trait OptionalFromRequest: Sized + Send + Sync + 'static {
25 type Rejection: IntoResponse;
29
30 fn from_request(
32 req: Request,
33 ) -> impl Future<Output = Result<Option<Self>, Self::Rejection>> + Send;
34}
35
36impl<S, T> FromRequestContextRefPair<S> for Option<T>
37where
38 T: OptionalFromRequestContextRefPair<S>,
39 S: Send + Sync,
40{
41 type Rejection = T::Rejection;
42
43 fn from_request_context_ref_pair(
44 ctx: &Context<S>,
45 parts: &Parts,
46 ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send {
47 T::from_request_context_ref_pair(ctx, parts)
48 }
49}
50
51impl<T> FromRequest for Option<T>
52where
53 T: OptionalFromRequest,
54{
55 type Rejection = T::Rejection;
56
57 async fn from_request(req: Request) -> Result<Option<T>, Self::Rejection> {
58 T::from_request(req).await
59 }
60}