telety_impl/alias/
mod.rs

1pub(crate) mod arguments;
2pub(crate) use arguments::Arguments;
3pub mod error;
4pub use error::Error;
5mod exact;
6pub(crate) use exact::Exact;
7mod index;
8pub(crate) use index::Index;
9mod kind;
10pub use kind::Kind;
11mod map;
12pub use map::Map;
13mod module;
14pub use module::Module;
15mod public;
16pub(crate) use public::Public;
17mod path;
18pub(crate) use path::Path;
19
20use quote::quote;
21use syn::parse_quote;
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Debug, Clone)]
26pub struct Alias<'map> {
27    pub(crate) map: &'map Map<'map>,
28    pub(crate) path: &'map Path,
29    pub(crate) index: Index,
30    pub(crate) arguments: Arguments,
31    pub(crate) kind: Kind,
32}
33
34impl<'map> Alias<'map> {
35    pub(crate) fn new(
36        map: &'map Map,
37        path: &'map Path,
38        index: Index,
39        arguments: Arguments,
40        kind: Kind,
41    ) -> Self {
42        Self {
43            map,
44            path,
45            index,
46            arguments,
47            kind,
48        }
49    }
50
51    // The original type path this alias points to, with generic arguments removed
52    pub fn aliased_path(&self) -> &syn::Path {
53        &self.path.truncated_path
54    }
55
56    // Path to the alias with no generic arguments. Does not include `!`.
57    pub fn to_macro_path(&self) -> syn::Path {
58        let path = self.map.map_path();
59        let module = self.map.module().ident();
60        let alias_ident = self.index.ident(self.path.friendly_path());
61
62        parse_quote!(#path::#module::#alias_ident)
63    }
64
65    pub fn to_path(&self) -> syn::Path {
66        let macro_path = self.to_macro_path();
67        // Janky turbofish
68        let arguments = self.arguments.args.as_ref().map(|a| quote!(::#a));
69
70        parse_quote!(#macro_path #arguments)
71    }
72
73    pub fn generic_arguments(&self) -> Option<&syn::AngleBracketedGenericArguments> {
74        self.arguments.args.as_ref()
75    }
76
77    pub fn kind(&self) -> Kind {
78        self.kind
79    }
80
81    pub(crate) fn exact(self) -> Exact<'map> {
82        Exact::new(self)
83    }
84
85    pub(crate) fn public(self) -> Public<'map> {
86        Public::new(self)
87    }
88}