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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use core::{
    convert::Infallible,
    fmt::{self, Debug, Display, Formatter},
};

use crate::{ready::ReadyService, service::Service};

/// A pipeline type where two variants have a either/or relationship
pub enum Pipeline<F, S> {
    First(F),
    Second(S),
}

impl<F, S> Clone for Pipeline<F, S>
where
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        match *self {
            Self::First(ref p) => Self::First(p.clone()),
            Self::Second(ref p) => Self::Second(p.clone()),
        }
    }
}

impl<F, S> Pipeline<F, S>
where
    F: From<S>,
{
    pub fn into_first(self) -> F {
        match self {
            Self::First(f) => f,
            Self::Second(s) => F::from(s),
        }
    }
}

impl<F, S> Pipeline<F, S>
where
    S: From<F>,
{
    pub fn into_second(self) -> S {
        match self {
            Self::First(f) => S::from(f),
            Self::Second(s) => s,
        }
    }
}

impl<F, S> Debug for Pipeline<F, S>
where
    F: Debug,
    S: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::First(ref p) => Debug::fmt(p, f),
            Self::Second(ref p) => Debug::fmt(p, f),
        }
    }
}

impl<F, S> Display for Pipeline<F, S>
where
    F: Display,
    S: Display,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::First(ref p) => Display::fmt(p, f),
            Self::Second(ref p) => Display::fmt(p, f),
        }
    }
}

// useful default impl when pipeline enum used as error type.
impl<F, S> From<Infallible> for Pipeline<F, S> {
    fn from(e: Infallible) -> Self {
        match e {}
    }
}

impl<F, S, Req> Service<Req> for Pipeline<F, S>
where
    F: Service<Req>,
    S: Service<Req, Response = F::Response, Error = F::Error>,
{
    type Response = F::Response;
    type Error = F::Error;

    async fn call(&self, req: Req) -> Result<Self::Response, Self::Error> {
        match self {
            Self::First(ref f) => f.call(req).await,
            Self::Second(ref s) => s.call(req).await,
        }
    }
}

impl<F, S> ReadyService for Pipeline<F, S>
where
    F: ReadyService,
    S: ReadyService,
{
    type Ready = Pipeline<F::Ready, S::Ready>;

    #[inline]
    async fn ready(&self) -> Self::Ready {
        match self {
            Self::First(ref f) => Pipeline::First(f.ready().await),
            Self::Second(ref s) => Pipeline::Second(s.ready().await),
        }
    }
}

#[cfg(feature = "std")]
impl<F, S> std::error::Error for Pipeline<F, S>
where
    F: std::error::Error,
    S: std::error::Error,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            Self::First(ref f) => std::error::Error::source(f),
            Self::Second(ref f) => std::error::Error::source(f),
        }
    }
}