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
#![recursion_limit = "128"]

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

use proc_macro::TokenStream;
use syn::*;

#[proc_macro_derive(SerializeMwsParams, attributes(mws_param))]
pub fn derive_params(input: TokenStream) -> TokenStream {
  let input: DeriveInput = syn::parse(input).unwrap();

  let name = input.ident;

  let expanded = match input.data {
    Data::Struct(meta) => {
      let item_push: Vec<_> = meta
        .fields
        .iter()
        .map(|f| {
          let ident = &f
            .ident
            .as_ref()
            .expect("only named struct field is supported.");
          let config_items: Vec<(String, Option<String>)> =
            get_config_items("mws_param", f, &["list_item_type_name"]);
          let ident_str = format!("{}", ident);

          let let_next_ctx = {
            let setters: Vec<_> = config_items
              .iter()
              .filter_map(|(k, v)| {
                if k == "list_item_type_name" && v.is_some() {
                  Some(quote! {
                    next_ctx.field_config.list_item_type_name = Some(#v);
                  })
                } else {
                  None
                }
              })
              .collect();
            quote! {
              let mut next_ctx = ctx.clone();
              next_ctx.field_config.list_item_type_name = Some("member");
              #(#setters)*
            }
          };

          let update_path = quote! {
            let next_path = match ctx.path.as_ref() {
              None => #ident_str.to_owned(),
              Some(ref path) => format!("{}.{}", path, #ident_str),
            };
            next_ctx.path = Some(next_path);
          };

          quote! {
            {
              #let_next_ctx
              #update_path

              self.#ident.serialize_mws_params(&next_ctx, pairs);
            }
          }
        })
        .collect();

      quote! {
        impl ::SerializeMwsParams for #name {
          fn serialize_mws_params(&self, ctx: &::SerializeMwsParamsContext, pairs: &mut Vec<(String, String)>) {
            #(#item_push)*
          }
        }
      }
    }
    Data::Enum(meta) => {
      let pat_item: Vec<_> = meta
        .variants
        .iter()
        .map(|v| {
          if v.fields != Fields::Unit {
            panic!("only unit enum variant is supported");
          }

          let variant_ident = &v.ident;
          let variant_ident_string = v.ident.to_string();

          quote! {
            #name::#variant_ident => #variant_ident_string,
          }
        })
        .collect();
      quote! {
        impl ::SerializeMwsParams for #name {
          fn serialize_mws_params(&self, ctx: &::SerializeMwsParamsContext, pairs: &mut Vec<(String, String)>) {
            let value = match *self {
              #(#pat_item)*
            };

            pairs.push((ctx.path.clone().expect("mws params type should be struct").to_owned(), value.to_owned()))
          }
        }
      }
    }
    _ => {
      panic!("union is not supported.");
    }
  };

  expanded.into()
}

