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
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use std::collections::HashSet;
use std::str::FromStr;

use super::RpcDescription;
use crate::{
	helpers::{generate_where_clause, is_option},
	rpc_macro::RpcFnArg,
};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned};
use syn::Attribute;

impl RpcDescription {
	pub(super) fn render_server(&self) -> Result<TokenStream2, syn::Error> {
		let trait_name = quote::format_ident!("{}Server", &self.trait_def.ident);
		let generics = self.trait_def.generics.clone();
		let (impl_generics, _, where_clause) = generics.split_for_impl();

		let method_impls = self.render_methods()?;
		let into_rpc_impl = self.render_into_rpc()?;
		let async_trait = self.jrps_server_item(quote! { core::__reexports::async_trait });

		// Doc-comment to be associated with the server.
		let doc_comment = format!("Server trait implementation for the `{}` RPC API.", &self.trait_def.ident);

		let trait_impl = quote! {
			#[#async_trait]
			#[doc = #doc_comment]
			pub trait #trait_name #impl_generics: Sized + Send + Sync + 'static #where_clause {
				#method_impls
				#into_rpc_impl
			}
		};

		Ok(trait_impl)
	}

	fn render_methods(&self) -> Result<TokenStream2, syn::Error> {
		let methods = self.methods.iter().map(|method| {
			let docs = &method.docs;
			let mut method_sig = method.signature.clone();

			if method.with_extensions {
				let ext_ty = self.jrps_server_item(quote! { Extensions });
				// Add `Extension` as the second parameter to the signature.
				let ext: syn::FnArg = syn::parse_quote!(ext: &#ext_ty);
				method_sig.sig.inputs.insert(1, ext);
			}

			quote! {
				#docs
				#method_sig
			}
		});

		let subscriptions = self.subscriptions.iter().map(|sub| {
			let docs = &sub.docs;
			let subscription_sink_ty = self.jrps_server_item(quote! { PendingSubscriptionSink });

			// Add `SubscriptionSink` as the second input parameter to the signature.
			let subscription_sink: syn::FnArg = syn::parse_quote!(subscription_sink: #subscription_sink_ty);
			let mut sub_sig = sub.signature.clone();
			sub_sig.sig.inputs.insert(1, subscription_sink);

			if sub.with_extensions {
				let ext_ty = self.jrps_server_item(quote! { Extensions });
				// Add `Extension` as the third parameter to the signature.
				let ext: syn::FnArg = syn::parse_quote!(ext: &#ext_ty);
				sub_sig.sig.inputs.insert(2, ext);
			}

			quote! {
				#docs
				#sub_sig
			}
		});

		Ok(quote! {
			#(#methods)*
			#(#subscriptions)*
		})
	}

	/// Helper that will ignore results of `register_*` method calls, and panic if there have been
	/// any errors in debug builds.
	///
	/// The debug assert is a safeguard should the contract that guarantees the method names to
	/// never conflict in the macro be broken in the future.
	fn handle_register_result(&self, tokens: TokenStream2) -> TokenStream2 {
		let reexports = self.jrps_server_item(quote! { core::__reexports });
		quote! {{
			let _res = #tokens;
			#[cfg(debug_assertions)]
			if _res.is_err() {
				#reexports::panic_fail_register();
			}
		}}
	}

	fn render_into_rpc(&self) -> Result<TokenStream2, syn::Error> {
		let rpc_module = self.jrps_server_item(quote! { RpcModule });

		let mut registered = HashSet::new();
		let mut errors = Vec::new();
		let mut check_name = |name: &str, span: Span| {
			if registered.contains(name) {
				let message = format!("{name:?} is already defined");
				errors.push(quote_spanned!(span => compile_error!(#message);));
			} else {
				registered.insert(name.to_string());
			}
		};

		let methods = self
			.methods
			.iter()
			.map(|method| {
				// Rust method to invoke (e.g. `self.<foo>(...)`).
				let rust_method_name = &method.signature.sig.ident;
				// Name of the RPC method (e.g. `foo_makeSpam`).
				let rpc_method_name = self.rpc_identifier(&method.name);
				// `parsing` is the code associated with parsing structure from the
				// provided `Params` object.
				// `params_seq` is the comma-delimited sequence of parameters we're passing to the rust function
				// called..
				let (parsing, params_seq) = self.render_params_decoding(&method.params, None);

				let into_response = self.jrps_server_item(quote! { IntoResponse });

				check_name(&rpc_method_name, rust_method_name.span());

				if method.signature.sig.asyncness.is_some() {
					if method.with_extensions {
						self.handle_register_result(quote! {
							rpc.register_async_method(#rpc_method_name, |params, context, ext| async move {
								#parsing
								#into_response::into_response(context.as_ref().#rust_method_name(&ext, #params_seq).await)
							})
						})
					} else {
						self.handle_register_result(quote! {
							rpc.register_async_method(#rpc_method_name, |params, context, _| async move {
								#parsing
								#into_response::into_response(context.as_ref().#rust_method_name(#params_seq).await)
							})
						})
					}
				} else {
					let register_kind =
						if method.blocking { quote!(register_blocking_method) } else { quote!(register_method) };

					if method.with_extensions {
						self.handle_register_result(quote! {
							rpc.#register_kind(#rpc_method_name, |params, context, ext| {
								#parsing
								#into_response::into_response(context.#rust_method_name(&ext, #params_seq))
							})
						})
					} else {
						self.handle_register_result(quote! {
							rpc.#register_kind(#rpc_method_name, |params, context, _| {
								#parsing
								#into_response::into_response(context.#rust_method_name(#params_seq))
							})
						})
					}
				}
			})
			.collect::<Vec<_>>();

		let subscriptions = self
			.subscriptions
			.iter()
			.map(|sub| {
				// Rust method to invoke (e.g. `self.<foo>(...)`).
				let rust_method_name = &sub.signature.sig.ident;
				// Name of the RPC method to subscribe to (e.g. `foo_sub`).
				let rpc_sub_name = self.rpc_identifier(&sub.name);
				// Name of `method` in the subscription response.
				let rpc_notif_name_override = sub.notif_name_override.as_ref().map(|m| self.rpc_identifier(m));
				// Name of the RPC method to unsubscribe (e.g. `foo_sub`).
				let rpc_unsub_name = self.rpc_identifier(&sub.unsubscribe);
				// `parsing` is the code associated with parsing structure from the
				// provided `Params` object.
				// `params_seq` is the comma-delimited sequence of parameters.
				let pending = proc_macro2::Ident::new("pending", rust_method_name.span());
				let (parsing, params_seq) = self.render_params_decoding(&sub.params, Some(pending));
				let sub_err = self.jrps_server_item(quote! { SubscriptionCloseResponse });
				let into_sub_response = self.jrps_server_item(quote! { IntoSubscriptionCloseResponse });

				check_name(&rpc_sub_name, rust_method_name.span());
				check_name(&rpc_unsub_name, rust_method_name.span());

				let rpc_notif_name = match rpc_notif_name_override {
					Some(notif) => {
						check_name(&notif, rust_method_name.span());
						notif
					}
					None => rpc_sub_name.clone(),
				};

				if sub.signature.sig.asyncness.is_some() {
					if sub.with_extensions {
						self.handle_register_result(quote! {
							rpc.register_subscription(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, ext| async move {
								#parsing
								#into_sub_response::into_response(context.as_ref().#rust_method_name(pending, &ext, #params_seq).await)
							})
						})
					} else {
						self.handle_register_result(quote! {
							rpc.register_subscription(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, _| async move {
								#parsing
								#into_sub_response::into_response(context.as_ref().#rust_method_name(pending, #params_seq).await)
							})
						})
					}
				} else if sub.with_extensions {
					self.handle_register_result(quote! {
						rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, ext| {
							#parsing
							let _ = context.as_ref().#rust_method_name(pending, &ext, #params_seq);
							#sub_err::None
						})
					})
				} else {
					self.handle_register_result(quote! {
						rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, _| {
							#parsing
							let _ = context.as_ref().#rust_method_name(pending, #params_seq);
							#sub_err::None
						})
					})
				}
			})
			.collect::<Vec<_>>();

		let method_aliases = self
			.methods
			.iter()
			.map(|method| {
				let rpc_name = self.rpc_identifier(&method.name);
				let rust_method_name = &method.signature.sig.ident;

				// Rust method to invoke (e.g. `self.<foo>(...)`).
				let aliases: Vec<TokenStream2> = method
					.aliases
					.iter()
					.map(|alias| {
						check_name(alias, rust_method_name.span());
						self.handle_register_result(quote! {
							rpc.register_alias(#alias, #rpc_name)
						})
					})
					.collect();

				quote!( #(#aliases)* )
			})
			.collect::<Vec<_>>();

		let subscription_aliases = self
			.subscriptions
			.iter()
			.map(|method| {
				let sub_name = self.rpc_identifier(&method.name);
				let unsub_name = self.rpc_identifier(&method.unsubscribe);
				let rust_method_name = &method.signature.sig.ident;

				let sub: Vec<TokenStream2> = method
					.aliases
					.iter()
					.map(|alias| {
						check_name(alias, rust_method_name.span());
						self.handle_register_result(quote! {
							rpc.register_alias(#alias, #sub_name)
						})
					})
					.collect();
				let unsub: Vec<TokenStream2> = method
					.unsubscribe_aliases
					.iter()
					.map(|alias| {
						check_name(alias, rust_method_name.span());
						self.handle_register_result(quote! {
							rpc.register_alias(#alias, #unsub_name)
						})
					})
					.collect();

				quote! (
					#(#sub)*
					#(#unsub)*
				)
			})
			.collect::<Vec<_>>();

		let doc_comment = "Collects all the methods and subscriptions defined in the trait \
								and adds them into a single `RpcModule`.";

		let sub_tys: Vec<syn::Type> = self.subscriptions.clone().into_iter().map(|s| s.item).collect();
		let where_clause = generate_where_clause(&self.trait_def, &sub_tys, false, self.server_bounds.as_ref());

		// NOTE(niklasad1): empty where clause is valid rust syntax.
		Ok(quote! {
			#[doc = #doc_comment]
			fn into_rpc(self) -> #rpc_module<Self> where #(#where_clause,)* {
				let mut rpc = #rpc_module::new(self);

				#(#errors)*
				#(#methods)*
				#(#subscriptions)*
				#(#method_aliases)*
				#(#subscription_aliases)*

				rpc
			}
		})
	}

	fn render_params_decoding(
		&self,
		params: &[RpcFnArg],
		sub: Option<proc_macro2::Ident>,
	) -> (TokenStream2, TokenStream2) {
		if params.is_empty() {
			return (TokenStream2::default(), TokenStream2::default());
		}

		let params_fields_seq = params.iter().map(RpcFnArg::arg_pat);
		let params_fields = quote! { #(#params_fields_seq),* };

		let reexports = self.jrps_server_item(quote! { core::__reexports });

		let error_ret = if let Some(pending) = &sub {
			let tokio = quote! { #reexports::tokio };
			let sub_err = self.jrps_server_item(quote! { SubscriptionCloseResponse });
			quote! {
				#tokio::spawn(#pending.reject(e));
				return #sub_err::None;
			}
		} else {
			let response_payload = self.jrps_server_item(quote! { ResponsePayload });
			quote! {
				return #response_payload::error(e);
			}
		};

		// Code to decode sequence of parameters from a JSON array.
		let decode_array = {
			let decode_fields = params.iter().map(|RpcFnArg { arg_pat, ty, .. }| {
				let is_option = is_option(ty);
				let next_method = if is_option { quote!(optional_next) } else { quote!(next) };
				quote! {
					let #arg_pat: #ty = match seq.#next_method() {
						Ok(v) => v,
						Err(e) => {
							#reexports::log_fail_parse(stringify!(#arg_pat), stringify!(#ty), &e, #is_option);
							#error_ret
						}
					};
				}
			});

			quote! {
				let mut seq = params.sequence();
				#(#decode_fields);*
				(#params_fields)
			}
		};

		// Code to decode sequence of parameters from a JSON object (aka map).
		let decode_map = {
			let generics = (0..params.len()).map(|n| quote::format_ident!("G{}", n));

			let serde = self.jrps_server_item(quote! { core::__reexports::serde });
			let serde_crate = serde.to_string();

			let fields = params.iter().zip(generics.clone()).map(|(fn_arg, ty)| {
				let arg_pat = fn_arg.arg_pat();
				let name = fn_arg.name();

				let mut alias_vals = String::new();
				alias_vals.push_str(&format!(r#"alias = "{}""#, heck::ToSnakeCase::to_snake_case(name.as_str())));
				alias_vals.push(',');
				alias_vals
					.push_str(&format!(r#"alias = "{}""#, heck::ToLowerCamelCase::to_lower_camel_case(name.as_str())));

				let serde_rename = quote!(#[serde(rename = #name)]);

				let alias = TokenStream2::from_str(alias_vals.as_str()).unwrap();

				let serde_alias: Attribute = syn::parse_quote! {
					#[serde(#alias)]
				};

				quote! {
					#serde_alias
					#serde_rename
					#arg_pat: #ty,
				}
			});
			let destruct = params.iter().map(RpcFnArg::arg_pat).map(|a| quote!(parsed.#a));
			let types = params.iter().map(RpcFnArg::ty);

			quote! {
				#[derive(#serde::Deserialize)]
				#[serde(crate = #serde_crate)]
				struct ParamsObject<#(#generics,)*> {
					#(#fields)*
				}

				let parsed: ParamsObject<#(#types,)*> = match params.parse() {
					Ok(p) => p,
					Err(e) => {
						#reexports::log_fail_parse_as_object(&e);
						#error_ret
					}
				};

				(#(#destruct),*)
			}
		};

		let parsing = quote! {
			let (#params_fields) = if params.is_object() {
				#decode_map
			} else {
				#decode_array
			};
		};

		(parsing, params_fields)
	}
}