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
/*!
This is a procedural macro library to enable simple xml serialization by annotating structs with attribute macros.
```rust
extern crate simple_xml_serialize;
extern crate simple_xml_serialize_macro;
use simple_xml_serialize::XMLElement;
use simple_xml_serialize_macro::xml_element;


#[xml_element("custom_name_here")]
struct MyPoint {
    #[sxs_type_attr(rename="lat")]
    latitude: f32,
    #[sxs_type_attr]
    lon: f32,
    #[sxs_type_attr]
    active: bool,
    #[sxs_type_element(rename="Identifier")]
    name: MyName,
}

#[xml_element("Name")]
struct MyName {
    #[sxs_type_text]
    val: String,
}

fn main() {
    let my_point = MyPoint {
        latitude: 43.38,
        lon: 60.11,
        active: true,
        name: MyName{val: "p1".to_string()},
    };
    let my_point_xml = XMLElement::from(my_point); // can also take refs `&my_point`
    let expected = r#"<custom_name_here lat="43.38" lon="60.11" active="true"><Identifier>p1</Identifier></custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string());

    let expected = r#"<custom_name_here lat="43.38" lon="60.11" active="true">
  <Identifier>
    p1
  </Identifier>
</custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string_pretty("\n","  "));
}
```

There is also a feature `process_options` to allow all the same code to work behind `Option` types. This feature is behind
a feature gate since generating the code is a bit tricky and I suspect it may be too easy to break. Enable it by adding
`features = ["process_options"]` in your `Cargo.toml`.

```rust,ignore
use simple_xml_serialize::XMLElement;
use simple_xml_serialize_macro::xml_element;

#[xml_element("Employee")]
struct Person1 {
    #[sxs_type_attr(rename="Name")]
    name: String,
    #[sxs_type_attr]
    age: Option<u8>,
}

let person1 = Person1{name: "Robert".to_string(), age: None};
let expected = r#"<Employee Name="Robert"/>"#;
assert_eq!(XMLElement::from(&person1).to_string(), expected);


let person1 = Person1{name: "Robert".to_string(), age: Some(52)};
let expected = r#"<Employee Name="Robert" age="52"/>"#;
assert_eq!(XMLElement::from(&person1).to_string(), expected);
```
*/

extern crate proc_macro;
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use quote::quote;
use quote::TokenStreamExt;
// ref
// https://tinkering.xyz/introduction-to-proc-macros/
// https://github.com/tylerreisinger/cache-macro/blob/master/src/lib.rs
// https://doc.rust-lang.org/1.26.2/unstable-book/language-features/proc-macro.html
// https://stackoverflow.com/questions/46002861/how-do-i-generate-quotetokens-from-both-a-constant-value-and-a-collection-of
// https://docs.rs/quote/0.6.11/quote/macro.quote.html
// https://docs.rs/quote/0.6.11/quote/trait.ToTokens.html
// https://docs.rs/syn/0.14/syn/struct.Attribute.html
// https://stackoverflow.com/questions/42484062/how-do-i-process-enum-struct-field-attributes-in-a-procedural-macro/42526546
// https://stackoverflow.com/questions/49506485/how-to-provide-attributes-for-fields-for-struct-annotated-with-an-attribute-itse

#[proc_macro_attribute]
pub fn xml_element(attr: TokenStream, input: TokenStream) -> TokenStream {
    let item: syn::Item = syn::parse(input).expect("failed to parse input");
    
    //clone our item so we can check and alter its attributes
    let mut original_clone = item.clone();

    // assert that we need to have a name argument for the new XMLElement
    let args = attr.to_string();
    assert!(args.starts_with("\""), "`#[xml_element]` requires an argument of the form `#[xml_element(\"xml_element_name_here\")]`");

    // trim down to just the value
    let element_name = args.trim_matches(&['=', ' ', '"'][..]);

    // match item and only continue if it is a struct type
    match item {
        syn::Item::Struct(ref struct_item) => {
            return gen_impl_code(&element_name, &mut original_clone, struct_item);
        },
        _ => {
            assert!(false, "#[xml_element] may only be applied to structs");
        },
    }

    unreachable!();
}

/// function with hardcoded values to remove from the vec of struct field attributes
fn remove_our_attrs_from_item_fields(original_struct: syn::Item) -> syn::Item {
    let our_attrs = ["sxs_type_attr", "sxs_type_element", "sxs_type_text", "sxs_type_multi_element", "sxs_type_raw",];

    let mut original_struct_clone = original_struct.clone();

    for a in our_attrs.iter() {
        original_struct_clone = remove_attr_from_item(original_struct_clone, a);
    }
    original_struct_clone
}