#[proc_macro_derive(FromXmlStream, attributes(from_xml_stream))]
pub fn derive_from_xml_stream(input: TokenStream) -> TokenStream {
  let input: DeriveInput = syn::parse(input).unwrap();

  let name = input.ident;

  let meta = if let Data::Struct(data) = input.data {
    get_struct_meta(
      data,
      "from_xml_stream",
      &["no_list_wrapper", "from_attr", "from_content"],
    )
  } else {
    panic!("only struct is supported.");
  };

  let (attr_fields, rest): (Vec<StructFieldMeta>, Vec<StructFieldMeta>) =
    meta.fields.into_iter().partition(|f| {
      f.config_list
        .iter()
        .find(|(k, _)| k == "from_attr")
        .is_some()
    });

  let (content_fields, rest): (Vec<StructFieldMeta>, Vec<StructFieldMeta>) =
    rest.into_iter().partition(|f| {
      f.config_list
        .iter()
        .find(|(k, _)| k == "from_content")
        .is_some()
    });

  let tag_fields = rest;

  if content_fields.len() > 1 {
    panic!("cannot have more than 1 `from_content` field.");
  }

  if content_fields.len() > 0 && tag_fields.len() > 0 {
    panic!("`from_content` field found, other fields must be `from_attr`.");
  }

  let tag_fields: Vec<_> = tag_fields
    .iter()
    .map(|f| {
      let ident = &f.ident;
      let ident_str = format!("{}", ident);

      // special cases
      if let Type::Path(TypePath {
        path: Path { ref segments, .. },
        ..
      }) = f.ty
      {
        let last_node = segments.last().unwrap();

        if last_node.ident == "Option" || last_node.ident == "Vec" {
          if let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
            ref args, ..
          }) = last_node.arguments
          {
            if args.len() == 1 {
              let first_node = args.first().unwrap();
              if let &GenericArgument::Type(Type::Path(TypePath {
                path: Path { ref segments, .. },
                ..
              })) = first_node
              {
                let quote_vec =
                  |ident: &Ident, optional: bool, cl: &Vec<(String, Option<String>)>| {
                    let ident_str = ident.to_string();
                    let no_list_wrapper = cl.iter().find(|(k, _)| k == "no_list_wrapper").is_some();
                    let expr = match (optional, no_list_wrapper) {
                      (true, true) => {
                        quote! {
                          record.#ident.get_or_insert_with(|| vec![])
                            .push(::xmlhelper::decode::FromXmlStream::from_xml(s)?)
                        }
                      }
                      (false, true) => {
                        quote! {
                          record.#ident
                            .push(::xmlhelper::decode::FromXmlStream::from_xml(s)?)
                        }
                      }
                      (true, false) => {
                        quote! {
                          record.#ident = ::xmlhelper::decode::fold_elements(s, vec![], |s, v| {
                            v.push(::xmlhelper::decode::FromXmlStream::from_xml(s)?);
                            Ok(())
                          }).map(Some)?
                        }
                      }
                      (false, false) => {
                        quote! {
                          record.#ident = ::xmlhelper::decode::fold_elements(s, vec![], |s, v| {
                            v.push(::xmlhelper::decode::FromXmlStream::from_xml(s)?);
                            Ok(())
                          })?
                        }
                      }
                    };

                    quote! {
                      #ident_str => #expr,
                    }
                  };

                if let Some(arg_ident) = segments.last().map(|node| node.ident.clone()) {
                  // `DateTime<_>` does not impl `Default`
                  // So generic impl FromXmlStream for Option<T> won't work
                  if last_node.ident == "Option" {
                    if arg_ident == "DateTime" {
                      return quote! {
                        #ident_str => record.#ident = ::xmlhelper::decode::characters(s).map(Some)?,
                      };
                    } else if arg_ident == "Vec" {
                      return quote_vec(ident, true, &f.config_list);
                    }
                  }

                  // error[E0477]: the type `mws::xmlhelper::decode::ElementScopedStream<'_, _S>` does not fulfill the required lifetime
                  if last_node.ident == "Vec" {
                    return quote_vec(ident, false, &f.config_list);
                  }
                }
              }
            }
          }
        }
      } else {
        use quote::ToTokens;
        panic!(
          "unsupported field type: '{}'",
          f.ty.clone().into_token_stream()
        );
      }

      // workaround: error[E0477]: the type `mws::xmlhelper::decode::ElementScopedStream<'_, _S>` does not fulfill the required lifetime

      quote! {
        #ident_str => record.#ident = ::xmlhelper::decode::FromXmlStream::from_xml(s)?,
      }
    })
    .collect();

  let attr_fields: Vec<_> = attr_fields
    .iter()
    .map(|f| {
      let ident = &f.ident;
      let ident_str = format!("{}", ident);
      let attr_name = f
        .config_list
        .iter()
        .find(|(k, _)| k == "from_attr")
        .and_then(|(_, v)| v.clone())
        .unwrap_or_else(|| ident_str.clone());
      if let Type::Path(TypePath {
        path: Path { ref segments, .. },
        ..
      }) = f.ty
      {
        let last_node = segments.last().unwrap();

        if last_node.ident == "Option" {
          quote! {
            if let Some(value) = elem.attributes.value(#attr_name) {
              record.#ident = Some(::xmlhelper::decode::parse_str(&value)?);
            }
          }
        } else {
          quote! {
            if let Some(value) = elem.attributes.value(#attr_name) {
              record.#ident = ::xmlhelper::decode::parse_str(&value)?;
            }
          }
        }
      } else {
        use quote::ToTokens;
        panic!(
          "unsupported field type: '{}'",
          f.ty.clone().into_token_stream()
        );
      }
    })
    .collect();

  let assign_attr_fields = if attr_fields.len() > 0 {
    quote! {
      if let Some(elem) = s.container_elem() {
        #(#attr_fields)*
      }
    }
  } else {
    quote! {}
  };

  let content_field = if let Some(f) = content_fields.get(0) {
    let ident = &f.ident;
    quote! {
      record.#ident = ::xmlhelper::decode::FromXmlStream::from_xml(s)?;
    }
  } else {
    quote! {}
  };

  let assign_tag_fields = if tag_fields.len() > 0 {
    quote! {
      use ::xmlhelper::decode::fold_elements;

      record = fold_elements(s, record, |s, record| {
        match s.local_name() {
          #(#tag_fields)*
          _ => {}
        }
        Ok(())
      })?;
    }
  } else {
    quote! {}
  };

  let expanded = quote! {
    impl<_S> ::xmlhelper::decode::FromXmlStream<_S> for #name
    where _S: ::xmlhelper::decode::XmlEventStream
    {
      fn from_xml(s: &mut _S) -> ::result::MwsResult<Self> {
        let mut record = Self::default();

        #assign_attr_fields
        #content_field
        #assign_tag_fields

        Ok(record)
      }
    }
  };

  expanded.into()
}

