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
79
80
81
82
83
84
85
86
87
88
use types::{Params, Value, Error};
use futures::BoxFuture;

/// Metadata trait
pub trait Metadata: Default + Clone + Send + 'static {}
impl Metadata for () {}

/// Synchronous Method
pub trait RpcMethodSync: Send + Sync + 'static {
	/// Call method
	fn call(&self, params: Params) -> Result<Value, Error>;
}

/// Asynchronous Method
pub trait RpcMethodSimple: Send + Sync + 'static {
	/// Call method
	fn call(&self, params: Params) -> BoxFuture<Value, Error>;
}

/// Asynchronous Method with Metadata
pub trait RpcMethod<T: Metadata>: Send + Sync + 'static {
	/// Call method
	fn call(&self, params: Params, meta: T) -> BoxFuture<Value, Error>;
}

/// Notification
pub trait RpcNotificationSimple: Send + Sync + 'static {
	/// Execute notification
	fn execute(&self, params: Params);
}

/// Notification with Metadata
pub trait RpcNotification<T: Metadata>: Send + Sync + 'static {
	/// Execute notification
	fn execute(&self, params: Params, meta: T);
}

/// Possible Remote Procedures with Metadata
pub enum RemoteProcedure<T: Metadata> {
	/// A method call
	Method(Box<RpcMethod<T>>),
	/// A notification
	Notification(Box<RpcNotification<T>>),
	/// An alias to other method,
	Alias(String),
}

impl<F: Send + Sync + 'static> RpcMethodSync for F where
	F: Fn(Params) -> Result<Value, Error>,
{
	fn call(&self, params: Params) -> Result<Value, Error> {
		self(params)
	}
}

impl<F: Send + Sync + 'static> RpcMethodSimple for F where
	F: Fn(Params) -> BoxFuture<Value, Error>,
{
	fn call(&self, params: Params) -> BoxFuture<Value, Error> {
		self(params)
	}
}

impl<F: Send + Sync + 'static> RpcNotificationSimple for F where
	F: Fn(Params),
{
	fn execute(&self, params: Params) {
		self(params)
	}
}

impl<F: Send + Sync + 'static, T> RpcMethod<T> for F where
	T: Metadata,
	F: Fn(Params, T) -> BoxFuture<Value, Error>,
{
	fn call(&self, params: Params, meta: T) -> BoxFuture<Value, Error> {
		self(params, meta)
	}
}

impl<F: Send + Sync + 'static, T> RpcNotification<T> for F where
	T: Metadata,
	F: Fn(Params, T),
{
	fn execute(&self, params: Params, meta: T) {
		self(params, meta)
	}
}