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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#![recursion_limit = "128"]

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use syn::{Data, Fields, Ident, Type};

use proc_macro::TokenStream;

struct Analysis {
    filters: Vec<String>,
    num_fields: Vec<Ident>,
    str_fields: Vec<(Ident, bool)>,
    runner: Option<Ident>,
}

impl Analysis {
    fn from(ast: &syn::DeriveInput) -> Analysis {
        let name = ast.ident;
        let mut filters = vec![];
        let mut runner = None;
        for attr in &ast.attrs {
            let attr_name = attr.path.segments[0].ident.to_string();
            let val = attr.tts
                .clone()
                .into_iter()
                .skip(1)
                .next()
                .expect(&format!("Error: Expected #[{} = value]", attr_name))
                .to_string()
                .replace("\"", "");
            match attr_name.as_str() {
                "filter" => filters.push(val.to_string()),
                "runner" => runner = Some(Ident::new(&val, name.span())),
                _ => panic!("unexpected attribute {}", attr_name),
            };
        }
        let mut num_fields = vec![];
        let mut str_fields = vec![];
        match &ast.data {
            Data::Struct(data) => match &data.fields {
                Fields::Named(fields) => {
                    'fieldpush: for field in &fields.named {
                        for attr in &field.attrs {
                            if attr.path.segments[0].ident.to_string() == "zapper_ignore" {
                                continue 'fieldpush;
                            }
                        }
                        let id = field.ident.unwrap();
                        if is_num(&field.ty) {
                            num_fields.push(id);
                        } else {
                            str_fields.push((id, is_str_primitive(&field.ty)));
                        }
                    }
                }
                _ => panic!("must have named fields"),
            },
            _ => panic!("only works on structs"),
        }

        Analysis {
            filters,
            num_fields,
            str_fields,
            runner,
        }
    }
}

#[proc_macro_derive(ZapperEnv, attributes(runner, zapper_ignore))]
pub fn zapper_env_derive(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast = syn::parse(input).unwrap();

    // Build the impl
    let gen = impl_zapper_env(ast);

    // Return the generated impl
    gen.into()
}

fn impl_zapper_env(ast: syn::DeriveInput) -> quote::Tokens {
    let Analysis {
        filters,
        num_fields,
        str_fields,
        runner,
    } = Analysis::from(&ast);

    assert_eq!(filters.len(), 0, "ZapperEnv should not have any filters");

    let name = ast.ident;

    let runner = runner.expect(&format!(
        "You must provide a #[runner = ZapperRunnerStruct] annotation on the \"{}\" struct.",
        name
    ));

    let num_enum = Ident::new(&(runner.to_string() + "Nums"), runner.span());
    let str_enum = Ident::new(&(runner.to_string() + "Strs"), runner.span());
    let filter_enum = Ident::new(&(runner.to_string() + "Filters"), runner.span());

    let num_match = num_fields
        .iter()
        .map(|f| {
            let fs = f.to_string();
            quote! { #fs => Some(self.#f as f64), }
        })
        .collect::<Vec<_>>();

    let str_match = str_fields
        .iter()
        .map(|(f, prim)| {
            let fs = f.to_string();
            if *prim {
                quote! { #fs => ::std::borrow::Cow::from(&*self.#f).into(), }
            } else {
                quote! { #fs => ::std::borrow::Cow::from(self.#f.to_string()).into(), }
            }
        })
        .collect::<Vec<_>>();

    quote!{
        #[allow(bad_style, unused)]
        impl<'a> ::zapper::Environment<'a, #num_enum, #str_enum, #filter_enum> for Provider {
            fn num_constant(&self, name: &str) -> Option<f64> {
                match name {
                    #(#num_match)*
                    _ => None
                }
            }

            fn str_constant(&self, name: &str) -> Option<::std::borrow::Cow<str>> {
                match name {
                    #(#str_match)*
                    _ => None
                }
            }

            fn num_var(name: &str) -> Option<#num_enum> {
                #num_enum::from_str(name)
            }

            fn str_var(name: &str) -> Option<#str_enum> {
                #str_enum::from_str(name)
            }

            fn filter(name: &str) -> Option<(#filter_enum, usize, ::zapper::FilterInput<#str_enum>)> {
                #filter_enum::from_str(name)
            }
        }
    }
}

#[proc_macro_derive(ZapperRunner, attributes(filter, zapper_ignore))]
pub fn zapper_runner_derive(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast = syn::parse(input).unwrap();

    // Build the impl
    let gen = impl_zapper_runner(ast);

    // panic!("{:#?}", gen);

    // Return the generated impl
    gen.into()
}

fn is_num(ty: &Type) -> bool {
    match ty {
        Type::Path(ty_path) => {
            let ty = ty_path.path.segments[0].ident.to_string();
            match ty.as_str() {
                "u8" | "u16" | "u32" | "u64" | "u128" | "i8" | "i16" | "i32" | "i64" | "i128"
                | "f32" | "f64" => true,
                _ => false,
            }
        }
        _ => false,
    }
}

fn is_str_primitive(ty: &Type) -> bool {
    match ty {
        Type::Path(ty_path) => {
            let ty = ty_path.path.segments[0].ident.to_string();
            match ty.as_str() {
                "str" | "String" => true,
                _ => false,
            }
        }
        _ => false,
    }
}

