battler_wamp_values_proc_macro/
lib.rs

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

use itertools::Itertools;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
    parse::{
        Parse,
        ParseStream,
    },
    parse_macro_input,
    Error,
    Field,
    Ident,
    Index,
    ItemStruct,
    Meta,
    Path,
    Type,
};

#[derive(Default)]
enum DefaultAttr {
    #[default]
    False,
    True,
    Path(Path),
}

impl DefaultAttr {
    pub fn can_be_default(&self) -> bool {
        match self {
            Self::False => false,
            _ => true,
        }
    }
}

#[derive(Default)]
struct InputFieldAttrs {
    default: DefaultAttr,
    skip_serializing_if: Option<Path>,
}

fn parse_input_field_attrs(field: &Field) -> syn::Result<InputFieldAttrs> {
    let attr = field.attrs.iter().find(|attr| {
        if let Meta::List(list) = &attr.meta {
            if list.path.is_ident("battler_wamp_values") {
                return true;
            }
        }
        false
    });
    let attr = match attr {
        Some(attr) => attr,
        None => return Ok(InputFieldAttrs::default()),
    };

    let mut default = DefaultAttr::False;
    let mut skip_serializing_if = None;
    attr.parse_nested_meta(|meta| {
        if meta.path.is_ident("default") {
            default = match meta.value() {
                Ok(value) => DefaultAttr::Path(value.parse::<Path>()?),
                Err(_) => DefaultAttr::True,
            };
        }
        if meta.path.is_ident("skip_serializing_if") {
            let value = meta.value()?;
            skip_serializing_if = Some(value.parse::<Path>()?);
        }
        Ok(())
    })?;
    Ok(InputFieldAttrs {
        default,
        skip_serializing_if,
    })
}

struct InputField {
    ident: Option<Ident>,
    ty: Type,
    attrs: InputFieldAttrs,
}

struct ListInput {
    ident: Ident,
    fields: Vec<InputField>,
}

impl Parse for ListInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let call_site = Span::call_site();
        let input = match ItemStruct::parse(input) {
            Ok(item) => item,
            Err(_) => return Err(Error::new(call_site, "input must be a struct")),
        };
        let ident = input.ident;
        let mut defaulted = false;
        let fields = input
            .fields
            .into_iter()
            .map(|field| {
                let attrs = parse_input_field_attrs(&field)?;
                if attrs.default.can_be_default() {
                    defaulted = true;
                } else if defaulted {
                    return Err(Error::new(
                        call_site,
                        "fields after a default field must also have a default",
                    ));
                }
                Ok(InputField {
                    ident: field.ident,
                    ty: field.ty,
                    attrs,
                })
            })
            .collect::<Result<Vec<_>, Error>>()?;
        Ok(Self { ident, fields })
    }
}

