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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use std::collections::HashSet;

use crate::params_style::ParamStyle;
use crate::rpc_attr::RpcMethodAttribute;
use quote::quote;
use syn::{
	parse_quote,
	punctuated::Punctuated,
	visit::{self, Visit},
	Result, Token,
};

pub enum MethodRegistration {
	Standard {
		method: RpcMethod,
		has_metadata: bool,
	},
	PubSub {
		name: String,
		subscribes: Vec<RpcMethod>,
		unsubscribe: RpcMethod,
	},
	Notification {
		method: RpcMethod,
		has_metadata: bool,
	},
}

impl MethodRegistration {
	fn generate(&self) -> Result<proc_macro2::TokenStream> {
		match self {
			MethodRegistration::Standard { method, has_metadata } => {
				let rpc_name = &method.name();
				let add_method = if *has_metadata {
					quote!(add_method_with_meta)
				} else {
					quote!(add_method)
				};
				let closure = method.generate_delegate_closure(false)?;
				let add_aliases = method.generate_add_aliases();

				Ok(quote! {
					del.#add_method(#rpc_name, #closure);
					#add_aliases
				})
			}
			MethodRegistration::PubSub {
				name,
				subscribes,
				unsubscribe,
			} => {
				let unsub_name = unsubscribe.name();
				let unsub_method_ident = unsubscribe.ident();
				let unsub_closure = quote! {
					move |base, id, meta| {
						use self::_futures::{Future, IntoFuture};
						Self::#unsub_method_ident(base, meta, id).into_future()
							.map(|value| _jsonrpc_core::to_value(value)
									.expect("Expected always-serializable type; qed"))
							.map_err(Into::into)
					}
				};

				let mut add_subscriptions = proc_macro2::TokenStream::new();

				for subscribe in subscribes.iter() {
					let sub_name = subscribe.name();
					let sub_closure = subscribe.generate_delegate_closure(true)?;
					let sub_aliases = subscribe.generate_add_aliases();

					add_subscriptions = quote! {
						#add_subscriptions
						del.add_subscription(
							#name,
							(#sub_name, #sub_closure),
							(#unsub_name, #unsub_closure),
						);
						#sub_aliases
					};
				}

				let unsub_aliases = unsubscribe.generate_add_aliases();

				Ok(quote! {
					#add_subscriptions
					#unsub_aliases
				})
			}
			MethodRegistration::Notification { method, has_metadata } => {
				let name = &method.name();
				let add_notification = if *has_metadata {
					quote!(add_notification_with_meta)
				} else {
					quote!(add_notification)
				};
				let closure = method.generate_delegate_closure(false)?;
				let add_aliases = method.generate_add_aliases();

				Ok(quote! {
					del.#add_notification(#name, #closure);
					#add_aliases
				})
			}
		}
	}
}

const SUBSCRIBER_TYPE_IDENT: &str = "Subscriber";
const METADATA_CLOSURE_ARG: &str = "meta";
const SUBSCRIBER_CLOSURE_ARG: &str = "subscriber";

// tuples are limited to 16 fields: the maximum supported by `serde::Deserialize`
const TUPLE_FIELD_NAMES: [&str; 16] = [
	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
];

pub fn generate_trait_item_method(
	methods: &[MethodRegistration],
	trait_item: &syn::ItemTrait,
	has_metadata: bool,
	has_pubsub_methods: bool,
) -> Result<syn::TraitItemMethod> {
	let io_delegate_type = if has_pubsub_methods {
		quote!(_jsonrpc_pubsub::IoDelegate)
	} else {
		quote!(_jsonrpc_core::IoDelegate)
	};
	let add_methods = methods
		.iter()
		.map(MethodRegistration::generate)
		.collect::<Result<Vec<_>>>()?;
	let to_delegate_body = quote! {
		let mut del = #io_delegate_type::new(self.into());
		#(#add_methods)*
		del
	};

	let method: syn::TraitItemMethod = if has_metadata {
		parse_quote! {
			/// Create an `IoDelegate`, wiring rpc calls to the trait methods.
			fn to_delegate(self) -> #io_delegate_type<Self, Self::Metadata> {
				#to_delegate_body
			}
		}
	} else {
		parse_quote! {
			/// Create an `IoDelegate`, wiring rpc calls to the trait methods.
			fn to_delegate<M: _jsonrpc_core::Metadata>(self) -> #io_delegate_type<Self, M> {
				#to_delegate_body
			}
		}
	};

