telety_impl/visitor/
crateify.rs1use proc_macro2::Span;
2use syn::{Ident, PathArguments, PathSegment, UseTree};
3
4use super::calling_crate;
5
6pub struct Crateify(PathSegment);
7
8impl Crateify {
9 pub fn new() -> Self {
10 Self::new_as_crate(calling_crate(Span::call_site()))
11 }
12
13 pub fn new_as_crate(crate_ident: Ident) -> Self {
14 Self(PathSegment {
15 ident: crate_ident,
16 arguments: PathArguments::None,
17 })
18 }
19}
20
21impl Default for Crateify {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl directed_visit::syn::visit::FullMut for Crateify {
28 fn visit_path_mut<D>(visitor: directed_visit::Visitor<'_, D, Self>, node: &mut syn::Path)
29 where
30 D: directed_visit::DirectMut<Self, syn::Path> + ?Sized,
31 {
32 if let Some(first_segment) = node.segments.first_mut()
33 && *first_segment == visitor.0
34 {
35 first_segment.ident = Ident::new("crate", first_segment.ident.span());
36 node.leading_colon = None;
37 }
38
39 directed_visit::Visitor::visit_mut(visitor, node);
40 }
41
42 fn visit_item_use_mut<D>(visitor: directed_visit::Visitor<'_, D, Self>, node: &mut syn::ItemUse)
43 where
44 D: directed_visit::DirectMut<Self, syn::ItemUse> + ?Sized,
45 {
46 if let UseTree::Path(path) = &mut node.tree
47 && path.ident == visitor.0.ident
48 {
49 path.ident = Ident::new("crate", path.ident.span());
50 node.leading_colon = None;
51 }
52
53 directed_visit::Visitor::visit_mut(visitor, node);
54 }
55}