2018-09-27 17:05:48 -07:00
|
|
|
//! Contains `Either` service and related types and functions.
|
2018-12-09 10:15:49 -08:00
|
|
|
use actix_service::{NewService, Service};
|
2019-03-04 20:37:03 -08:00
|
|
|
use futures::{future, try_ready, Async, Future, IntoFuture, Poll};
|
2018-09-27 17:05:48 -07:00
|
|
|
|
|
|
|
/// Combine two different service types into a single type.
|
|
|
|
///
|
|
|
|
/// Both services must be of the same request, response, and error types.
|
|
|
|
/// `EitherService` is useful for handling conditional branching in service
|
|
|
|
/// middleware to different inner service types.
|
|
|
|
pub enum EitherService<A, B> {
|
|
|
|
A(A),
|
|
|
|
B(B),
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:00:08 -08:00
|
|
|
impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
EitherService::A(srv) => EitherService::A(srv.clone()),
|
|
|
|
EitherService::B(srv) => EitherService::B(srv.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
impl<A, B> Service for EitherService<A, B>
|
2018-09-27 17:05:48 -07:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>,
|
2018-09-27 17:05:48 -07:00
|
|
|
{
|
2019-03-09 07:27:35 -08:00
|
|
|
type Request = A::Request;
|
2018-09-27 17:05:48 -07:00
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
type Future = future::Either<A::Future, B::Future>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
match self {
|
|
|
|
EitherService::A(ref mut inner) => inner.poll_ready(),
|
|
|
|
EitherService::B(ref mut inner) => inner.poll_ready(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
2018-09-27 17:05:48 -07:00
|
|
|
match self {
|
|
|
|
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
|
|
|
|
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine two different new service types into a single type.
|
|
|
|
pub enum Either<A, B> {
|
|
|
|
A(A),
|
|
|
|
B(B),
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:33:10 -08:00
|
|
|
impl<A, B> Either<A, B> {
|
2019-03-09 07:27:35 -08:00
|
|
|
pub fn new_a<C>(srv: A) -> Self
|
2019-01-16 15:33:10 -08:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
A: NewService<C>,
|
|
|
|
B: NewService<
|
|
|
|
C,
|
|
|
|
Request = A::Request,
|
|
|
|
Response = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2019-01-16 15:33:10 -08:00
|
|
|
{
|
|
|
|
Either::A(srv)
|
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
pub fn new_b<C>(srv: B) -> Self
|
2019-01-16 15:33:10 -08:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
A: NewService<C>,
|
|
|
|
B: NewService<
|
|
|
|
C,
|
|
|
|
Request = A::Request,
|
|
|
|
Response = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2019-01-16 15:33:10 -08:00
|
|
|
{
|
|
|
|
Either::B(srv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
impl<A, B, C> NewService<C> for Either<A, B>
|
2018-09-27 17:05:48 -07:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
A: NewService<C>,
|
|
|
|
B: NewService<
|
|
|
|
C,
|
|
|
|
Request = A::Request,
|
|
|
|
Response = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2018-09-27 17:05:48 -07:00
|
|
|
{
|
2019-03-09 07:27:35 -08:00
|
|
|
type Request = A::Request;
|
2018-09-27 17:05:48 -07:00
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
type InitError = A::InitError;
|
|
|
|
type Service = EitherService<A::Service, B::Service>;
|
2019-03-09 07:27:35 -08:00
|
|
|
type Future = EitherNewService<A, B, C>;
|
2018-09-27 17:05:48 -07:00
|
|
|
|
2019-02-22 12:44:37 -08:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2018-09-27 17:05:48 -07:00
|
|
|
match self {
|
2019-03-04 21:35:47 -08:00
|
|
|
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
|
|
|
|
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
|
2018-09-27 17:05:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:00:08 -08:00
|
|
|
impl<A: Clone, B: Clone> Clone for Either<A, B> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
Either::A(srv) => Either::A(srv.clone()),
|
|
|
|
Either::B(srv) => Either::B(srv.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:05:48 -07:00
|
|
|
#[doc(hidden)]
|
2019-03-09 07:27:35 -08:00
|
|
|
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
|
2019-03-04 20:37:03 -08:00
|
|
|
A(<A::Future as IntoFuture>::Future),
|
|
|
|
B(<B::Future as IntoFuture>::Future),
|
2018-09-27 17:05:48 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
impl<A, B, C> Future for EitherNewService<A, B, C>
|
2018-09-27 17:05:48 -07:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
A: NewService<C>,
|
|
|
|
B: NewService<
|
|
|
|
C,
|
|
|
|
Request = A::Request,
|
|
|
|
Response = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2018-09-27 17:05:48 -07:00
|
|
|
{
|
|
|
|
type Item = EitherService<A::Service, B::Service>;
|
|
|
|
type Error = A::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self {
|
|
|
|
EitherNewService::A(ref mut fut) => {
|
|
|
|
let service = try_ready!(fut.poll());
|
|
|
|
Ok(Async::Ready(EitherService::A(service)))
|
|
|
|
}
|
|
|
|
EitherNewService::B(ref mut fut) => {
|
|
|
|
let service = try_ready!(fut.poll());
|
|
|
|
Ok(Async::Ready(EitherService::B(service)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|