	let predicates = generate_where_clause_serialization_predicates(&trait_item, false);
	let mut method = method.clone();
	method.sig.generics.make_where_clause().predicates.extend(predicates);
	Ok(method)
}

#[derive(Clone)]
pub struct RpcMethod {
	pub attr: RpcMethodAttribute,
	pub trait_item: syn::TraitItemMethod,
}

impl RpcMethod {
	pub fn new(attr: RpcMethodAttribute, trait_item: syn::TraitItemMethod) -> RpcMethod {
		RpcMethod { attr, trait_item }
	}

	pub fn attr(&self) -> &RpcMethodAttribute {
		&self.attr
	}

	pub fn name(&self) -> &str {
		&self.attr.name
	}

	pub fn ident(&self) -> &syn::Ident {
		&self.trait_item.sig.ident
	}

	pub fn is_pubsub(&self) -> bool {
		self.attr.is_pubsub()
	}

	pub fn subscriber_arg(&self) -> Option<syn::Type> {
		self.trait_item
			.sig
			.inputs
			.iter()
			.filter_map(|arg| match arg {
				syn::FnArg::Typed(ty) => Some(*ty.ty.clone()),
				_ => None,
			})
			.find(|ty| {
				if let syn::Type::Path(path) = ty {
					if path.path.segments.iter().any(|s| s.ident == SUBSCRIBER_TYPE_IDENT) {
						return true;
					}
				}
				false
			})
	}