fn impl_zapper_runner(ast: syn::DeriveInput) -> quote::Tokens {
    let Analysis {
        filters,
        num_fields,
        str_fields,
        runner,
    } = Analysis::from(&ast);

    assert!(
        runner.is_none(),
        "ZapperRunner should not have a runner attribute"
    );

    let name = ast.ident;

    let num_enum = Ident::new(&(name.to_string() + "Nums"), name.span());
    let str_enum = Ident::new(&(name.to_string() + "Strs"), name.span());
    let filter_enum = Ident::new(&(name.to_string() + "Filters"), name.span());
    let filter_fields = filters.iter().map(|f| {
        Ident::new(
            &f[..f.find('/')
                   .expect("filters must specify number of args and return type.")],
            name.span(),
        )
    });

    let num_match = num_fields
        .iter()
        .map(|f| quote! { #num_enum::#f => self.#f as f64, })
        .collect::<Vec<_>>();

    let num_from = num_fields
        .iter()
        .map(|f| {
            let fs = f.to_string();
            quote! { #fs => Some(#num_enum::#f), }
        })
        .collect::<Vec<_>>();

    let str_match = str_fields
        .iter()
        .map(|(f, prim)| {
            if *prim {
                quote! { #str_enum::#f => ::std::borrow::Cow::from(&*self.#f).into(), }
            } else {
                quote! { #str_enum::#f => ::std::borrow::Cow::from(self.#f.to_string()).into(), }
            }
        })
        .collect::<Vec<_>>();

    let str_from = str_fields
        .iter()
        .map(|(f, _prim)| {
            let fs = f.to_string();
            quote! { #fs => Some(#str_enum::#f), }
        })
        .collect::<Vec<_>>();

    let mut num_filters = vec![];
    let mut str_filters = vec![];
    let mut custom_filters = vec![];

    let str_fields = str_fields.into_iter().map(|(f, _)| f);

    let filter_from = filters
        .iter()
        .map(|f| {
            let split = f.find('/')
                .expect("filters must specify number of args and return type.");
            let filter = &f[..split];
            let filter_i = Ident::new(&filter, name.span());
            let arg_count = f[split + 1..f.len() - 1]
                .parse::<usize>()
                .expect("argument count for filter must be a usize");
            let filter_type = f.as_bytes()[f.len() - 1] as char;
            match filter_type {
            'n' => {
                num_filters.push(quote! { #filter_enum::#filter_i => #filter_i(self, args, input), });
                quote!( #filter => Some((#filter_enum::#filter_i, #arg_count, ::zapper::FilterInput::Numeric)), )
            }
            's' => {
                str_filters.push(quote! { #filter_enum::#filter_i => #filter_i(self, args, &input, buffer), });                
                quote!( #filter => Some((#filter_enum::#filter_i, #arg_count, ::zapper::FilterInput::Stringified)), )
            }
            'x' => {
                custom_filters.push(quote! { #filter_enum::#filter_i => #filter_i(self, args, input_id, buffer), });                                
                quote!( #filter => Some((#filter_enum::#filter_i, #arg_count, ::zapper::FilterInput::StrEnumId(vec![]))), )
            }
            _ => panic!("no such input type as {}, valid options are n (numeric), s (stringified), x (custom)", filter_type)
        }
        })
        .collect::<Vec<_>>();

    // println!(
    //     "{:#?}",
    quote! {
        #[allow(bad_style)]
        #[derive(Copy, Clone, Debug, PartialEq)]
        enum #num_enum {
            #(#num_fields,)*
        }

        impl #num_enum {
            fn from_str(name: &str) -> Option<#num_enum> {
                match name {
                    #(#num_from)*
                    _ => None
                }
            }
        }

        #[allow(bad_style)]
        #[derive(Copy, Clone, Debug, PartialEq)]
        enum #str_enum {
            #(#str_fields,)*
        }

        impl #str_enum {
            fn from_str(name: &str) -> Option<#str_enum> {
                match name {
                    #(#str_from)*
                    _ => None
                }
            }
        }

        #[allow(bad_style)]
        #[derive(Copy, Clone, Debug, PartialEq)]
        enum #filter_enum {
            #(#filter_fields,)*
        }

        impl #filter_enum {
            fn from_str(name: &str) -> Option<(#filter_enum, usize, ::zapper::FilterInput<#str_enum>)> {
                match name {
                    #(#filter_from)*
                    _ => None
                }
            }
        }

        #[allow(bad_style, unused)]
        impl ::zapper::Runner<#num_enum, #str_enum, #filter_enum> for #name {
            fn num_var(&self, var: #num_enum) -> f64 {
                match var {
                   #(#num_match)*
                }
            }

            fn str_var(&self, var: #str_enum) -> ::std::borrow::Cow<str> {
                match var {
                    #(#str_match)*
                }
            }

            fn filter_num(&self, filter: #filter_enum, args: &[f64], input: f64) -> f64 {
                match filter {
                    #(#num_filters)*
                    _ => unreachable!("bug in zapper! attempted to execute {:?} as a numeric filter erroneously", filter)
                }
            }

            fn filter_str(&self, filter: #filter_enum, args: &[f64], input: ::std::borrow::Cow<str>, buffer: &mut String) {
                match filter {
                    #(#str_filters)*
                    _ => unreachable!("bug in zapper! attempted to execute {:?} as a string filter erroneously", filter)
                }
            }

            fn filter_id(
                &self,
                filter: #filter_enum,
                args: &[f64],
                input_id: #str_enum,
                buffer: &mut String,
            ) {
                match filter {
                    #(#custom_filters)*
                    _ => unreachable!("bug in zapper! attempted to execute {:?} as a custom filter erroneously", filter)
                }
            }
        }
    }
    // );
    // unreachable!();
}