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
extern crate proc_macro;
extern crate syn;
extern crate quote;

use proc_macro::TokenStream;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{
    parse_macro_input, Data, DeriveInput, Fields, Ident, Meta, Type, Field
};

#[proc_macro_derive(Deserialize, attributes(ignore))]
pub fn aeon_deserialize(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    assert!(
        input.generics.type_params().next().is_none(),
        "Deserialize is not implemented for generic types"
    );

    let from_aeon_func = generate_from_aeon_func(&input.data);
    let from_property_func = generate_from_property_func(&input.data);

    let expanded = quote! {
        impl AeonDeserialize for #name {
            fn from_property(field: aeon::value::AeonValue) -> Self {
                Self {
                    #from_property_func
                }
            }

            fn from_aeon(s: String) -> Self {
                let aeon = aeon::deserialize(s).unwrap();
                Self {
                    #from_aeon_func
                }
            }
        }

    };

    TokenStream::from(expanded)
}

/// These identifiers do not contain the surrounding Vec or HashMap!
enum SerTy {
    Regular(Ident),
    Vec(Ident),
    Map(Ident),
}

fn generate_from_property_func(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let name = &f.ident;
        let ser_ty = get_ser_ty(&f.ty, f.span());
        let func_call = from_property_func_call_from_ser_ty(name.clone().unwrap(), ser_ty);
        quote_spanned! {f.span() =>
            #name : #func_call,
        }
    });
    quote! {
        #(#recurse)*
    }
}

