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
use super::BoxFuture;

use crate::{Context, Response};

/// A Extractor trait.
pub trait Extract: Sized {
    /// The type of failures extracted by this Extractor.
    type Error: Into<Response>;

    /// Extract the value from Context.
    fn extract(cx: &mut Context) -> BoxFuture<'_, Result<Self, Self::Error>>;
}

impl<T> Extract for Option<T>
where
    T: Extract,
{
    type Error = T::Error;

    fn extract(cx: &mut Context) -> BoxFuture<'_, Result<Self, Self::Error>> {
        Box::pin(async move { Ok(T::extract(cx).await.ok()) })
    }
}

impl<T> Extract for Result<T, T::Error>
where
    T: Extract,
{
    type Error = T::Error;

    fn extract(cx: &mut Context) -> BoxFuture<'_, Result<Self, Self::Error>> {
        Box::pin(async move { Ok(T::extract(cx).await) })
    }
}