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