fn from_property_func_call_from_ser_ty(name: Ident, ser_ty: SerTy) -> proc_macro2::TokenStream {
    let prop = format!("{:#}", name);
    match ser_ty { // ordered by complexity
        SerTy::Map(_) => quote! { field.get(#prop).map() },
        SerTy::Regular(id) => quote! { #id::from_property(field.get(#prop)) },
        SerTy::Vec(id) => quote! { field.get(#prop).list().drain(..).map(|field| #id::from_property(field)).collect() },
    }
}

fn generate_from_aeon_func(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let name = &f.ident;
        let ser_ty = get_ser_ty(&f.ty, f.span());
        let func_call = from_aeon_func_call_from_ser_ty(name.clone().unwrap(), ser_ty);
        quote_spanned! {f.span() =>
            #name : #func_call,
        }
    });
    quote! {
        #(#recurse)*
    }
}

fn from_aeon_func_call_from_ser_ty(name: Ident, ser_ty: SerTy) -> proc_macro2::TokenStream {
    let prop = format!("{:#}", name);
    match ser_ty { // ordered by complexity
        SerTy::Map(_) => quote! { aeon.get(#prop).map() },
        SerTy::Regular(id) => quote! { #id::from_property(aeon.get(#prop)) },
        SerTy::Vec(id) => quote! { aeon.get(#prop).list().drain(..).map(|field| #id::from_property(field)).collect() },
    }
}

/// Get type that is being serialized/deserialized for easier/lazier handling
fn get_ser_ty(ty: &Type, s: proc_macro2::Span) -> SerTy {
    let mut ty_name = quote!(#ty).to_string() // may need .as_str()
        .replace("\"", "")
        .replace(" ", "");

    macro_rules! inner_generic_ty {
        ($ty:ident) => (
            {
                // keep inner generic name
                let begin = ty_name.find('<').unwrap();
                ty_name.replace_range(..=begin, "");
                let end = ty_name.rfind('>').unwrap();
                ty_name.replace_range(end.., "");
                SerTy::$ty(Ident::new(&ty_name, s))
            }
        );
    }

    if ty_name.contains("Vec<") {
        inner_generic_ty!(Vec)
    } else if ty_name.contains("HashMap<") {
        inner_generic_ty!(Map)
    } else {
        SerTy::Regular(Ident::new(&ty_name, s))
    }
}


#[proc_macro_derive(Serialize, attributes(ignore))]
pub fn aeon_serialize(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    assert!(
        input.generics.type_params().next().is_none(),
        "Serialize is not implemented for generic types"
    );

    let name = input.ident;
    let name_lower = quote!(#name).to_string().to_lowercase();

    let to_aeon_func = generate_to_aeon_func(&input.data);
    let serialize_aeon_property_func = generate_serialize_aeon_property_func(&input.data);
    let create_macros_func = generate_create_macros_func(&input.data);

    let args_vec = generate_args_vec(&input.data);

    let expanded = quote! {
        impl AeonSerialize for #name {
            fn to_aeon(&self) -> String {
                let mut obj = aeon::object::AeonObject::new();
                #to_aeon_func

                let mut macros = Self::create_macros(false);
                macros.drain().for_each(|(k,v)| obj.add_macro(v));
                aeon::serialize(obj)
            }

            fn serialize_aeon_property(&self) -> aeon::value::AeonValue {
                let mut map = std::collections::HashMap::<String,aeon::value::AeonValue>::new();
                #serialize_aeon_property_func 
                aeon::value::AeonValue::Map(map)
            }

            fn create_macros(insert_self: bool) -> std::collections::HashMap::<String, aeon::object::Macro> {
                let mut macros = std::collections::HashMap::<String,aeon::object::Macro>::new();
                if insert_self {
                    macros.insert(#name_lower.into(),
                        aeon::object::Macro::new(#name_lower.into(), #args_vec));
                }
                #create_macros_func
                macros
            }
        }

    };

    TokenStream::from(expanded)
}

fn generate_to_aeon_func(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let name = &f.ident;
        let ser_ty = get_ser_ty(&f.ty, f.span());
        let func_call = to_aeon_func_call_from_ser_ty(name.clone().unwrap(), ser_ty);
        quote_spanned! {f.span() =>
            #func_call
        }
    });
    quote! {
        #(#recurse)*
    }
}

fn to_aeon_func_call_from_ser_ty(name: Ident, ser_ty: SerTy) -> proc_macro2::TokenStream {
    let prop = format!("{:#}", name).to_lowercase();
    match ser_ty { // ordered by complexity
        // TODO: allow serializing HashMap<String,TNotAeonValue> if the values can be converted to
        // AeonValues
        SerTy::Map(_) => quote! {
            obj.add_property(aeon::object::AeonProperty::new(
                    #prop.into(),
                    aeon::value::AeonValue::Map(self.#name.clone()))); 
        },
        SerTy::Regular(id) => quote! { 
            { 
                let ser = #id::serialize_aeon_property(&self.#name);
                obj.add_property(aeon::object::AeonProperty::new(#prop.into(), ser));
            }
        },
        SerTy::Vec(id) => quote! {
            obj.add_property(
                aeon::object::AeonProperty::new(
                    #prop.into(),
                    aeon::value::AeonValue::List(
                        self.#name.iter()
                        .map(|item| {
                            let ser = #id::serialize_aeon_property(item);
                            ser
                        }).collect()))); 
        },
    }
}

fn generate_create_macros_func(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let ser_ty = get_ser_ty(&f.ty, f.span());
        let func_call = create_macros_func_call_from_ser_ty(ser_ty);
        quote_spanned! {f.span() =>
            #func_call
        }
    });
    quote! {
        #(#recurse)*
    }
}

fn create_macros_func_call_from_ser_ty(ser_ty: SerTy) -> proc_macro2::TokenStream {
    match ser_ty { // ordered by complexity
        SerTy::Map(_) => quote! { }, // no point in making macros for arbitrary hashmaps
        SerTy::Regular(id) => quote! {
            macros.extend(#id::create_macros(true));
        },
        SerTy::Vec(id) => quote! { 
            macros.extend(#id::create_macros(true));
        },
    }
}

fn generate_serialize_aeon_property_func(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let name = &f.ident;
        let ser_ty = get_ser_ty(&f.ty, f.span());
        let func_call = serialize_aeon_property_func_call_from_ser_ty(name.clone().unwrap(), ser_ty);
        quote_spanned! {f.span() =>
            #func_call
        }
    });
    quote! {
        #(#recurse)*
    }
}

fn serialize_aeon_property_func_call_from_ser_ty(name: Ident, ser_ty: SerTy) -> proc_macro2::TokenStream {
    let prop = format!("{:#}", name).to_lowercase();
    match ser_ty { // ordered by complexity
        // TODO: allow serializing HashMap<String,TNotAeonValue> if the values can be converted to
        // AeonValues
        SerTy::Map(_m) => quote! { 
            map.insert(#prop.into(), 
                       aeon::value::AeonValue::Map(self.#name.clone()));
        },
        SerTy::Regular(id) => quote! {
            {
                let ser = #id::serialize_aeon_property(&self.#name);
                map.insert(#prop.into(), ser);
            }
        },
        SerTy::Vec(id) => quote! { 
            map.insert(#prop.into(),
                aeon::value::AeonValue::List(
                    self.#name.iter()
                    .map(|item| {
                        let ser = #id::serialize_aeon_property(item);
                        ser
                    }).collect()));
        },
    }
}

fn generate_args_vec(data: &Data) -> proc_macro2::TokenStream {
    let fields = get_struct_fields(data);
    let recurse = fields.iter().map(|f| {
        let name = f.ident.clone().unwrap();
        let name_lower = quote!(#name).to_string().to_lowercase();
        quote_spanned! {f.span() =>
            #name_lower.into(),
        }
    });
    quote! {
        vec![ #(#recurse)* ]
    }
}

fn get_struct_fields(data: &Data) -> Vec<&Field> {
    match *data {
        Data::Struct(ref data) => match data.fields {
            Fields::Named(ref fields) => {
                fields
                    .named
                    .iter()
                    .filter(|f| !get_name_attribute("ignore".into(), &f.attrs))
                    .collect()
            }
            _ => unimplemented!(),
        },
        _ => unimplemented!(),
    }
}

fn get_name_attribute(name: String, input: &Vec<syn::Attribute>) -> bool {
    for att in input.into_iter() {
        let attr = att.parse_meta().unwrap();
        match attr {
            Meta::Path(p) if p.get_ident().unwrap().to_string() == name => return true,
            _ => return false,
        }
    }
    false
}