telety_impl/
interop.rs

1use proc_macro2::TokenStream;
2
3// TODO Find a better place to put this
4pub struct AliasMapArgs {
5    pub map_path: syn::Path,
6    pub map_path_comma: syn::Token![,],
7    pub vis: syn::Visibility,
8    pub unique_ident: syn::Ident,
9    pub generics: syn::Generics,
10    pub where_clause: Option<syn::WhereClause>,
11    pub unique_ident_comma: syn::Token![,],
12    pub telety_path: Option<syn::Path>,
13    pub telety_path_comma: syn::Token![,],
14    pub self_type: Option<syn::Type>,
15    pub self_type_comma: syn::Token![,],
16    pub aliased_types: syn::punctuated::Punctuated<syn::Type, syn::Token![,]>,
17}
18
19impl AliasMapArgs {
20    pub fn new(
21        map_path: syn::Path,
22        vis: syn::Visibility,
23        unique_ident: syn::Ident,
24        mut generics: syn::Generics,
25        telety_path: Option<syn::Path>,
26        self_type: Option<syn::Type>,
27        aliased_types: Vec<syn::Type>,
28    ) -> Self {
29        let where_clause = generics.where_clause.take();
30
31        Self {
32            map_path,
33            map_path_comma: Default::default(),
34            vis,
35            unique_ident,
36            generics,
37            where_clause,
38            unique_ident_comma: Default::default(),
39            telety_path,
40            telety_path_comma: Default::default(),
41            self_type,
42            self_type_comma: Default::default(),
43            aliased_types: aliased_types.into_iter().collect(),
44        }
45    }
46}
47
48impl quote::ToTokens for AliasMapArgs {
49    fn to_tokens(&self, tokens: &mut TokenStream) {
50        let Self {
51            map_path,
52            map_path_comma,
53            vis,
54            unique_ident,
55            generics,
56            where_clause,
57            unique_ident_comma,
58            telety_path,
59            telety_path_comma,
60            self_type,
61            self_type_comma,
62            aliased_types,
63        } = self;
64
65        map_path.to_tokens(tokens);
66        map_path_comma.to_tokens(tokens);
67        vis.to_tokens(tokens);
68        unique_ident.to_tokens(tokens);
69        generics.to_tokens(tokens);
70        where_clause.to_tokens(tokens);
71        unique_ident_comma.to_tokens(tokens);
72        telety_path.to_tokens(tokens);
73        telety_path_comma.to_tokens(tokens);
74        self_type.to_tokens(tokens);
75        self_type_comma.to_tokens(tokens);
76        aliased_types.to_tokens(tokens);
77    }
78}
79
80impl syn::parse::Parse for AliasMapArgs {
81    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
82        let map_path = input.parse()?;
83        let map_path_comma = input.parse()?;
84        let vis = input.parse()?;
85        let unique_ident = input.parse()?;
86        let generics = input.parse()?;
87        let where_clause = input
88            .peek(syn::Token![where])
89            .then(|| input.parse())
90            .transpose()?;
91        let unique_ident_comma = input.parse()?;
92        let telety_path = (!input.peek(syn::Token![,]))
93            .then(|| input.parse())
94            .transpose()?;
95        let telety_path_comma = input.parse()?;
96        let self_type = (!input.peek(syn::Token![,]))
97            .then(|| input.parse())
98            .transpose()?;
99        let self_type_comma = input.parse()?;
100        let aliased_types = syn::punctuated::Punctuated::parse_terminated(input)?;
101
102        Ok(Self {
103            map_path,
104            map_path_comma,
105            vis,
106            unique_ident,
107            generics,
108            where_clause,
109            unique_ident_comma,
110            telety_path,
111            telety_path_comma,
112            self_type,
113            self_type_comma,
114            aliased_types,
115        })
116    }
117}