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
#![feature(proc_macro, proc_macro_lib)]
#![recursion_limit="150"]

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

use proc_macro::TokenStream;
use std::collections::BTreeMap;

#[proc_macro_derive(TelegramFunction)]
pub fn derive_telegram_function(input: TokenStream) -> TokenStream {
    let ast = syn::parse_macro_input(&input.to_string()).unwrap();
    let expanded = expand(ast);
    expanded.to_string().parse().unwrap()
}

fn expand(ast: syn::MacroInput) -> quote::Tokens {
    let config = config_from(&ast.attrs);

    let function = config.get("call").unwrap();
    let function = syn::Lit::Str((*function).clone(), syn::StrStyle::Cooked);
    let bot_function = syn::Ident::from(config.get("function").unwrap().as_str());
    let answer = syn::Ident::from(config.get("answer").unwrap().as_str());
    let file_kind = config.get("file_kind").map(|tmp| syn::Ident::from(tmp.as_str()));

    let fields: Vec<_> = match ast.body {
        syn::Body::Struct(syn::VariantData::Struct(ref fields)) => {
            fields.iter().map(|f| (f.ident.as_ref().unwrap(), &f.ty)).collect()
        },
        syn::Body::Struct(syn::VariantData::Unit) => {
            vec![]
        },
        _ => panic!("#[derive(getters)] can only be used with braced structs"),
    };

    
    /*for field in &fields {
        println!("{:?}", field.1);
    }*/
    
    let name = &ast.ident;
    let is_option_ident = |ref f: &(&syn::Ident, &syn::Ty)| -> bool {
        match *f.1 {
            syn::Ty::Path(_, ref path) => {
                match path.segments.first().unwrap().ident.as_ref() {
                    "Option" => true,
                    _ => false
                }
            },
            _ => false
        }
    };

    let field_compulsory: Vec<_> = fields.iter().filter(|f| !is_option_ident(&f))
        .map(|f| syn::Ident::from(format!("_{}", f.0.as_ref()))).collect();
    
    let field_optional: Vec<_> = fields.iter().filter(|f| is_option_ident(&f)).map(|f| f.0).collect();
    let field_optional2 = field_optional.clone();

    let field_compulsory2: Vec<_> = fields.iter().map(|f| f.0).collect();
    let field_compulsory3 = field_compulsory.clone();
    let values: Vec<_> = fields.iter().map(|f| {
        match *f.1 {
            syn::Ty::Path(_, ref path) => {
                match path.segments.first().unwrap().ident.as_ref() {
                    "Option" => return syn::Ident::from("None"),
                    _ => return syn::Ident::from(format!("_{}", f.0.as_ref()))
                }
            },
            _ => return syn::Ident::from("None")
        }
    }).collect();
    
    let ty_compulsory: Vec<_> = fields.iter().map(|f| f.1).collect();
    let ty_compulsory2 = ty_compulsory.clone();
    let ty_optional: Vec<_> = fields.iter().filter(|f| is_option_ident(&f)).map(|f| {
        if let syn::Ty::Path(_, ref path) = *f.1 {
            if let syn::PathParameters::AngleBracketed(ref param) = path.segments.first().unwrap().parameters {
                if let &syn::Ty::Path(_, ref path) = param.types.first().unwrap() {
                    return (*path).clone();
                }
            }
        }

        panic!("no sane type!");
    }).collect();

    //println!("{:?}", ty_optional.first());

    let trait_name = syn::Ident::from(format!("Function{}",  name.as_ref()));
    let wrapper_name = syn::Ident::from(format!("Wrapper{}", name.as_ref()));
    let bot_function_name = syn::Lit::Str(format!("{}", bot_function), syn::StrStyle::Cooked);

    let tokens = quote! {
        #[allow(dead_code)]
        pub struct #wrapper_name {
            bot: Rc<Bot>,
            inner: #name,
            file: Option<file::File>
        }

        pub trait #trait_name {
             fn #bot_function(&self, #( #field_compulsory: #ty_compulsory, )*) -> #wrapper_name;
        }

        impl #trait_name for RcBot {
            fn #bot_function(&self, #( #field_compulsory3: #ty_compulsory2, )*) -> #wrapper_name {
                #wrapper_name { inner: #name { #( #field_compulsory2: #values, )* }, bot: self.inner.clone(), file: None }
            }
        }
    };

    if let Some(file_kind) = file_kind {
        quote! {
            #tokens
        
            impl #wrapper_name {
                pub fn send<'a>(self) -> impl Future<Item=(RcBot, objects::#answer), Error=Error> + 'a {
                    use futures::future::result;
                   
                    let cloned_bot = self.bot.clone();

                    result::<#wrapper_name, (#wrapper_name, Error)>(Ok(self))
                        .and_then(|tmp| {
                            if tmp.file.is_some() {
                                return Ok(tmp);
                            } else {
                                return Err((tmp, Error::Unknown));
                            }
                        })
                        .and_then(|mut tmp| {
                            let msg = serde_json::to_value(&tmp.inner);
                            let file = tmp.file.take().unwrap();
                            tmp.bot.fetch_formdata(#function, msg, file.source, #bot_function_name, &file.name).map_err(|err| (tmp, err))
                        })
                        .or_else(move |(tmp,_)| {
                            let msg = serde_json::to_string(&tmp.inner).unwrap();
                            tmp.bot.fetch_json(#function, &msg)
                        })
                        .map(move |answer| (RcBot { inner: cloned_bot }, serde_json::from_str::<objects::#answer>(&answer).unwrap()))
                }
               
                #(
                    pub fn #field_optional<S>(mut self, val: S) -> Self where S: Into<#ty_optional> {
                        self.inner.#field_optional2 = Some(val.into());
        
                        self
                    }
                )*
        
                pub fn url<S>(mut self, val: S) -> Self where S: Into<String> {
                    self.inner.#file_kind = Some(val.into());
                
                    self
                }
        
                pub fn file_id<S>(mut self, val: S) -> Self where S: Into<String> {
                    self.inner.#file_kind = Some(val.into());
        
                    self
                }
        
                pub fn file<S>(mut self, val: S) -> Self where S: Into<file::File> {
                    self.file = Some(val.into());
        
                    self
                }
            }
        }
    } else {
        quote! {
            #tokens

            impl #wrapper_name {
                pub fn send<'a>(self) -> impl Future<Item=(RcBot, objects::#answer), Error=Error> + 'a {
                    let msg = serde_json::to_string(&self.inner).unwrap();
                    self.bot.fetch_json(#function, &msg)
                        .map(move |x| (RcBot { inner: self.bot.clone() }, serde_json::from_str::<objects::#answer>(&x).unwrap()))
                }
               
                #(
                    pub fn #field_optional<S>(mut self, val: S) -> Self where S: Into<#ty_optional> {
                        self.inner.#field_optional2 = Some(val.into());
        
                        self
                    }
                )*
            }
        }
    }
}

fn config_from(attrs: &[syn::Attribute]) -> BTreeMap<String, String> {
    let mut result = BTreeMap::new();
    for attr in attrs {
        if let syn::MetaItem::NameValue(ref name, ref value) = attr.value {
            let name = format!("{}", name);
            let value = match value.clone() {
                syn::Lit::Str(value, _) => value,
                _ => panic!("bla")
            };
            result.insert(name, value);
        }
    }
    result
}