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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use http::uri::Scheme;

use crate::{
    endpoint::BoxEndpoint, error::NotFoundError, Endpoint, EndpointExt, IntoEndpoint, Request,
    Response,
};

/// Routing object for request scheme
///
/// # Errors
///
/// - [`NotFoundError`]
#[derive(Default)]
pub struct RouteScheme {
    schemes: Vec<(Scheme, BoxEndpoint<'static>)>,
    fallback: Option<BoxEndpoint<'static>>,
}

impl RouteScheme {
    /// Create a `RouteScheme` object.
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets the endpoint for `HTTPS`.
    #[must_use]
    pub fn https<E>(mut self, ep: E) -> Self
    where
        E: IntoEndpoint,
        E::Endpoint: 'static,
    {
        self.schemes
            .push((Scheme::HTTPS, ep.into_endpoint().map_to_response().boxed()));
        self
    }

    /// Sets the endpoint for `HTTP`.
    #[must_use]
    pub fn http<E>(mut self, ep: E) -> Self
    where
        E: IntoEndpoint,
        E::Endpoint: 'static,
    {
        self.schemes
            .push((Scheme::HTTP, ep.into_endpoint().map_to_response().boxed()));
        self
    }

    /// Sets the endpoint for the specified `scheme`.
    #[must_use]
    pub fn custom<E>(mut self, scheme: Scheme, ep: E) -> Self
    where
        E: IntoEndpoint,
        E::Endpoint: 'static,
    {
        self.schemes
            .push((scheme, ep.into_endpoint().map_to_response().boxed()));
        self
    }

    /// Sets the endpoint for the fallback schemes.
    ///
    /// All unmatched schemes will use this endpoint.
    #[must_use]
    pub fn fallback<E>(mut self, ep: E) -> Self
    where
        E: IntoEndpoint,
        E::Endpoint: 'static,
    {
        self.fallback = Some(ep.into_endpoint().map_to_response().boxed());
        self
    }
}

#[async_trait::async_trait]
impl Endpoint for RouteScheme {
    type Output = Response;

    async fn call(&self, req: Request) -> crate::Result<Self::Output> {
        match self
            .schemes
            .iter()
            .find(|(scheme, _)| scheme == req.scheme())
            .map(|(_, ep)| ep)
        {
            Some(ep) => ep.call(req).await,
            None => match &self.fallback {
                Some(ep) => ep.call(req).await,
                None => Err(NotFoundError.into()),
            },
        }
    }
}