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
#![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)]
pub fn derive_params(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)
  } else {
    panic!("only struct is supported.");
  };

  let item_push: Vec<_> = meta
    .fields
    .iter()
    .map(|f| {
      let ident = &f.ident;
      let ident_str = format!("{}", ident);
      let ident_str_with_name = format!("{}.{}", name, ident);
      let bind_key = quote! {
        let part = if include_name {
          #ident_str_with_name
        } else {
          #ident_str
        };

        let key = if !path.is_empty() {
          format!("{}.{}", path, part)
        } else {
          part.to_string()
        };
      };

      quote! {
        #bind_key

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

  let expanded = quote! {
    impl ::SerializeMwsParams for #name {
      fn serialize_mws_params(&self, path: &str, include_name: bool, pairs: &mut Vec<(String, String)>) {
        #(#item_push)*
      }
    }
  };

  expanded.into()
}

#[proc_macro_derive(FromXmlStream)]
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)
  } else {
    panic!("only struct is supported.");
  };

  let fields: Vec<_> = meta
    .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 = segments.last().unwrap();
        let last_node = last.value();

        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 = args.first().unwrap();
              let first_node = first.value();
              if let &GenericArgument::Type(Type::Path(TypePath {
                path: Path { ref segments, .. },
                ..
              })) = first_node
              {
                if let Some(arg_ident) = segments.last().map(|node| node.value().ident.clone()) {
                  // `DateTime<_>` does not impl `Default`
                  // So generic impl FromXmlStream for Option<T> won't work
                  if last_node.ident == "Option" && arg_ident == "DateTime" {
                    return quote! {
                      #ident_str => record.#ident = ::xmlhelper::decode::characters(s).map(Some)?,
                    };
                  }

                  // error[E0477]: the type `mws::xmlhelper::decode::ElementScopedStream<'_, _S>` does not fulfill the required lifetime
                  if last_node.ident == "Vec" {
                    return quote! {
                      #ident_str => {
                        record.#ident = ::xmlhelper::decode::fold_elements(s, vec![], |s, v| {
                          v.push(::xmlhelper::decode::FromXmlStream::from_xml(s)?);
                          Ok(())
                        })?;
                      },
                    };
                  }
                }
              }
            }
          }
        }
      } 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 expanded = quote! {
    impl<_S> ::xmlhelper::decode::FromXmlStream<_S> for #name
    where _S: ::xmlhelper::decode::XmlEventStream
    {
      fn from_xml(s: &mut _S) -> ::result::MwsResult<Self> {
        use ::xmlhelper::decode::fold_elements;
        fold_elements(s, Self::default(), |s, record| {
          match s.local_name() {
            #(#fields)*
            _ => {}
          }
          Ok(())
        })
      }
    }
  };

  expanded.into()
}

struct StructMeta {
  fields: Vec<StructFieldMeta>,
}

struct StructFieldMeta {
  ident: Ident,
  ty: Type,
}

fn get_struct_meta(data: DataStruct) -> 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(),
      }
    }).collect();

  StructMeta { fields }
}