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
// Copyright 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 syn::visit::{self, Visit};
use syn::Ident;

/// Visitor that parses generic type parameters from `syn::Type` by traversing the AST.
/// A `syn::Type` can any type such as `Vec<T>, T, Foo<A<B<V>>>, usize or similar`.
/// The implementation is based on <https://github.com/serde-rs/serde/blob/master/serde_derive/src/bound.rs>.
#[derive(Default, Debug)]
pub(crate) struct FindSubscriptionParams {
	pub(crate) generic_sub_params: HashSet<Ident>,
	pub(crate) all_type_params: HashSet<Ident>,
}

/// Visitor for the entire `RPC trait`.
pub struct FindAllParams {
	pub(crate) trait_generics: HashSet<syn::Ident>,
	pub(crate) input_params: HashSet<syn::Ident>,
	pub(crate) ret_params: HashSet<syn::Ident>,
	pub(crate) sub_params: HashSet<syn::Ident>,
	pub(crate) visiting_return_type: bool,
	pub(crate) visiting_fn_arg: bool,
}

impl FindAllParams {
	/// Create a visitor to traverse the entire RPC trait.
	/// It takes the already visited subscription parameters as input.
	pub fn new(sub_params: HashSet<syn::Ident>) -> Self {
		Self {
			trait_generics: HashSet::new(),
			input_params: HashSet::new(),
			ret_params: HashSet::new(),
			sub_params,
			visiting_return_type: false,
			visiting_fn_arg: false,
		}
	}
}

impl<'ast> Visit<'ast> for FindAllParams {
	/// Visit generic type param.
	fn visit_type_param(&mut self, ty_param: &'ast syn::TypeParam) {
		self.trait_generics.insert(ty_param.ident.clone());
	}

	/// Visit return type and mark it as `visiting_return_type`.
	/// To know whether a given Ident is a function argument or return type when traversing.
	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
	}

	/// Visit ident.
	fn visit_ident(&mut self, ident: &'ast syn::Ident) {
		if self.trait_generics.contains(ident) {
			if self.visiting_return_type {
				self.ret_params.insert(ident.clone());
			}
			if self.visiting_fn_arg {
				self.input_params.insert(ident.clone());
			}
		}
	}

	/// Visit function argument and mark it as `visiting_fn_arg`.
	/// To know whether a given Ident is a function argument or return type when traversing.
	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;
	}
}

impl FindSubscriptionParams {
	/// Visit all types and returns all generic [`struct@syn::Ident`]'s that are subscriptions.
	pub fn visit(mut self, tys: &[syn::Type]) -> HashSet<Ident> {
		for ty in tys {
			self.visit_type(ty);
		}
		self.generic_sub_params
	}

	/// Create a new subscription parameters visitor that takes all
	/// generic parameters on the RPC trait as input in order to determine
	/// whether a given ident is a generic type param or not when traversing
	/// one or more types in `FindSubscriptionParams::visit`.
	pub fn new(all_type_params: HashSet<Ident>) -> Self {
		Self { generic_sub_params: HashSet::new(), all_type_params }
	}

	/// Visit path, if it's a leaf path and generic type param then add it as a subscription param.
	fn visit_path(&mut self, path: &syn::Path) {
		if path.leading_colon.is_none() && path.segments.len() == 1 {
			let id = &path.segments[0].ident;
			if self.all_type_params.contains(id) {
				self.generic_sub_params.insert(id.clone());
			}
		}
		for segment in &path.segments {
			self.visit_path_segment(segment);
		}
	}

