actix-telegram-derive 0.2.2

derive for actix-telegram
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*!
Getset, we're ready to go!

A procedural macro for generating the most basic getters and setters on fields.

Getters are generated as `fn field(&self) -> &type`, while setters are generated as `fn field(&mut self, val: type)`.

These macros are not intended to be used on fields which require custom logic inside of their setters and getters. Just write your own in that case!

```rust
#[macro_use]
extern crate getset;

#[derive(Getters, Setters, New, Default)]
#[get(vis = "pub")]
#[get(vis = "pub", mutable)]
#[set(vis = "pub", consume)]
#[set(vis = "pub")]
#[new(vis = "pub")]
pub struct Foo<T> where T: Copy + Clone + Default {
    /// Doc comments are supported!
    /// Multiline, even.
    #[get(copy)] #[get(mutable)] #[set]
    private: T,

    /// Doc comments are supported!
    /// Multiline, even.
    public: Option<T>,

    #[set(optional)]
    optional: Option<String>,
}

fn main() {
    let mut foo: Foo<i64> = Foo::new(1).consume_set_public(3);
    assert_eq!(foo.private(), 1);
    assert_eq!(*foo.public(), Some(3));
    foo.set_private(3);
    (*foo.private_mut()) += 1;
    assert_eq!(foo.private(), 4);
    foo.set_public(4);
    assert_eq!(*foo.public(), Some(4));
    foo.set_public(None);
    assert_eq!(*foo.public(), None);
    foo.set_optional(Some("test"));
    assert_eq!(foo.optional(), &Some("test".to_string()));
    foo.set_optional(None::<&str>);
    assert_eq!(*foo.optional(), None);
}
```
```compile_fail
#[macro_use]
extern crate getset;
mod submodule {
    #[derive(Getters, Default)]
    #[get = "pub"]
    pub struct Foo {
        #[get]
        private: i32,
        public: i32,
    }
}
fn main() {
    let mut foo = submodule::Foo::default();
    assert_eq!(*foo.private(), 2);
}
```
*/
#![recursion_limit="128"]

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

mod generate;
mod parse;
mod types;

use crate::types::{GenMode, GenParams};
use proc_macro::TokenStream;
use syn::*;
use quote::ToTokens;
use proc_macro2::Ident;

#[proc_macro_derive(Getters, attributes(get))]
pub fn getters(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast = parse_macro_input!(input as DeriveInput);
    let params = GenParams {
        attribute_name: "get",
        fn_name_prefix: None,
        fn_name_suffix: None,
        global_attr: parse::global_attr(&ast.attrs, "get"),
    };

    // Build the impl
    let gen = produce(&ast, &GenMode::Get, &params);

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

#[proc_macro_derive(MutGetters, attributes(get_mut))]
pub fn mut_getters(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for getters");
    let params = GenParams {
        attribute_name: "get_mut",
        fn_name_prefix: None,
        fn_name_suffix: Some("_mut"),
        global_attr: parse::global_attr(&ast.attrs, "get_mut"),
    };

    // Build the impl
    let gen = produce(&ast, &GenMode::GetMut, &params);
    // Return the generated impl
    gen.into()
}

#[proc_macro_derive(Setters, attributes(set))]
pub fn setters(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for setters");
    let params = GenParams {
        attribute_name: "set",
        fn_name_prefix: Some("set_"),
        fn_name_suffix: None,
        global_attr: parse::global_attr(&ast.attrs, "set"),
    };

    // Build the impl
    let gen = produce(&ast, &GenMode::Set, &params);

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

#[proc_macro_derive(New, attributes(new))]
pub fn new(input: TokenStream) -> TokenStream {
    // Parse the string representation
    let ast: DeriveInput = syn::parse2(input.into()).expect("Couldn't parse for setters");

    // Build the impl
    let gen = produce_new(&ast);

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

fn produce_new(ast: &DeriveInput) -> proc_macro2::TokenStream {
    let name = &ast.ident;
    let generics = &ast.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    // Is it a struct?
    if let syn::Data::Struct(DataStruct { ref fields, .. }) = ast.data {
        let generated = fields
            .iter()
            .map(|f| generate::implement_new(f))
            .collect::<Vec<_>>();

        let initialize = generated.iter().map(|(a, _)| a).collect::<Vec<_>>();
        let struct_initialize = generated.iter().map(|(_, a)| a).collect::<Vec<_>>();
        let global_attr = parse::global_attr(&ast.attrs, "new");
        let attr = global_attr.first().expect("new attribute").clone();
        let params = GenParams {
            attribute_name: "new",
            fn_name_prefix: None,
            fn_name_suffix: None,
            global_attr: global_attr,
        };

        let attributes = parse::meta(&attr, &params);
        let visibility: Option<Visibility> = attributes
            .vis
            .map(|vis| syn::parse_str(vis.as_ref()).expect("visibility"));
        quote! {
            impl #impl_generics #name #ty_generics #where_clause {
                #visibility fn new(#(#initialize)*) -> Self {
                    Self {
                        #(#struct_initialize)*
                    }
                }
            }
        }
    } else {
        // Nope. This is an Enum. We cannot handle these!
        panic!("#[derive(New)] is only defined for structs, not for enums!");
    }
}

fn produce(ast: &DeriveInput, mode: &GenMode, params: &GenParams) -> proc_macro2::TokenStream {
    let name = &ast.ident;
    let generics = &ast.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    // Is it a struct?
    if let syn::Data::Struct(DataStruct { ref fields, .. }) = ast.data {
        let generated = fields
            .iter()
            .map(|f| generate::implement(f, mode, params))
            .collect::<Vec<_>>();

        quote! {
            impl #impl_generics #name #ty_generics #where_clause {
                #(#generated)*
            }
        }
    } else {
        // Nope. This is an Enum. We cannot handle these!
        panic!("#[derive(Getters)] is only defined for structs, not for enums!");
    }
}

