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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Delegate rpc calls

use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;

use crate::calls::{Metadata, RemoteProcedure, RpcMethod, RpcNotification};
use crate::types::{Error, Params, Value};
use crate::BoxFuture;

struct DelegateAsyncMethod<T, F> {
	delegate: Arc<T>,
	closure: F,
}

impl<T, M, F, I> RpcMethod<M> for DelegateAsyncMethod<T, F>
where
	M: Metadata,
	F: Fn(&T, Params) -> I,
	I: Future<Output = Result<Value, Error>> + Send + 'static,
	T: Send + Sync + 'static,
	F: Send + Sync + 'static,
{
	fn call(&self, params: Params, _meta: M) -> BoxFuture<crate::Result<Value>> {
		let closure = &self.closure;
		Box::pin(closure(&self.delegate, params))
	}
}

struct DelegateMethodWithMeta<T, F> {
	delegate: Arc<T>,
	closure: F,
}

impl<T, M, F, I> RpcMethod<M> for DelegateMethodWithMeta<T, F>
where
	M: Metadata,
	F: Fn(&T, Params, M) -> I,
	I: Future<Output = Result<Value, Error>> + Send + 'static,
	T: Send + Sync + 'static,
	F: Send + Sync + 'static,
{
	fn call(&self, params: Params, meta: M) -> BoxFuture<crate::Result<Value>> {
		let closure = &self.closure;
		Box::pin(closure(&self.delegate, params, meta))
	}
}

struct DelegateNotification<T, F> {
	delegate: Arc<T>,
	closure: F,
}

impl<T, M, F> RpcNotification<M> for DelegateNotification<T, F>
where
	M: Metadata,
	F: Fn(&T, Params) + 'static,
	F: Send + Sync + 'static,
	T: Send + Sync + 'static,
{
	fn execute(&self, params: Params, _meta: M) {
		let closure = &self.closure;
		closure(&self.delegate, params)
	}
}

struct DelegateNotificationWithMeta<T, F> {
	delegate: Arc<T>,
	closure: F,
}

impl<T, M, F> RpcNotification<M> for DelegateNotificationWithMeta<T, F>
where
	M: Metadata,
	F: Fn(&T, Params, M) + 'static,
	F: Send + Sync + 'static,
	T: Send + Sync + 'static,
{
	fn execute(&self, params: Params, meta: M) {
		let closure = &self.closure;
		closure(&self.delegate, params, meta)
	}
}

/// A set of RPC methods and notifications tied to single `delegate` struct.
pub struct IoDelegate<T, M = ()>
where
	T: Send + Sync + 'static,
	M: Metadata,
{
	delegate: Arc<T>,
	methods: HashMap<String, RemoteProcedure<M>>,
}

impl<T, M> IoDelegate<T, M>
where
	T: Send + Sync + 'static,
	M: Metadata,
{
	/// Creates new `IoDelegate`
	pub fn new(delegate: Arc<T>) -> Self {
		IoDelegate {
			delegate,
			methods: HashMap::new(),
		}
	}

	/// Adds an alias to existing method.
	/// NOTE: Aliases are not transitive, i.e. you cannot create alias to an alias.
	pub fn add_alias(&mut self, from: &str, to: &str) {
		self.methods.insert(from.into(), RemoteProcedure::Alias(to.into()));
	}

	/// Adds async method to the delegate.
	pub fn add_method<F, I>(&mut self, name: &str, method: F)
	where
		F: Fn(&T, Params) -> I,
		I: Future<Output = Result<Value, Error>> + Send + 'static,
		F: Send + Sync + 'static,
	{
		self.methods.insert(
			name.into(),
			RemoteProcedure::Method(Arc::new(DelegateAsyncMethod {
				delegate: self.delegate.clone(),
				closure: method,
			})),
		);
	}

	/// Adds async method with metadata to the delegate.
	pub fn add_method_with_meta<F, I>(&mut self, name: &str, method: F)
	where
		F: Fn(&T, Params, M) -> I,
		I: Future<Output = Result<Value, Error>> + Send + 'static,
		F: Send + Sync + 'static,
	{
		self.methods.insert(
			name.into(),
			RemoteProcedure::Method(Arc::new(DelegateMethodWithMeta {
				delegate: self.delegate.clone(),
				closure: method,
			})),
		);
	}

	/// Adds notification to the delegate.
	pub fn add_notification<F>(&mut self, name: &str, notification: F)
	where
		F: Fn(&T, Params),
		F: Send + Sync + 'static,
	{
		self.methods.insert(
			name.into(),
			RemoteProcedure::Notification(Arc::new(DelegateNotification {
				delegate: self.delegate.clone(),
				closure: notification,
			})),
		);
	}

	/// Adds notification with metadata to the delegate.
	pub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)
	where
		F: Fn(&T, Params, M),
		F: Send + Sync + 'static,
	{
		self.methods.insert(
			name.into(),
			RemoteProcedure::Notification(Arc::new(DelegateNotificationWithMeta {
				delegate: self.delegate.clone(),
				closure: notification,
			})),
		);
	}
}

impl<T, M> crate::io::IoHandlerExtension<M> for IoDelegate<T, M>
where
	T: Send + Sync + 'static,
	M: Metadata,
{
	fn augment<S: crate::Middleware<M>>(self, handler: &mut crate::MetaIoHandler<M, S>) {
		handler.extend_with(self.methods)
	}
}

impl<T, M> IntoIterator for IoDelegate<T, M>
where
	T: Send + Sync + 'static,
	M: Metadata,
{
	type Item = (String, RemoteProcedure<M>);
	type IntoIter = std::collections::hash_map::IntoIter<String, RemoteProcedure<M>>;

	fn into_iter(self) -> Self::IntoIter {
		self.methods.into_iter()
	}
}