1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Extracts data from the [`Request`] by types.

use crate::{Future, IntoResponse, Request};

/// An interface for extracting data from the HTTP [`Request`].
pub trait FromRequest: Sized {
    /// The type returned in the event of a conversion error.
    type Error: IntoResponse;

    /// Extracts this type from the HTTP [`Request`].
    #[must_use]
    fn extract(req: &mut Request) -> impl Future<Output = Result<Self, Self::Error>> + Send;
}

impl<T> FromRequest for Option<T>
where
    T: FromRequest,
{
    type Error = std::convert::Infallible;

    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
        Ok(T::extract(req).await.ok())
    }
}

impl<T> FromRequest for Result<T, T::Error>
where
    T: FromRequest,
{
    type Error = std::convert::Infallible;

    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
        Ok(T::extract(req).await)
    }
}