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