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
use std::{pin::Pin, sync::Arc};
use async_trait::async_trait;
use crate::{RpcService, ServerError};
pub struct OrService<T: RpcService, U: RpcService>(T, U);
impl<T: RpcService, U: RpcService> OrService<T, U> {
pub fn new(t: T, u: U) -> Self {
Self(t, u)
}
}
#[async_trait::async_trait]
impl<T: RpcService, U: RpcService> RpcService for OrService<T, U> {
async fn respond(
&self,
method: &str,
params: Vec<serde_json::Value>,
) -> Option<Result<serde_json::Value, ServerError>> {
if let Some(res) = self.0.respond(method, params.clone()).await {
Some(res)
} else {
self.1.respond(method, params).await
}
}
}
#[allow(clippy::type_complexity)]
pub struct FnService(
Arc<
dyn Fn(
&str,
Vec<serde_json::Value>,
) -> Pin<
Box<
dyn std::future::Future<Output = Option<Result<serde_json::Value, ServerError>>>
+ Send
+ 'static,
>,
> + Sync
+ Send
+ 'static,
>,
);
impl FnService {
pub fn new<
Fut: std::future::Future<Output = Option<Result<serde_json::Value, ServerError>>>
+ Send
+ 'static,
Fun: Fn(&str, Vec<serde_json::Value>) -> Fut + Send + Sync + 'static,
>(
f: Fun,
) -> Self {
let f = Arc::new(f);
Self(Arc::new(move |m, args| {
let m = m.to_string();
let f = f.clone();
Box::pin(async move { f(&m, args).await })
}))
}
}
#[async_trait]
impl RpcService for FnService {
async fn respond(
&self,
method: &str,
params: Vec<serde_json::Value>,
) -> Option<Result<serde_json::Value, ServerError>> {
self.0(method, params).await
}
}