#[proc_macro_derive(TelegramApi, attributes(return_type))]
pub fn telegram_api_macro_derive(input: TokenStream) -> TokenStream {
    let input: DeriveInput = syn::parse2(input.into()).unwrap();
    let struct_name = input.ident;
    let mut method_name = struct_name.to_string();
    let lowercase = &method_name[0..1].to_lowercase();
    method_name.replace_range(0..1, lowercase);
    let attributes = input.attrs;
    let return_type = return_type(attributes);

    let file_fields: Option<Box<ToTokens>> = match struct_name.to_string().as_str() {
        "SetWebhook" => Some(Box::new(quote! {
            match msg.certificate {
                Some(InputFile::Memory { name, source, len }) => {
                    form.add_reader2("certificate", source, Some(name.as_str()), None, len);
                },
                Some(InputFile::Disk { path }) => {
                    let path: &Path = path.as_ref();
                    let field_name = path.file_name().unwrap().to_str().unwrap();
                    form.add_file("certificate", &path).unwrap();
                },
                None => (),
            }
        })),
        _ => match input.data {
            Data::Struct(data_struct) => match data_struct.fields {
                Fields::Named(fields) => file_fields(fields),
                Fields::Unit => None,
                _ => unreachable!(),
            },
            _ => unreachable!(),
        },
    };

    let request = match file_fields {
        Some(fields) => {
            quote! {
                let mut form = Form::default();
                let value = serde_json::to_value(&msg).unwrap();
                let object = value.as_object().unwrap();

                // add properties
                for (key, val) in object {
                    let val = match val {
                        Value::String(val) => val.to_string(),
                        etc => etc.to_string(),
                    };

                    form.add_text(key, val);
                }
                #fields;
                Self::send_multipart_request(&self.token, #method_name, self.timeout, form)
            }
        }
        None => {
            quote! {
                Self::send_request(&self.token, #method_name, self.timeout, &msg)
            }
        }
    };

    let expanded = quote! {
        use actix::{Context, Handler, Message as ActixMessage};
        use actors::TelegramApi;
        use futures::Future;
        use failure::Error;

        #[allow(unused_imports)]
        use multipart_rfc7578::Form;
        #[allow(unused_imports)]
        use serde_json::{self, Value};
        #[allow(unused_imports)]
        use std::path::Path;

        impl ActixMessage for #struct_name {
            type Result = Result<#return_type, Error>;
        }

        impl Handler<#struct_name> for TelegramApi {
            type Result = Box<Future<Item = #return_type, Error = Error>>;

            fn handle(&mut self, msg: #struct_name, _ctx: &mut Context<Self>) -> Self::Result {
                #request
            }
        }
    };

    expanded.into()
}

fn return_type(attributes: Vec<Attribute>) -> Type {
    let mut return_type =
        attributes
            .into_iter()
            .filter_map(|attribute| match attribute.interpret_meta() {
                Some(Meta::NameValue(meta)) => {
                    if meta.ident == "return_type" {
                        match meta.lit {
                            syn::Lit::Str(a) => Some(a.parse::<syn::Type>().unwrap()),
                            _ => unreachable!(),
                        }
                    } else {
                        None
                    }
                }
                _ => None,
            });
    return_type.next().unwrap()
}

fn file_fields(fields: FieldsNamed) -> Option<Box<ToTokens>> {
    let fields: Vec<_> = fields
        .named
        .into_iter()
        .filter_map(|field| {
            let field_name = field.ident.unwrap();
            match field.ty {
                Type::Path(ty) => file_field(ty).map(|(field_type, is_optional)| FileField {
                    field_type,
                    is_optional,
                    field_name,
                }),
                _ => unreachable!(),
            }
        }).collect();
    if fields.is_empty() {
        None
    } else {
        Some(Box::new(FileFields(fields)))
    }
}

