Skip to main content

jsonrpsee_core/middleware/layer/
either.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27//! [`tower::util::Either`] but
28//! adjusted to satisfy the trait bound [`RpcServiceT].
29//!
30//! NOTE: This is introduced because it doesn't
31//! work to implement tower::Layer for
32//! external types such as future::Either.
33
34use crate::{
35    middleware::{Batch, Notification, RpcServiceT},
36    server::MethodResponse,
37};
38use jsonrpsee_types::Request;
39
40/// [`tower::util::Either`] but
41/// adjusted to satisfy the trait bound [`RpcServiceT].
42#[derive(Clone, Debug)]
43pub enum Either<A, B> {
44    /// One type of backing [`RpcServiceT`].
45    Left(A),
46    /// The other type of backing [`RpcServiceT`].
47    Right(B),
48}
49
50impl<S, A, B> tower::Layer<S> for Either<A, B>
51where
52    A: tower::Layer<S>,
53    B: tower::Layer<S>,
54{
55    type Service = Either<A::Service, B::Service>;
56
57    fn layer(&self, inner: S) -> Self::Service {
58        match self {
59            Either::Left(layer) => Either::Left(layer.layer(inner)),
60            Either::Right(layer) => Either::Right(layer.layer(inner)),
61        }
62    }
63}
64
65impl<A, B> RpcServiceT for Either<A, B>
66where
67    A: RpcServiceT + Send,
68    B: RpcServiceT + Send,
69{
70    fn call<'a>(&self, request: Request<'a>) -> impl Future<Output = MethodResponse> + Send + 'a {
71        match self {
72            Either::Left(service) => futures_util::future::Either::Left(service.call(request)),
73            Either::Right(service) => futures_util::future::Either::Right(service.call(request)),
74        }
75    }
76
77    fn batch<'a>(&self, batch: Batch<'a>) -> impl Future<Output = MethodResponse> + Send + 'a {
78        match self {
79            Either::Left(service) => futures_util::future::Either::Left(service.batch(batch)),
80            Either::Right(service) => futures_util::future::Either::Right(service.batch(batch)),
81        }
82    }
83
84    fn notification<'a>(
85        &self,
86        n: Notification<'a>,
87    ) -> impl Future<Output = MethodResponse> + Send + 'a {
88        match self {
89            Either::Left(service) => futures_util::future::Either::Left(service.notification(n)),
90            Either::Right(service) => futures_util::future::Either::Right(service.notification(n)),
91        }
92    }
93}