#[proc_macro_derive(FromTdffRow, attributes(from_tdff_row))]
pub fn derive_from_diff_row(input: TokenStream) -> TokenStream {
  let input: DeriveInput = syn::parse(input).unwrap();

  let name = input.ident;

  let meta = if let Data::Struct(data) = input.data {
    get_struct_meta(data, "from_tdff_row", &["key"])
  } else {
    panic!("only struct is supported.");
  };

  let fields: Vec<_> = meta
    .fields
    .iter()
    .map(|f| {
      let ident = &f.ident;
      match f.config_list.iter().find(|(k, _)| k == "key") {
        Some(&(_, Some(ref v))) => {
          if v.contains(',') {
            let keys: Vec<_> = v.split(',').map(|s| s.trim()).collect();
            quote! {
              #(#keys)|* => record.#ident = FromTdffField::parse_tdff_field(k, &v)?,
            }
          } else {
            quote! {
              #v => record.#ident = FromTdffField::parse_tdff_field(k, &v)?,
            }
          }
        }
        _ => {
          let ident_str = format!("{}", ident);
          let ident_underscore_str = ident_str.replace("-", "_");
          if ident_str == ident_underscore_str {
            quote! {
              #ident_str => record.#ident = FromTdffField::parse_tdff_field(k, v)?,
            }
          } else {
            quote! {
              #ident_str => record.#ident = FromTdffField::parse_tdff_field(k, v)?,
            }
          }
        }
      }
    })
    .collect();

  let expanded = quote! {
    impl ::mws::tdff::FromTdffRow for #name
    {
      fn from_tdff_row(pairs: &::mws::tdff::TdffRow) -> ::mws::result::MwsResult<Self> {
        use ::mws::tdff::FromTdffField;
        let mut record = #name::default();
        for (k, v) in pairs {
          let k = k as &str;
          match k {
            #(#fields)*
            _ => {},
          }
        }
        Ok(record)
      }
    }
  };

  expanded.into()
}

struct StructMeta {
  fields: Vec<StructFieldMeta>,
}

struct StructFieldMeta {
  ident: Ident,
  ty: Type,
  config_list: Vec<(String, Option<String>)>,
}

fn get_struct_meta(data: DataStruct, config_attr_name: &str, config_key_wl: &[&str]) -> StructMeta {
  let fields = data
    .fields
    .iter()
    .map(|field| {
      let ident = if let Some(ref ident) = field.ident {
        ident
      } else {
        panic!("unnamed field is not supported");
      };
      StructFieldMeta {
        ident: ident.clone(),
        ty: field.ty.clone(),
        config_list: get_config_items(config_attr_name, field, config_key_wl),
      }
    })
    .collect();

  StructMeta { fields }
}

fn get_config_items(attr_name: &str, field: &Field, wl: &[&str]) -> Vec<(String, Option<String>)> {
  field
    .attrs
    .iter()
    .filter_map(|a| {
      a.parse_meta().ok().and_then(|meta| match meta {
        Meta::List(list) => {
          if list.path.is_ident(attr_name) {
            Some(
              list
                .nested
                .iter()
                .filter_map(|m| match m {
                  NestedMeta::Meta(meta) => {
                    let kv = match meta {
                      Meta::NameValue(MetaNameValue {
                        path,
                        lit: Lit::Str(lit_str),
                        ..
                      }) => {
                        let k = path.get_ident().unwrap().to_string();
                        let v = lit_str.value();
                        (k, Some(v))
                      }
                      Meta::Path(path) => {
                        let k = path.get_ident().unwrap().to_string();
                        (k, None)
                      }
                      _ => {
                        panic!("only `key = \"value\"` or `key` is allowed in config attrbute.");
                      }
                    };

                    if !wl.contains(&kv.0.as_ref()) {
                      panic!(
                        "unknown config key: `{}`. expecting {}",
                        kv.0,
                        wl.iter()
                          .map(|k| format!("`{}`", k))
                          .collect::<Vec<_>>()
                          .join(", ")
                      );
                    }

                    Some(kv)
                  }
                  _ => None,
                })
                .collect::<Vec<_>>(),
            )
          } else {
            None
          }
        }
        _ => None,
      })
    })
    .flat_map(|i| i)
    .collect()
}