jsonrpsee_core/middleware/layer/
either.rs1use crate::middleware::{Batch, Notification, RpcServiceT};
35use jsonrpsee_types::Request;
36
37#[derive(Clone, Debug)]
40pub enum Either<A, B> {
41 Left(A),
43 Right(B),
45}
46
47impl<S, A, B> tower::Layer<S> for Either<A, B>
48where
49 A: tower::Layer<S>,
50 B: tower::Layer<S>,
51{
52 type Service = Either<A::Service, B::Service>;
53
54 fn layer(&self, inner: S) -> Self::Service {
55 match self {
56 Either::Left(layer) => Either::Left(layer.layer(inner)),
57 Either::Right(layer) => Either::Right(layer.layer(inner)),
58 }
59 }
60}
61
62impl<A, B> RpcServiceT for Either<A, B>
63where
64 A: RpcServiceT + Send,
65 B: RpcServiceT<
66 MethodResponse = A::MethodResponse,
67 NotificationResponse = A::NotificationResponse,
68 BatchResponse = A::BatchResponse,
69 > + Send,
70{
71 type BatchResponse = A::BatchResponse;
72 type MethodResponse = A::MethodResponse;
73 type NotificationResponse = A::NotificationResponse;
74
75 fn call<'a>(&self, request: Request<'a>) -> impl Future<Output = Self::MethodResponse> + Send + 'a {
76 match self {
77 Either::Left(service) => futures_util::future::Either::Left(service.call(request)),
78 Either::Right(service) => futures_util::future::Either::Right(service.call(request)),
79 }
80 }
81
82 fn batch<'a>(&self, batch: Batch<'a>) -> impl Future<Output = Self::BatchResponse> + Send + 'a {
83 match self {
84 Either::Left(service) => futures_util::future::Either::Left(service.batch(batch)),
85 Either::Right(service) => futures_util::future::Either::Right(service.batch(batch)),
86 }
87 }
88
89 fn notification<'a>(&self, n: Notification<'a>) -> impl Future<Output = Self::NotificationResponse> + Send + 'a {
90 match self {
91 Either::Left(service) => futures_util::future::Either::Left(service.notification(n)),
92 Either::Right(service) => futures_util::future::Either::Right(service.notification(n)),
93 }
94 }
95}