/// Procedural macro for deriving `battler_wamp_values::WampSerialize` and
/// `battler_wamp_values::WampDeserialize` for a struct that converts to a
/// `battler_wamp_values::List`.
#[proc_macro_derive(WampList, attributes(battler_wamp_values))]
pub fn derive_wamp_list(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ListInput);
    let call_site = Span::call_site();

    let ident = input.ident;

    let (field_serializers, field_deserializers, field_identifiers): (Vec<_>, Vec<_>, Vec<_>) = input.fields.iter().enumerate().map(|(i, field)| {
        let accessor = match &field.ident {
            Some(ident) => quote!(self.#ident),
            None => { let i = Index::from(i); quote!(self.#i) },
        };
        let ty = &field.ty;
        let field_name = field.ident.clone().unwrap_or(Ident::new(&format!("field_{i}"), call_site));
        let serialize_check = match &field.attrs.skip_serializing_if {
            Some(skip_serializing_if) => Some(quote! {
                if #skip_serializing_if(&#accessor) {
                    return Ok(battler_wamp_values::Value::List(list));
                }
            }),
            None => None,
        };
        let if_empty = match &field.attrs.default {
            DefaultAttr::False => quote!(return Err(battler_wamp_values::WampDeserializeError::new(std::fmt::format(format_args!("list member {} of {} is missing", std::stringify!(#field_name), std::stringify!(#ident)))))),
            DefaultAttr::True => quote!(<#ty as Default>::default()),
            DefaultAttr::Path(path) => quote!(#path()),
        };
        (
            quote! {
                #serialize_check
                match battler_wamp_values::WampSerialize::wamp_serialize(#accessor) {
                    Ok(val) => list.push(val),
                    Err(err) => return Err(err.annotate(std::fmt::format(format_args!("failed to serialize list member {} of {}", std::stringify!(#field_name), std::stringify!(#ident))))),
                }
            },
            quote! {
                let #field_name = match list.get_mut(#i) {
                    Some(val) => {
                        let mut out = battler_wamp_values::Value::Bool(false);
                        std::mem::swap(val, &mut out);
                        Some(out)
                    }
                    None => None,
                };
                let #field_name = match #field_name {
                    Some(val) => match battler_wamp_values::WampDeserialize::wamp_deserialize(val) {
                        Ok(val) => val,
                        Err(err) => return Err(err.annotate(std::fmt::format(format_args!("failed to deserialize list member {} of {}", std::stringify!(#field_name), std::stringify!(#ident)))))
                    },
                    None => #if_empty,
                };
            },
            quote!(#field_name)
        )
    }).multiunzip();

    let serialize = quote! {
        impl battler_wamp_values::WampSerialize for #ident {
            fn wamp_serialize(self) -> core::result::Result<battler_wamp_values::Value, battler_wamp_values::WampSerializeError> {
                let mut list = battler_wamp_values::List::default();
                #(#field_serializers)*
                Ok(battler_wamp_values::Value::List(list))
            }
        }
    };

    let named = input.fields.is_empty() || input.fields.iter().any(|field| field.ident.is_some());
    let struct_constructor = if named {
        quote!(#ident { #(#field_identifiers,)* })
    } else {
        quote!(#ident(#(#field_identifiers,)*))
    };
    let deserialize = quote! {
        impl battler_wamp_values::WampDeserialize for #ident {
            fn wamp_deserialize(value: battler_wamp_values::Value) -> core::result::Result<Self, battler_wamp_values::WampDeserializeError> {
                let mut list = match value {
                    battler_wamp_values::Value::List(list) => list,
                    _ => return Err(battler_wamp_values::WampDeserializeError::new("value must be a list")),
                };
                #(#field_deserializers)*
                Ok(#struct_constructor)
            }
        }
    };

    quote! {
        #serialize
        #deserialize
    }
    .into()
}

struct DictionaryInput {
    ident: Ident,
    fields: Vec<InputField>,
}

impl Parse for DictionaryInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let call_site = Span::call_site();
        let input = match ItemStruct::parse(input) {
            Ok(item) => item,
            Err(_) => return Err(Error::new(call_site, "input must be a struct")),
        };
        let ident = input.ident;
        let fields = input
            .fields
            .into_iter()
            .map(|field| {
                let attrs = parse_input_field_attrs(&field)?;
                Ok(InputField {
                    ident: field.ident,
                    ty: field.ty,
                    attrs,
                })
            })
            .collect::<Result<Vec<_>, Error>>()?;
        Ok(Self { ident, fields })
    }
}

/// Procedural macro for deriving `battler_wamp_values::WampSerialize` and
/// `battler_wamp_values::WampDeserialize` for a struct that converts to a
/// `battler_wamp_values::Dictionary`.
#[proc_macro_derive(WampDictionary, attributes(battler_wamp_values))]
pub fn derive_wamp_dictionary(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DictionaryInput);
    let call_site = Span::call_site();

    let ident = input.ident;

    let (field_serializers, field_deserializers, field_identifiers): (Vec<_>, Vec<_>, Vec<_>) = input.fields.iter().enumerate().map(|(i, field)| {
        let accessor = match &field.ident {
            Some(ident) => quote!(self.#ident),
            None => { let i = Index::from(i); quote!(self.#i) },
        };
        let ty = &field.ty;
        let field_name = field.ident.clone().unwrap_or(Ident::new(&format!("field_{i}"), call_site));
        let serialize_check = match &field.attrs.skip_serializing_if {
            Some(skip_serializing_if) => quote!(!#skip_serializing_if(&#accessor)),
            None => quote!(true),
        };
        let if_empty = match &field.attrs.default {
            DefaultAttr::False => quote!(return Err(battler_wamp_values::WampDeserializeError::new(std::fmt::format(format_args!("dictionary member {} of {} is missing", std::stringify!(#field_name), std::stringify!(#ident)))))),
            DefaultAttr::True => quote!(<#ty as Default>::default()),
            DefaultAttr::Path(path) => quote!(#path()),
        };
        (
            quote! {
                if #serialize_check {
                    match battler_wamp_values::WampSerialize::wamp_serialize(#accessor) {
                        Ok(val) => dict.insert(stringify!(#field_name).to_owned(), val),
                        Err(err) => return Err(err.annotate(std::fmt::format(format_args!("failed to serialize dictionary member {} of {}", std::stringify!(#field_name), std::stringify!(#ident))))),
                    };
                }
            },
            quote! {
                let #field_name = match dict.get_mut(stringify!(#field_name)) {
                    Some(val) => {
                        let mut out = battler_wamp_values::Value::Bool(false);
                        std::mem::swap(val, &mut out);
                        Some(out)
                    }
                    None => None,
                };
                let #field_name = match #field_name {
                    Some(val) => match battler_wamp_values::WampDeserialize::wamp_deserialize(val) {
                        Ok(val) => val,
                        Err(err) => return Err(err.annotate(std::fmt::format(format_args!("failed to deserialize dictionary member {} of {}", std::stringify!(#field_name), std::stringify!(#ident)))))
                    },
                    None => #if_empty,
                };
            },
            quote!(#field_name)
        )
    }).multiunzip();

    let serialize = quote! {
        impl battler_wamp_values::WampSerialize for #ident {
            fn wamp_serialize(self) -> core::result::Result<battler_wamp_values::Value, battler_wamp_values::WampSerializeError> {
                let mut dict = battler_wamp_values::Dictionary::default();
                #(#field_serializers)*
                Ok(battler_wamp_values::Value::Dictionary(dict))
            }
        }
    };

    let named = input.fields.is_empty() || input.fields.iter().any(|field| field.ident.is_some());
    let struct_constructor = if named {
        quote!(#ident { #(#field_identifiers,)* })
    } else {
        quote!(#ident(#(#field_identifiers,)*))
    };
    let deserialize = quote! {
        impl battler_wamp_values::WampDeserialize for #ident {
            fn wamp_deserialize(value: battler_wamp_values::Value) -> core::result::Result<Self, battler_wamp_values::WampDeserializeError> {
                let mut dict = match value {
                    battler_wamp_values::Value::Dictionary(dict) => dict,
                    _ => return Err(battler_wamp_values::WampDeserializeError::new("value must be a list")),
                };
                #(#field_deserializers)*
                Ok(#struct_constructor)
            }
        }
    };

    quote! {
        #serialize
        #deserialize
    }
    .into()
}