/// dig into the fields attributes and remove the attributes we added to avoid 
/// compilation errors after code generation is done
fn remove_attr_from_item(original_struct: syn::Item, to_remove: &str) -> syn::Item {
    if let syn::Item::Struct(mut struct_item) = original_struct {
        if let syn::Fields::Named(ref mut fields) = struct_item.fields {
            for field in fields.named.iter_mut() {
                let index = field.attrs.iter().position(|a| {
                    match a.interpret_meta() {
                        Some(w) => {
                            match w {
                                syn::Meta::Word(i) => &i.to_string() == to_remove,
                                syn::Meta::List(ml) => &ml.ident.to_string() == to_remove,
                                _ => false,
                            }
                        },
                        _ => false,
                    }
                });
                if let Some(found_index) = index {
                    field.attrs.remove(found_index);
                }
            }
        }
        // this has to go here since our destructuring above moves the value
        return struct_item.into(); 
    }
    original_struct
}

// new_element_name is what our xml element will ultimately be called
// original_struct is the struct this macro was applied to, since that has to exist in the final code
// ast is the breakdown of the struct stuff by syn that we need to examine for the code generation
fn gen_impl_code(new_element_name: &str, original_struct: &mut syn::Item, ast: &syn::ItemStruct) -> TokenStream {
    let struct_ident = &ast.ident;

    // get the ident and name of the fields our attribute were applied to
    let attr_field_idents           = get_field_idents_of_attr_type(&ast.fields, "sxs_type_attr");
    let element_field_idents        = get_field_idents_of_attr_type(&ast.fields, "sxs_type_element");
    let multi_element_field_idents  = get_field_idents_of_attr_type(&ast.fields, "sxs_type_multi_element");
    let text_field_idents           = get_field_idents_of_attr_type(&ast.fields, "sxs_type_text");
    let raw_field_idents            = get_field_idents_of_attr_type(&ast.fields, "sxs_type_raw");

    // since get_field_idents_of_attr_type returns a vec of tuple and we can't use that correctly in quote!
    // the following is just breaking up the tuples into separate vecs
    
    // generate the code for the From trait impl
    let from_impl = quote! {
        impl From<#struct_ident> for XMLElement {
            fn from(si: #struct_ident) -> Self {
                XMLElement::from(&si)
            }
        }
    };

    let add_attrs_code = gen_xml_attr_code(attr_field_idents);
    let add_elements_code = gen_xml_element_code(element_field_idents);
    let add_multi_elements_code = gen_xml_multi_element_code(multi_element_field_idents);
    let add_text_code = gen_xml_text_code(text_field_idents);
    let add_raw_code = gen_xml_raw_code(raw_field_idents);

    // build out our From using #()* for repetition
    let from_ref_impl = quote! {
        impl From<&#struct_ident> for XMLElement {
            fn from(si: &#struct_ident) -> Self {
                let mut new_ele = XMLElement::new(#new_element_name);
                
                #add_attrs_code

                #add_elements_code

                #add_multi_elements_code

                #add_raw_code

                #add_text_code

                new_ele
            }
        }
    };
    // remove our attrs so it doesn't screw up the generated code
    let original_struct_with_our_attrs_removed = remove_our_attrs_from_item_fields(original_struct.clone());

    // build up our final generate code and return it
    let gen = quote! {
        #original_struct_with_our_attrs_removed

        #from_ref_impl

        #from_impl
    };
    gen.into()
}

