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