rama_http/service/web/endpoint/extract/
option.rs

1use rama_core::Context;
2
3use crate::service::web::endpoint::IntoResponse;
4use crate::{Request, dep::http::request::Parts};
5
6use super::{FromRequest, FromRequestContextRefPair};
7
8/// Customize the behavior of `Option<Self>` as a [`FromRequestContextRefPair`]
9/// extractor.
10pub trait OptionalFromRequestContextRefPair<S>: Sized + Send + Sync + 'static {
11    /// If the extractor fails, it will use this "rejection" type.
12    ///
13    /// A rejection is a kind of error that can be converted into a response.
14    type Rejection: IntoResponse;
15
16    /// Perform the extraction.
17    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
23/// Customize the behavior of `Option<Self>` as a [`FromRequest`] extractor.
24pub trait OptionalFromRequest: Sized + Send + Sync + 'static {
25    /// If the extractor fails, it will use this "rejection" type.
26    ///
27    /// A rejection is a kind of error that can be converted into a response.
28    type Rejection: IntoResponse;
29
30    /// Perform the extraction.
31    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}