	fn generate_delegate_closure(&self, is_subscribe: bool) -> Result<proc_macro2::TokenStream> {
		let mut param_types: Vec<_> = self
			.trait_item
			.sig
			.inputs
			.iter()
			.cloned()
			.filter_map(|arg| match arg {
				syn::FnArg::Typed(ty) => Some(*ty.ty),
				_ => None,
			})
			.collect();

		// special args are those which are not passed directly via rpc params: metadata, subscriber
		let special_args = Self::special_args(&param_types);
		param_types.retain(|ty| special_args.iter().find(|(_, sty)| sty == ty).is_none());
		if param_types.len() > TUPLE_FIELD_NAMES.len() {
			return Err(syn::Error::new_spanned(
				&self.trait_item,
				&format!("Maximum supported number of params is {}", TUPLE_FIELD_NAMES.len()),
			));
		}
		let tuple_fields: &Vec<_> = &(TUPLE_FIELD_NAMES
			.iter()
			.take(param_types.len())
			.map(|name| ident(name))
			.collect());
		let param_types = &param_types;
		let parse_params = {
			// last arguments that are `Option`-s are optional 'trailing' arguments
			let trailing_args_num = param_types.iter().rev().take_while(|t| is_option_type(t)).count();
			if trailing_args_num != 0 {
				self.params_with_trailing(trailing_args_num, param_types, tuple_fields)
			} else if param_types.is_empty() {
				quote! { let params = params.expect_no_params(); }
			} else if self.attr.params_style == Some(ParamStyle::Raw) {
				quote! { let params: _jsonrpc_core::Result<_> = Ok((params,)); }
			} else if self.attr.params_style == Some(ParamStyle::Positional) {
				quote! { let params = params.parse::<(#(#param_types, )*)>(); }
			} else
			/* if self.attr.params_style == Some(ParamStyle::Named) */
			{
				unimplemented!("Server side named parameters are not implemented");
			}
		};

		let method_ident = self.ident();
		let result = &self.trait_item.sig.output;
		let extra_closure_args: &Vec<_> = &special_args.iter().cloned().map(|arg| arg.0).collect();
		let extra_method_types: &Vec<_> = &special_args.iter().cloned().map(|arg| arg.1).collect();

		let closure_args = quote! { base, params, #(#extra_closure_args), * };
		let method_sig = quote! { fn(&Self, #(#extra_method_types, ) * #(#param_types), *) #result };
		let method_call = quote! { (base, #(#extra_closure_args, )* #(#tuple_fields), *) };
		let match_params = if is_subscribe {
			quote! {
				Ok((#(#tuple_fields, )*)) => {
					let subscriber = _jsonrpc_pubsub::typed::Subscriber::new(subscriber);
					(method)#method_call
				},
				Err(e) => {
					let _ = subscriber.reject(e);
					return
				}
			}
		} else if self.attr.is_notification() {
			quote! {
				Ok((#(#tuple_fields, )*)) => {
					(method)#method_call
				},
				Err(_) => return,
			}
		} else {
			quote! {
				Ok((#(#tuple_fields, )*)) => {
					use self::_futures::{Future, IntoFuture};
					let fut = (method)#method_call
						.into_future()
						.map(|value| _jsonrpc_core::to_value(value)
							.expect("Expected always-serializable type; qed"))
						.map_err(Into::into as fn(_) -> _jsonrpc_core::Error);
					_futures::future::Either::A(fut)
				},
				Err(e) => _futures::future::Either::B(_futures::failed(e)),
			}
		};

		Ok(quote! {
			move |#closure_args| {
				let method = &(Self::#method_ident as #method_sig);
				#parse_params
				match params {
					#match_params
				}
			}
		})
	}

	fn special_args(param_types: &[syn::Type]) -> Vec<(syn::Ident, syn::Type)> {
		let meta_arg = param_types.first().and_then(|ty| {
			if *ty == parse_quote!(Self::Metadata) {
				Some(ty.clone())
			} else {
				None
			}
		});
		let subscriber_arg = param_types.get(1).and_then(|ty| {
			if let syn::Type::Path(path) = ty {
				if path.path.segments.iter().any(|s| s.ident == SUBSCRIBER_TYPE_IDENT) {
					Some(ty.clone())
				} else {
					None
				}
			} else {
				None
			}
		});

		let mut special_args = Vec::new();
		if let Some(meta) = meta_arg {
			special_args.push((ident(METADATA_CLOSURE_ARG), meta.clone()));
		}
		if let Some(subscriber) = subscriber_arg {
			special_args.push((ident(SUBSCRIBER_CLOSURE_ARG), subscriber.clone()));
		}
		special_args
	}

	fn params_with_trailing(
		&self,
		trailing_args_num: usize,
		param_types: &[syn::Type],
		tuple_fields: &[syn::Ident],
	) -> proc_macro2::TokenStream {
		let total_args_num = param_types.len();
		let required_args_num = total_args_num - trailing_args_num;

		let switch_branches = (0..=trailing_args_num)
			.map(|passed_trailing_args_num| {
				let passed_args_num = required_args_num + passed_trailing_args_num;
				let passed_param_types = &param_types[..passed_args_num];
				let passed_tuple_fields = &tuple_fields[..passed_args_num];
				let missed_args_num = total_args_num - passed_args_num;
				let missed_params_values = ::std::iter::repeat(quote! { None })
					.take(missed_args_num)
					.collect::<Vec<_>>();

				if passed_args_num == 0 {
					quote! {
						#passed_args_num => params.expect_no_params()
							.map(|_| (#(#missed_params_values, ) *))
							.map_err(Into::into)
					}
				} else {
					quote! {
						#passed_args_num => params.parse::<(#(#passed_param_types, )*)>()
							.map(|(#(#passed_tuple_fields,)*)|
								(#(#passed_tuple_fields, )* #(#missed_params_values, )*))
							.map_err(Into::into)
					}
				}
			})
			.collect::<Vec<_>>();

		quote! {
			let passed_args_num = match params {
				_jsonrpc_core::Params::Array(ref v) => Ok(v.len()),
				_jsonrpc_core::Params::None => Ok(0),
				_ => Err(_jsonrpc_core::Error::invalid_params("`params` should be an array"))
			};

			let params = passed_args_num.and_then(|passed_args_num| {
				match passed_args_num {
					_ if passed_args_num < #required_args_num => Err(_jsonrpc_core::Error::invalid_params(
						format!("`params` should have at least {} argument(s)", #required_args_num))),
					#(#switch_branches),*,
					_ => Err(_jsonrpc_core::Error::invalid_params_with_details(
						format!("Expected from {} to {} parameters.", #required_args_num, #total_args_num),
						format!("Got: {}", passed_args_num))),
				}
			});
		}
	}

	fn generate_add_aliases(&self) -> proc_macro2::TokenStream {
		let name = self.name();
		let add_aliases: Vec<_> = self
			.attr
			.aliases
			.iter()
			.map(|alias| quote! { del.add_alias(#alias, #name); })
			.collect();
		quote! { #(#add_aliases)* }
	}
}

fn ident(s: &str) -> syn::Ident {
	syn::Ident::new(s, proc_macro2::Span::call_site())
}

fn is_option_type(ty: &syn::Type) -> bool {
	if let syn::Type::Path(path) = ty {
		path.path.segments.first().map_or(false, |t| t.ident == "Option")
	} else {
		false
	}
}

pub fn generate_where_clause_serialization_predicates(
	item_trait: &syn::ItemTrait,
	client: bool,
) -> Vec<syn::WherePredicate> {
	#[derive(Default)]
	struct FindTyParams {
		trait_generics: HashSet<syn::Ident>,
		server_to_client_type_params: HashSet<syn::Ident>,
		client_to_server_type_params: HashSet<syn::Ident>,
		visiting_return_type: bool,
		visiting_fn_arg: bool,
		visiting_subscriber_arg: bool,
	}
	impl<'ast> Visit<'ast> for FindTyParams {
		fn visit_type_param(&mut self, ty_param: &'ast syn::TypeParam) {
			self.trait_generics.insert(ty_param.ident.clone());
		}
		fn visit_return_type(&mut self, return_type: &'ast syn::ReturnType) {
			self.visiting_return_type = true;
			visit::visit_return_type(self, return_type);
			self.visiting_return_type = false
		}
		fn visit_path_segment(&mut self, segment: &'ast syn::PathSegment) {
			self.visiting_subscriber_arg =
				self.visiting_subscriber_arg || (self.visiting_fn_arg && segment.ident == SUBSCRIBER_TYPE_IDENT);
			visit::visit_path_segment(self, segment);
			self.visiting_subscriber_arg = self.visiting_subscriber_arg && segment.ident != SUBSCRIBER_TYPE_IDENT;
		}
		fn visit_ident(&mut self, ident: &'ast syn::Ident) {
			if self.trait_generics.contains(&ident) {
				if self.visiting_return_type || self.visiting_subscriber_arg {
					self.server_to_client_type_params.insert(ident.clone());
				}
				if self.visiting_fn_arg && !self.visiting_subscriber_arg {
					self.client_to_server_type_params.insert(ident.clone());
				}
			}
		}
		fn visit_fn_arg(&mut self, arg: &'ast syn::FnArg) {
			self.visiting_fn_arg = true;
			visit::visit_fn_arg(self, arg);
			self.visiting_fn_arg = false;
		}
	}
	let mut visitor = FindTyParams::default();
	visitor.visit_item_trait(item_trait);

	let additional_where_clause = item_trait.generics.where_clause.clone();

	item_trait
		.generics
		.type_params()
		.map(|ty| {
			let ty_path = syn::TypePath {
				qself: None,
				path: ty.ident.clone().into(),
			};
			let mut bounds: Punctuated<syn::TypeParamBound, Token![+]> = parse_quote!(Send + Sync + 'static);
			// add json serialization trait bounds
			if client {
				if visitor.server_to_client_type_params.contains(&ty.ident) {
					bounds.push(parse_quote!(_jsonrpc_core::serde::de::DeserializeOwned))
				}
				if visitor.client_to_server_type_params.contains(&ty.ident) {
					bounds.push(parse_quote!(_jsonrpc_core::serde::Serialize))
				}
			} else {
				if visitor.server_to_client_type_params.contains(&ty.ident) {
					bounds.push(parse_quote!(_jsonrpc_core::serde::Serialize))
				}
				if visitor.client_to_server_type_params.contains(&ty.ident) {
					bounds.push(parse_quote!(_jsonrpc_core::serde::de::DeserializeOwned))
				}
			}

			// add the trait bounds specified by the user in where clause.
			if let Some(ref where_clause) = additional_where_clause {
				for predicate in where_clause.predicates.iter() {
					if let syn::WherePredicate::Type(where_ty) = predicate {
						if let syn::Type::Path(ref predicate) = where_ty.bounded_ty {
							if *predicate == ty_path {
								bounds.extend(where_ty.bounds.clone().into_iter());
							}
						}
					}
				}
			}

			syn::WherePredicate::Type(syn::PredicateType {
				lifetimes: None,
				bounded_ty: syn::Type::Path(ty_path),
				colon_token: <Token![:]>::default(),
				bounds,
			})
		})
		.collect()
}