fn gen_xml_attr_code(attr_field_idents: Vec<(syn::Ident, String, bool, bool)>) -> quote::__rt::TokenStream {
    let attr_field_names: Vec<String>     = attr_field_idents.iter().map(|(_,b,_,_)|b.clone()).collect();
    let attr_idents:      Vec<syn::Ident> = attr_field_idents.iter().map(|(a,_,_,_)|a.clone()).collect();
    let attr_is_options:  Vec<bool>       = attr_field_idents.iter().map(|(_,_,_,d)|d.clone()).collect();
    
    let mut add_attrs_code = quote!();

    for i in 0..attr_is_options.len() {
        let attr_is_option = attr_is_options.get(i).unwrap();
        let attr_name = attr_field_names.get(i).unwrap();
        let attr_ident = attr_idents.get(i).unwrap();

        let attr_code = match attr_is_option {
            false => {
                quote! { new_ele.add_attr(#attr_name, &si.#attr_ident); }
            },
            true => {
                quote! {
                    if let Some(a) = &si.#attr_ident {
                        new_ele.add_attr(#attr_name, &a);
                    }
                }
            },
        };
        add_attrs_code.append_all(attr_code);
    }
    add_attrs_code
}

fn gen_xml_text_code(text_field_idents: Vec<(syn::Ident, String, bool, bool)>) -> quote::__rt::TokenStream {
    let text_idents:        Vec<syn::Ident> = text_field_idents.iter().map(|(a,_,_,_)|a.clone()).collect();
    let text_is_options:    Vec<bool>       = text_field_idents.iter().map(|(_,_,_,d)|d.clone()).collect();
    
    let mut add_texts_code = quote!();

    for i in 0..text_is_options.len() {
        let text_is_option = text_is_options.get(i).unwrap();
        let text_ident = text_idents.get(i).unwrap();

        let text_code = match text_is_option {
            false => {
                quote! { new_ele.set_text(&si.#text_ident); }
            },
            true => {
                quote! {
                    if let Some(a) = &si.#text_ident {
                        new_ele.set_text(&a);
                    }
                }
            },
        };
        add_texts_code.append_all(text_code);
    }
    add_texts_code
}

fn gen_xml_raw_code(raw_field_idents: Vec<(syn::Ident, String, bool, bool)>) -> quote::__rt::TokenStream {
    let raw_idents:        Vec<syn::Ident> = raw_field_idents.iter().map(|(a,_,_,_)|a.clone()).collect();
    let raw_is_options:    Vec<bool>       = raw_field_idents.iter().map(|(_,_,_,d)|d.clone()).collect();
    
    let mut add_raws_code = quote!();

    for i in 0..raw_is_options.len() {
        let raw_is_option = raw_is_options.get(i).unwrap();
        let raw_ident = raw_idents.get(i).unwrap();

        let raw_code = match raw_is_option {
            false => {
                quote! { new_ele.set_raw(&si.#raw_ident); }
            },
            true => {
                quote! {
                    if let Some(a) = &si.#raw_ident {
                        new_ele.set_raw(&a);
                    }
                }
            },
        };
        add_raws_code.append_all(raw_code);
    }
    add_raws_code
}


fn gen_xml_element_code(element_field_idents: Vec<(syn::Ident, String, bool, bool)>) -> quote::__rt::TokenStream {
    let element_names:          Vec<String>     = element_field_idents.iter().map(|(_,b,_,_)|b.clone()).collect();
    let element_renamed:        Vec<bool>       = element_field_idents.iter().map(|(_,_,c,_)|c.clone()).collect();
    let element_idents:         Vec<syn::Ident> = element_field_idents.iter().map(|(a,_,_,_)|a.clone()).collect();
    let element_is_options:     Vec<bool>       = element_field_idents.iter().map(|(_,_,_,d)|d.clone()).collect();
    
    let mut add_elements_code = quote!();

    for i in 0..element_is_options.len() {
        let element_is_option = element_is_options.get(i).unwrap();
        let element_name = element_names.get(i).unwrap();
        let element_was_renamed = element_renamed.get(i).unwrap();
        let element_ident = element_idents.get(i).unwrap();

        let element_code = match element_is_option {
            false => match element_was_renamed {
                false => quote! { new_ele.add_element(&si.#element_ident); },
                true => quote! { new_ele.add_element(XMLElement::from(&si.#element_ident).name(#element_name)); },
            },
            true => match element_was_renamed {
                false => quote! {
                    if let Some(a) = &si.#element_ident {
                        new_ele.add_element(a);
                    }
                },
                true => quote! { 
                    if let Some(a) = &si.#element_ident {
                        new_ele.add_element(XMLElement::from(a).name(#element_name));
                    }
                },
            }, 
        };
        add_elements_code.append_all(element_code);
    }
    add_elements_code
}

fn gen_xml_multi_element_code(multi_element_field_idents: Vec<(syn::Ident, String, bool, bool)>) -> quote::__rt::TokenStream {
    let multi_element_names:        Vec<String>     = multi_element_field_idents.iter().map(|(_,b,_,_)|b.clone()).collect();
    let multi_element_renamed:      Vec<bool>       = multi_element_field_idents.iter().map(|(_,_,c,_)|c.clone()).collect();
    let multi_element_idents:       Vec<syn::Ident> = multi_element_field_idents.iter().map(|(a,_,_,_)|a.clone()).collect();
    let multi_element_is_options:   Vec<bool>       = multi_element_field_idents.iter().map(|(_,_,_,d)|d.clone()).collect();
    
    let mut add_multi_elements_code = quote!();

    for i in 0..multi_element_is_options.len() {
        let multi_element_is_option = multi_element_is_options.get(i).unwrap();
        let multi_element_name = multi_element_names.get(i).unwrap();
        let multi_element_was_renamed = multi_element_renamed.get(i).unwrap();
        let multi_element_ident = multi_element_idents.get(i).unwrap();

        let multi_element_code = match multi_element_is_option {
            false => match multi_element_was_renamed {
                false => quote! { 
                    new_ele.add_elements(&si.#multi_element_ident);
                },
                true => quote! { 
                    new_ele.add_elements_with_name(#multi_element_name, &si.#multi_element_ident); 
                },
            },
            true => match multi_element_was_renamed {
                false => quote! {
                    if let Some(a) = &si.#multi_element_ident {
                        new_ele.add_elements(a);
                    }
                },
                true => quote! { 
                    if let Some(a) = &si.#multi_element_ident {
                        new_ele.add_elements_with_name(#multi_element_name, a); 
                    }
                },
            }, 
        };
        add_multi_elements_code.append_all(multi_element_code);
    }
    add_multi_elements_code
}

// dig down into the attributes of the named fields of our struct.
// return the field idents that match the provided attr_type paired with the name they will 
// ultimately be serialized with and a bool specifying if we renamed the field or not
#[cfg(feature = "process_options")]
fn get_field_idents_of_attr_type(fields: &syn::Fields, attr_type: &str) -> Vec<(syn::Ident, String, bool, bool)> {
    match fields {
        syn::Fields::Named(ref fields) => {
            let mut field_vec = Vec::new();
            for field in &fields.named {
                for a in field.attrs.clone().iter() {
                    if let Some(w) = a.interpret_meta() {
                        match w {
                            // this is if our attribute is of the form #[sxs_type_element]
                            syn::Meta::Word(i) => {
                                if &i.to_string() == attr_type {
                                    if field.ident.is_some() {
                                        let is_option = is_option_type(&field.ty);
                                        // field.ident.to_string() gives us the name of the field
                                        let val = (field.clone().ident.unwrap(), field.clone().ident.unwrap().to_string(), false, is_option);
                                        field_vec.push(val);
                                    }
                                }
                            },
                            // this is if our attribute is of the form #[sxs_type_element(rename="new_name"))]
                            syn::Meta::List(ref ml) => {
                                let newname = extract_ident_with_new_name(ml, attr_type);
                                if newname.is_some() &&  field.ident.is_some(){
                                    let is_option = is_option_type(&field.ty);
                                    let fc = field.clone();
                                    field_vec.push((fc.ident.unwrap(), newname.unwrap(), true, is_option));
                                }
                            },
                            _ => {},
                        }
                    }
                }
            }
            field_vec
        }
        // Ignore unit structs or anonymous fields.
        _ => {
            Vec::new()
        },
    }
}

#[cfg(not(feature = "process_options"))]
fn get_field_idents_of_attr_type(fields: &syn::Fields, attr_type: &str) -> Vec<(syn::Ident, String, bool, bool)> {
    match fields {
        syn::Fields::Named(ref fields) => {
            let mut field_vec = Vec::new();
            for field in &fields.named {
                for a in field.attrs.clone().iter() {
                    if let Some(w) = a.interpret_meta() {
                        match w {
                            // this is if our attribute is of the form #[sxs_type_element]
                            syn::Meta::Word(i) => {
                                if &i.to_string() == attr_type {
                                    if field.ident.is_some() {
                                        // field.ident.to_string() gives us the name of the field
                                        let val = (field.clone().ident.unwrap(), field.clone().ident.unwrap().to_string(), false, false);
                                        field_vec.push(val);
                                    }
                                }
                            },
                            // this is if our attribute is of the form #[sxs_type_element(rename="new_name"))]
                            syn::Meta::List(ref ml) => {
                                let newname = extract_ident_with_new_name(ml, attr_type);
                                if newname.is_some() &&  field.ident.is_some(){
                                    let fc = field.clone();
                                    field_vec.push((fc.ident.unwrap(), newname.unwrap(), true, false));
                                }
                            },
                            _ => {},
                        }
                    }
                }
            }
            field_vec
        }
        // Ignore unit structs or anonymous fields.
        _ => {
            Vec::new()
        },
    }
}

/// digs down into `#[sxs_type_element(rename="new_name"))]` to grab "new_name"
fn extract_ident_with_new_name(ml: &syn::MetaList, attr_type: &str) -> Option<String> {
    if ml.ident.to_string() != attr_type {
        return None;
    }
    for nested in &ml.nested {
        if let syn::NestedMeta::Meta(nv) = nested {
            if let syn::Meta::NameValue(mnv) = nv {
                // the only type of attribute param we currently allow is "rename"
                if &mnv.ident.to_string() == "rename" {
                    if let syn::Lit::Str(ref ls) = mnv.lit {
                        return Some(ls.value());
                    }
                }
            }
        }
    }
    None
}

#[cfg(feature = "process_options")]
fn is_option_type(ty: &syn::Type) -> bool {

    // syn::Type::Path(TypePath)
    if let syn::Type::Path(t) = ty {
        if t.path.segments.len() > 0 {
            let path_seg = &t.path.segments[0];
            if path_seg.ident.to_string() == "Option" {
                return true;
            }
        }
    }

    false
}