	/// Traverse syntax tree.
	fn visit_type(&mut self, ty: &syn::Type) {
		match ty {
			syn::Type::Array(ty) => self.visit_type(&ty.elem),
			syn::Type::BareFn(ty) => {
				for arg in &ty.inputs {
					self.visit_type(&arg.ty);
				}
				self.visit_return_type(&ty.output);
			}
			syn::Type::Group(ty) => self.visit_type(&ty.elem),
			syn::Type::ImplTrait(ty) => {
				for bound in &ty.bounds {
					self.visit_type_param_bound(bound);
				}
			}
			syn::Type::Macro(ty) => self.visit_macro(&ty.mac),
			syn::Type::Paren(ty) => self.visit_type(&ty.elem),
			syn::Type::Path(ty) => {
				if let Some(qself) = &ty.qself {
					self.visit_type(&qself.ty);
				}
				self.visit_path(&ty.path);
			}
			syn::Type::Ptr(ty) => self.visit_type(&ty.elem),
			syn::Type::Reference(ty) => self.visit_type(&ty.elem),
			syn::Type::Slice(ty) => self.visit_type(&ty.elem),
			syn::Type::TraitObject(ty) => {
				for bound in &ty.bounds {
					self.visit_type_param_bound(bound);
				}
			}
			syn::Type::Tuple(ty) => {
				for elem in &ty.elems {
					self.visit_type(elem);
				}
			}
			syn::Type::Infer(_) | syn::Type::Never(_) | syn::Type::Verbatim(_) => {}
			_ => {}
		}
	}

	/// Traverse syntax tree.
	fn visit_path_segment(&mut self, segment: &syn::PathSegment) {
		self.visit_path_arguments(&segment.arguments);
	}

	/// Traverse syntax tree.
	fn visit_path_arguments(&mut self, arguments: &syn::PathArguments) {
		match arguments {
			syn::PathArguments::None => {}
			syn::PathArguments::AngleBracketed(arguments) => {
				for arg in &arguments.args {
					match arg {
						syn::GenericArgument::Type(arg) => self.visit_type(arg),
						syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),
						_ => {}
					}
				}
			}
			syn::PathArguments::Parenthesized(arguments) => {
				for argument in &arguments.inputs {
					self.visit_type(argument);
				}
				self.visit_return_type(&arguments.output);
			}
		}
	}

	/// Traverse syntax tree.
	fn visit_return_type(&mut self, return_type: &syn::ReturnType) {
		match return_type {
			syn::ReturnType::Default => {}
			syn::ReturnType::Type(_, output) => self.visit_type(output),
		}
	}

	/// Traverse syntax tree.
	fn visit_type_param_bound(&mut self, bound: &syn::TypeParamBound) {
		if let syn::TypeParamBound::Trait(bound) = bound {
			self.visit_path(&bound.path);
		}
	}

	// Type parameter should not be considered used by a macro path.
	//
	//     struct TypeMacro<T> {
	//         mac: T!(),
	//         marker: PhantomData<T>,
	//     }
	fn visit_macro(&mut self, _mac: &syn::Macro) {}
}

#[cfg(test)]
mod tests {
	use super::*;
	use syn::{parse_quote, Type};

	#[test]
	fn it_works() {
		let t: Type = parse_quote!(Vec<T>);
		let id: Ident = parse_quote!(T);

		let mut exp = HashSet::new();
		exp.insert(id);
		let generics = exp.clone();

		assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
	}

	#[test]
	fn several_type_params() {
		let t: Type = parse_quote!(Vec<(A, B, C)>);

		let mut generics: HashSet<syn::Ident> = HashSet::new();
		let mut exp = HashSet::new();

		generics.insert(parse_quote!(A));
		generics.insert(parse_quote!(B));
		generics.insert(parse_quote!(C));
		generics.insert(parse_quote!(D));

		exp.insert(parse_quote!(A));
		exp.insert(parse_quote!(B));
		exp.insert(parse_quote!(C));

		assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
	}

	#[test]
	fn nested_type() {
		let t: Type = parse_quote!(Vec<Foo<A, B>>);

		let mut generics: HashSet<syn::Ident> = HashSet::new();
		let mut exp = HashSet::new();

		generics.insert(parse_quote!(A));
		generics.insert(parse_quote!(B));
		generics.insert(parse_quote!(C));
		generics.insert(parse_quote!(D));

		exp.insert(parse_quote!(A));
		exp.insert(parse_quote!(B));

		assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
	}
}