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
129
use futures_util::{
    future::Either,
    future::{MapErr, TryFutureExt},
};
use std::{
    fmt,
    sync::Arc,
    task::{Context, Poll},
};
use tower_service::Service;

#[derive(Debug)]
pub(crate) struct Routes<A, B, Request> {
    routes: Or<A, B, Request>,
}

impl<A, B, Request> Routes<A, B, Request> {
    pub(crate) fn new(
        predicate: impl Fn(&Request) -> bool + Send + Sync + 'static,
        a: A,
        b: B,
    ) -> Self {
        let routes = Or::new(predicate, a, b);
        Self { routes }
    }
}

impl<A, B, Request> Routes<A, B, Request> {
    pub(crate) fn push<C>(
        self,
        predicate: impl Fn(&Request) -> bool + Send + Sync + 'static,
        route: C,
    ) -> Routes<C, Or<A, B, Request>, Request> {
        let routes = Or::new(predicate, route, self.routes);
        Routes { routes }
    }
}

impl<A, B, Request> Service<Request> for Routes<A, B, Request>
where
    A: Service<Request>,
    A::Future: Send + 'static,
    A::Error: Into<crate::Error>,
    B: Service<Request, Response = A::Response>,
    B::Future: Send + 'static,
    B::Error: Into<crate::Error>,
{
    type Response = A::Response;
    type Error = crate::Error;
    type Future = <Or<A, B, Request> as Service<Request>>::Future;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Ok(()).into()
    }

    fn call(&mut self, req: Request) -> Self::Future {
        self.routes.call(req)
    }
}

impl<A: Clone, B: Clone, Request> Clone for Routes<A, B, Request> {
    fn clone(&self) -> Self {
        Self {
            routes: self.routes.clone(),
        }
    }
}

#[doc(hidden)]
pub struct Or<A, B, Request> {
    predicate: Arc<dyn Fn(&Request) -> bool + Send + Sync + 'static>,
    a: A,
    b: B,
}

impl<A, B, Request> Or<A, B, Request> {
    pub(crate) fn new<F>(predicate: F, a: A, b: B) -> Self
    where
        F: Fn(&Request) -> bool + Send + Sync + 'static,
    {
        let predicate = Arc::new(predicate);
        Self { predicate, a, b }
    }
}

impl<A, B, Request> Service<Request> for Or<A, B, Request>
where
    A: Service<Request>,
    A::Future: Send + 'static,
    A::Error: Into<crate::Error>,
    B: Service<Request, Response = A::Response>,
    B::Future: Send + 'static,
    B::Error: Into<crate::Error>,
{
    type Response = A::Response;
    type Error = crate::Error;
    type Future = Either<
        MapErr<A::Future, fn(A::Error) -> crate::Error>,
        MapErr<B::Future, fn(B::Error) -> crate::Error>,
    >;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Ok(()).into()
    }

    fn call(&mut self, req: Request) -> Self::Future {
        if (self.predicate)(&req) {
            Either::Left(self.a.call(req).map_err(|e| e.into()))
        } else {
            Either::Right(self.b.call(req).map_err(|e| e.into()))
        }
    }
}

impl<A: Clone, B: Clone, Request> Clone for Or<A, B, Request> {
    fn clone(&self) -> Self {
        Self {
            predicate: self.predicate.clone(),
            a: self.a.clone(),
            b: self.b.clone(),
        }
    }
}

impl<A, B, Request> fmt::Debug for Or<A, B, Request> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Or {{ .. }}")
    }
}