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
36
37
38
39
40
41
42
43
44
45
46
47
//! Trait implemented by types that can be extracted from Context.

use viz_utils::{futures::future::BoxFuture, tracing};

use crate::{Context, Error, Result};

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

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

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

    #[inline]
    fn extract(cx: &mut Context) -> BoxFuture<'_, Result<Self, Self::Error>> {
        Box::pin(async move {
            Ok(match T::extract(cx).await {
                Ok(v) => Some(v),
                Err(e) => {
                    tracing::debug!("Error for Option<T> extractor: {}", e.into());
                    None
                }
            })
        })
    }
}

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

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