1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use convert_case::Casing;
use proc_macro2::TokenStream;
use syn::Ident;

struct Argument {
    name: Ident,
    ty: syn::Type,
    enum_attr: Vec<proc_macro2::Group>,
    to_owned: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ReceiverStyle {
    Move,
    Mut,
    Ref,
}

struct Method {
    name: Ident,
    receiver_style: ReceiverStyle,
    args: Vec<Argument>,
    ret: Option<syn::Type>,
    enum_attr: Vec<proc_macro2::Group>,
    return_attr: Vec<proc_macro2::Group>,
    r#async: bool,
}

impl Method {
    fn variant_name(&self) -> proc_macro2::Ident {
        quote::format_ident!(
            "{}",
            self.name
                .to_string()
                .to_case(convert_case::Case::UpperCamel)
        )
    }
}

impl std::fmt::Debug for Method {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Method")
            .field("name", &self.name.to_string())
            .field("receiver_style", &self.receiver_style)
            .field("args", &self.args)
            .finish()
    }
}

struct InputData {
    /// Source trait or inherent impl name.
    name: Ident,
    methods: Vec<Method>,
    params: Params,
}

mod generate;
mod parse_args;
mod parse_input;
mod util;


struct GenProxyParams {
    level: ReceiverStyle,
    gen_infallible: bool,
    gen_unwrapping: bool,
    gen_unwrapping_and_panicking: bool,
    extra_arg: Option<proc_macro2::TokenStream>,
    name: Ident,
    traitname: Option<Ident>,
    r#async: bool,
}
impl GenProxyParams {
    fn some_impl_requested(&self) -> bool {
        self.gen_infallible || self.gen_unwrapping || self.gen_unwrapping_and_panicking
    }
}

struct CallFnParams {
    level: ReceiverStyle,
    allow_panic: bool,
    extra_arg: Option<proc_macro2::TokenStream>,
    name: Ident,
    r#async: bool,
}


#[derive(PartialEq, Eq, Copy, Clone, Debug)]
enum AccessMode {
    Priv,
    Pub,
    PubCrate,
}


struct Params {
    proxies: Vec<GenProxyParams>,
    call_fns: Vec<CallFnParams>,
    access_mode: AccessMode,
    returnval: Option<proc_macro2::Ident>,
    enum_attr: Vec<proc_macro2::Group>,
    enum_name: Ident,
    inherent_impl_mode : bool,
}

#[proc_macro_attribute]
pub fn enumizer(
    attrs: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let input: TokenStream = item.into();
    let attrs: TokenStream = attrs.into();

    let params = parse_args::parse_args(attrs);

    let mut ret = TokenStream::new();
    let input_data = if ! params.inherent_impl_mode {
        let mut tra: syn::ItemTrait = syn::parse2(input).unwrap();
        let input_data = InputData::parse_trait(&mut tra, params);
        ret.extend(quote::quote! {#tra});
        input_data
    } else {
        let mut imp: syn::ItemImpl = syn::parse2(input).unwrap();
        let input_data = InputData::parse_inherent_impl(&mut imp, params);
        ret.extend(quote::quote! {#imp});
        input_data
    };
    let params = &input_data.params;
   
    //dbg!(thetrait);
    input_data.generate_enum(&mut ret);

    let caller_inconv = input_data.receiver_style_that_is_the_most_inconvenient_for_caller();

    for g in &params.call_fns {
        match g.level {
            ReceiverStyle::Move => (),
            ReceiverStyle::Mut => {
                if caller_inconv == ReceiverStyle::Move && !g.allow_panic {
                    panic!("Cannot generate `call_fn(ref_mut)` function because of trait have `self` methods. Use `call_fn(... ,allow_panic)` to override.");
                }
            }
            ReceiverStyle::Ref => {
                if caller_inconv != ReceiverStyle::Ref && !g.allow_panic {
                    panic!("Cannot generate `call_fn(ref)` function because of trait have non-`&self` methods. Use `call_fn(... ,allow_panic)` to override.");
                }
            }
        }
        input_data.generate_call_fn(&mut ret, g);
    }

    let callee_inconv = input_data.receiver_style_that_is_the_most_inconvenient_for_callee();

    for g in &params.proxies {
        if params.inherent_impl_mode && g.some_impl_requested() {
            panic!("Generating trait impls is incompatible with inherent_impl mode");
        }

        if g.gen_infallible {
            if params.returnval.is_some() {
                panic!("infallible_impl and returnval are incompatible");
            }
        }
        match g.level {
            ReceiverStyle::Move => {
                if g.gen_infallible || g.gen_unwrapping {
                    if callee_inconv != ReceiverStyle::Move {
                        panic!("The trait contains `&self` or `&mut self` methods. The FnOnce proxy cannot implement it - only for traits with solely `self` methods. Use `unwrapping_and_panicking_impl` to force generation and retain only some methods");
                    }
                }
            }
            ReceiverStyle::Mut => {
                if g.gen_infallible || g.gen_unwrapping {
                    if callee_inconv == ReceiverStyle::Ref {
                        panic!("The trait contains &self methods. The FnMut proxy cannot implement it. Use `unwrapping_and_panicking_impl` to force generation and retain only some methods");
                    }
                }
            }
            ReceiverStyle::Ref => {
               
            }
        }

        if g.traitname.is_some() {
            input_data.generate_resultified_trait(&mut ret, g);
        }
        input_data.generate_proxy(&mut ret, g);
        if g.gen_infallible {
            input_data.generate_infallible_impl(&mut ret, g);
        }
        if g.gen_unwrapping || g.gen_unwrapping_and_panicking {
            input_data.generate_unwrapping_impl(&mut ret, g);
        }
    }

    ret.into()
}