fn file_field(ty: TypePath) -> Option<(Ident, bool)> {
    let mut is_optional = false;
    for segment in ty.path.segments.into_iter() {
        if segment.ident == "Option" {
            is_optional = true
        };

        if let PathArguments::AngleBracketed(args) = segment.arguments {
            for arg in args.args.into_iter() {
                if let GenericArgument::Type(Type::Path(ty)) = arg {
                    if let Some((ident, _)) = file_field(ty) {
                        return Some((ident, is_optional))
                    }
                }
            }
        }

        let segment_string = segment.ident.to_string();
        if segment_string.starts_with("InputFile") || segment_string.starts_with("InputMedia") {
            return Some((segment.ident, is_optional));
        }
    }
    None
}

#[derive(Debug)]
struct FileField {
    field_type: Ident,
    field_name: Ident,
    is_optional: bool,
}

#[derive(Debug)]
struct FileFields(Vec<FileField>);

impl ToTokens for FileFields {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        for field in self.0.iter() {
            let field_name = &field.field_name;
            let quote = match field.field_type.to_string().as_str() {
                "InputFile" => {
                    let expanded = expand_input_file_field(field.is_optional);
                    quote! {
                        match msg.#field_name {
                            #expanded
                        }
                    }
                }
                "InputFileOrString" => {
                    let expanded = expand_input_file_or_string_field(field.is_optional);
                    quote! {
                        match msg.#field_name {
                            #expanded
                        }
                    }
                }
                "InputMedia" => {
                    let expanded = expand_input_media_field(field.is_optional);
                    quote! {
                        match msg.#field_name {
                            #expanded
                        }
                    }
                }
                _ => {
                    let expanded = expand_input_media_photo_or_video_field(field.is_optional);
                    quote! {
                        for media in msg.#field_name {
                            match media {
                                #expanded
                            }
                        }
                    }
                }
            };
            quote.to_tokens(tokens);
        }
    }
}

fn expand_input_media_photo_or_video_field(_is_optional: bool) -> proc_macro2::TokenStream {
    let match_media = expand_input_file_or_string_field(false);
    let match_thumb = expand_input_file_or_string_field(true);
    quote! {
        InputMediaPhotoOrInputMediaVideo::InputMediaPhoto(input_media) => {
            match input_media.media {
                #match_media
            }
        },
        InputMediaPhotoOrInputMediaVideo::InputMediaVideo(input_media) => {
            match input_media.media {
                #match_media
            }
            match input_media.thumb {
                #match_thumb
            }
        },
    }
}

fn expand_input_media_field(_is_optional: bool) -> proc_macro2::TokenStream {
    let match_media = expand_input_file_or_string_field(false);
    let match_thumb = expand_input_file_or_string_field(true);
    quote! {
        InputMedia::InputMediaAnimation(input_media) => {
            match input_media.media {
                #match_media
            }
            match input_media.thumb {
                #match_thumb
            }
        },
        InputMedia::InputMediaDocument(input_media) => {
            match input_media.media {
                #match_media
            }
            match input_media.thumb {
                #match_thumb
            }
        },
        InputMedia::InputMediaPhoto(input_media) => {
            match input_media.media {
                #match_media
            }
        },
        InputMedia::InputMediaVideo(input_media) => {
            match input_media.media {
                #match_media
            }
            match input_media.thumb {
                #match_thumb
            }
        },
        InputMedia::InputMediaAudio(input_media) => {
            match input_media.media {
                #match_media
            }
            match input_media.thumb {
                #match_thumb
            }
        }
    }
}

fn expand_input_file_or_string_field(is_optional: bool) -> proc_macro2::TokenStream {
    let expanded = expand_input_file_field(false);
    if is_optional {
        quote! {
            Some(InputFileOrString::InputFile(input_file)) => {
                match input_file {
                    #expanded
                }
            },
            Some(InputFileOrString::String(_)) => {
                ()
            },
            None => (),
        }
    } else {
        quote! {
            InputFileOrString::InputFile(input_file) => {
                match input_file {
                    #expanded
                }
            },
            InputFileOrString::String(_) => {
                ()
            },
        }
    }
}

fn expand_input_file_field(is_optional: bool) -> proc_macro2::TokenStream {
    if is_optional {
        quote! {
            Some(InputFile::Memory { name, source, len }) => {
                form.add_reader2(&name, source, Some(name.as_str()), None, len);
            },
            Some(InputFile::Disk { path }) => {
                let path: &Path = path.as_ref();
                let field_name = path.file_name().unwrap().to_str().unwrap();
                form.add_file(field_name, &path).unwrap();
            },
            None => (),
        }
    } else {
        quote! {
            InputFile::Memory { name, source, len } => {
                form.add_reader2(&name, source, Some(name.as_str()), None, len);
            },
            InputFile::Disk { path } => {
                let path: &Path = path.as_ref();
                let field_name = path.file_name().unwrap().to_str().unwrap();
                form.add_file(field_name, &path).unwrap();
            }
        }
    }
}