-
Notifications
You must be signed in to change notification settings - Fork 153
/
traits.rs
57 lines (50 loc) · 2.48 KB
/
traits.rs
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
use crate::v2::params::{JsonRpcParams, RpcParams};
use crate::{Error, NotificationHandler, Subscription};
use async_trait::async_trait;
use serde::de::DeserializeOwned;
/// [JSON-RPC](https://www.jsonrpc.org/specification) client interface that can make requests and notifications.
#[async_trait]
pub trait Client {
/// Send a [notification request](https://www.jsonrpc.org/specification#notification)
async fn notification<'a>(&self, method: &'a str, params: JsonRpcParams<'a>) -> Result<(), Error>;
/// Send a [method call request](https://www.jsonrpc.org/specification#request_object).
async fn request<'a, R>(&self, method: &'a str, params: JsonRpcParams<'a>) -> Result<R, Error>
where
R: DeserializeOwned;
/// Send a [batch request](https://www.jsonrpc.org/specification#batch).
///
/// The response to batch are returned in the same order as it was inserted in the batch.
///
/// Returns `Ok` if all requests in the batch were answered successfully.
/// Returns `Error` if any of the requests in batch fails.
async fn batch_request<'a, R>(&self, batch: Vec<(&'a str, JsonRpcParams<'a>)>) -> Result<Vec<R>, Error>
where
R: DeserializeOwned + Default + Clone;
}
/// [JSON-RPC](https://www.jsonrpc.org/specification) client interface that can make requests, notifications and subscriptions.
#[async_trait]
pub trait SubscriptionClient: Client {
/// Send a subscription request to the server, technically not part of the [JSON-RPC specification](https://www.jsonrpc.org/specification)
///
/// The `subscribe_method` and `params` are used to ask for the subscription towards the
/// server.
///
/// The `unsubscribe_method` is used to close the subscription.
///
/// The `Notif` param is a generic type to receive generic subscriptions, see [`Subscription`](crate::client::Subscription) for further documentation.
async fn subscribe<'a, Notif>(
&self,
subscribe_method: &'a str,
params: JsonRpcParams<'a>,
unsubscribe_method: &'a str,
) -> Result<Subscription<Notif>, Error>
where
Notif: DeserializeOwned;
/// Register a NotificationHandler<Notif> that will listen for incoming JSON-RPC notifications
async fn register_notification<'a, Notif>(&self, method: &'a str) -> Result<NotificationHandler<Notif>, Error>
where
Notif: DeserializeOwned;
}
/// JSON-RPC server interface for managing method calls.
pub trait RpcMethod<R, E>: Fn(RpcParams) -> Result<R, E> + Send + Sync + 'static {}
impl<R, T, E> RpcMethod<R, E> for T where T: Fn(RpcParams) -> Result<R, E> + Send + Sync + 'static {}