ion_proc/
visitors.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use syn::punctuated::Punctuated;
8use syn::visit_mut::{VisitMut, visit_type_mut, visit_type_path_mut, visit_type_reference_mut};
9use syn::{GenericArgument, PathArguments, Type, TypePath, TypeReference, parse_quote};
10
11use crate::utils::path_ends_with;
12
13pub(crate) struct LifetimeRemover;
14
15impl VisitMut for LifetimeRemover {
16	fn visit_type_path_mut(&mut self, ty: &mut TypePath) {
17		if let Some(segment) = ty.path.segments.last_mut()
18			&& let PathArguments::AngleBracketed(arguments) = &mut segment.arguments
19		{
20			let args = arguments.args.clone().into_iter().filter(|argument| match argument {
21				GenericArgument::Lifetime(lt) => *lt == parse_quote!('static),
22				_ => true,
23			});
24			arguments.args = Punctuated::from_iter(args);
25		}
26		visit_type_path_mut(self, ty);
27	}
28
29	fn visit_type_reference_mut(&mut self, ty: &mut TypeReference) {
30		if ty.lifetime != Some(parse_quote!('static)) {
31			ty.lifetime = None;
32		}
33		visit_type_reference_mut(self, ty);
34	}
35}
36
37pub(crate) struct SelfRenamer<'t> {
38	pub(crate) ty: &'t Type,
39}
40
41impl VisitMut for SelfRenamer<'_> {
42	fn visit_type_mut(&mut self, ty: &mut Type) {
43		if let Type::Path(typ) = ty
44			&& path_ends_with(&typ.path, "Self")
45		{
46			*ty = self.ty.clone();
47		}
48		visit_type_mut(self, ty);
49	}
50}