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
use proc_macro::TokenStream;
use quote::quote;
use syn::*;

#[proc_macro_derive(StructForm, attributes(structform))]
pub fn derive_structform(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let form_ident = input.ident.clone();
    let field_enum_ident = field_enum_ident_transform(&form_ident);

    let input_struct_data = match input.data {
        Data::Struct(data) => data,
        _ => panic!("StructForm can only be derived for structs"),
    };
    let container_attrs: FormContainerAttribute = input
        .attrs
        .iter()
        .find(|attr| attr.path.is_ident("structform"))
        .map(|attr| {
            attr.parse_args()
                .expect("Failed to parse the #[structform] attr on the container")
        })
        .expect("Require a #[structform] attribute on the container");
    let model = container_attrs.model;

    let enriched_fields = enrich_fields(&input_struct_data);

    let (input_names, input_fields_type): (Vec<(Ident, Ident)>, Vec<Type>) = enriched_fields
        .iter()
        .filter_map(|field| match &field.ty {
            FieldType::Input { input_type } => Some((field.names(), input_type.clone())),
            _ => None,
        })
        .unzip();
    let (input_fields_snake_case, input_fields_pascal_case): (Vec<Ident>, Vec<Ident>) =
        input_names.into_iter().unzip();

    let (option_form_names, option_form_fields_type): (Vec<(Ident, Ident)>, Vec<Type>) =
        enriched_fields
            .iter()
            .filter_map(|field| match &field.ty {
                FieldType::OptionalSubform { subform_type } => {
                    Some((field.names(), subform_type.clone()))
                }
                _ => None,
            })
            .unzip();
    let (option_form_fields_snake_case, option_form_fields_pascal_case): (Vec<Ident>, Vec<Ident>) =
        option_form_names.into_iter().unzip();
    let option_form_fields_type_field_enum: Vec<Ident> = option_form_fields_type
        .iter()
        .map(type_to_field_enum_ident)
        .collect();

    let option_form_fields_toggles_pascal_case: Vec<Ident> = option_form_fields_pascal_case
        .iter()
        .map(|field_ident| Ident::new(&format!("Toggle{}", field_ident), field_ident.span()))
        .collect();

    let (list_form_names, list_form_fields_type): (Vec<(Ident, Ident)>, Vec<Type>) =
        enriched_fields
            .iter()
            .filter_map(|field| match &field.ty {
                FieldType::ListSubform { subform_type } => {
                    Some((field.names(), subform_type.clone()))
                }
                _ => None,
            })
            .unzip();
    let (list_form_fields_snake_case, list_form_fields_pascal_case): (Vec<Ident>, Vec<Ident>) =
        list_form_names.into_iter().unzip();
    let list_form_fields_type_field_enum: Vec<Ident> = list_form_fields_type
        .iter()
        .map(type_to_field_enum_ident)
        .collect();

    let list_form_fields_add_pascal_case: Vec<Ident> = list_form_fields_pascal_case
        .iter()
        .map(|field_ident| Ident::new(&format!("Add{}", field_ident), field_ident.span()))
        .collect();
    let list_form_fields_remove_pascal_case: Vec<Ident> = list_form_fields_pascal_case
        .iter()
        .map(|field_ident| Ident::new(&format!("Remove{}", field_ident), field_ident.span()))
        .collect();

    let (subform_names, subform_fields_type): (Vec<(Ident, Ident)>, Vec<Type>) = enriched_fields
        .iter()
        .filter_map(|field| match &field.ty {
            FieldType::Subform { subform_type } => Some((field.names(), subform_type.clone())),
            _ => None,
        })
        .unzip();
    let (subform_fields_snake_case, subform_fields_pascal_case): (Vec<Ident>, Vec<Ident>) =
        subform_names.into_iter().unzip();
    let subform_fields_type_field_enum: Vec<Ident> = subform_fields_type
        .iter()
        .map(type_to_field_enum_ident)
        .collect();

    let submit_attempted_fields_snake_case: Vec<Ident> = enriched_fields
        .iter()
        .filter_map(|field| match &field.ty {
            FieldType::SubmitAttempted => Some(field.snake_case_ident.clone()),
            _ => None,
        })
        .collect();

    let field_enum = quote! {
        #[derive(Debug)]
        pub enum #field_enum_ident {
            #(#input_fields_pascal_case,)*
            #(#option_form_fields_toggles_pascal_case,)*
            #(#option_form_fields_pascal_case(#option_form_fields_type_field_enum),)*
            #(#list_form_fields_add_pascal_case,)*
            #(#list_form_fields_pascal_case(usize, #list_form_fields_type_field_enum),)*
            #(#list_form_fields_remove_pascal_case(usize),)*
            #(#subform_fields_pascal_case(#subform_fields_type_field_enum),)*
        }
    };

    let impl_new = if container_attrs.flatten {
        quote! {
            fn new(model: &#model) -> #form_ident {
                #form_ident {
                    #(#input_fields_snake_case: <#input_fields_type>::new(&model),)*
                    #(#submit_attempted_fields_snake_case: false,)*
                }
            }
        }
    } else {
        quote! {
            fn new(model: &#model) -> #form_ident {
                #form_ident {
                    #(#input_fields_snake_case: <#input_fields_type>::new(&model.#input_fields_snake_case),)*
                    #(#option_form_fields_snake_case: model.#option_form_fields_snake_case.as_ref().map(<#option_form_fields_type>::new),)*
                    #(#list_form_fields_snake_case: model.#list_form_fields_snake_case.iter().map(<#list_form_fields_type>::new).collect(),)*
                    #(#subform_fields_snake_case: <#subform_fields_type>::new(&model.#subform_fields_snake_case),)*
                    #(#submit_attempted_fields_snake_case: false,)*
                }
            }
        }
    };

    let impl_submit = container_attrs
        .submit_with
        .map(|submit_with| {
            quote! {
                fn submit(&mut self) -> Result<#model, structform::ParseError> {
                    #(self.#submit_attempted_fields_snake_case = true;)*
                    #submit_with(self)
                }
            }
        })
        .unwrap_or(if container_attrs.flatten {
            quote! {
                fn submit(&mut self) -> Result<#model, structform::ParseError> {
                    #(self.#submit_attempted_fields_snake_case = true;)*
                    #(self.#input_fields_snake_case.submit())*
                }
            }
        } else {
            quote! {
                fn submit(&mut self) -> Result<#model, structform::ParseError> {
                    #(self.#submit_attempted_fields_snake_case = true;)*
                    self.submit_update(<#model>::default())
                }
            }
        });

    let impl_submit_update = if container_attrs.flatten {
        quote! {
            fn submit_update(&mut self, mut model: #model) -> Result<#model, structform::ParseError> {
                #(self.#submit_attempted_fields_snake_case = true;)*
                #(self.#input_fields_snake_case.submit())*
            }
        }
    } else {
        quote! {
            fn submit_update(&mut self, mut model: #model) -> Result<#model, structform::ParseError> {
                #(self.#submit_attempted_fields_snake_case = true;)*

                #(let #input_fields_snake_case = self.#input_fields_snake_case.submit();)*
                #(let #option_form_fields_snake_case = self.#option_form_fields_snake_case.as_mut().map(|inner_form| {
                    model.#option_form_fields_snake_case
                        .clone()
                        .map(|inner_model| inner_form.submit_update(inner_model))
                        .unwrap_or_else(|| inner_form.submit())
                }).transpose();)*
                #(let #list_form_fields_snake_case = self.#list_form_fields_snake_case.iter_mut().enumerate().map(|(i, inner_form)| {
                    model.#list_form_fields_snake_case
                        .get(i)
                        .map(|inner_model| inner_form.submit_update(inner_model.clone()))
                        .unwrap_or_else(|| inner_form.submit())
                }).collect::<Result<Vec<_>,_>>();)*
                #(let #subform_fields_snake_case = self.#subform_fields_snake_case.submit_update(model.#subform_fields_snake_case.clone());)*

                #(model.#input_fields_snake_case = #input_fields_snake_case?;)*
                #(model.#option_form_fields_snake_case = #option_form_fields_snake_case?;)*
                #(model.#list_form_fields_snake_case = #list_form_fields_snake_case?;)*
                #(model.#subform_fields_snake_case = #subform_fields_snake_case?;)*
                Ok(model)
            }
        }
    };

    let impl_set_input = quote! {
        fn set_input(&mut self, field: #field_enum_ident, value: String) {
            match field {
                #(#field_enum_ident::#input_fields_pascal_case => self.#input_fields_snake_case.set_input(value),)*
                #(#field_enum_ident::#option_form_fields_toggles_pascal_case => {
                    if self.#option_form_fields_snake_case.is_some() {
                        self.#option_form_fields_snake_case = None;
                    } else {
                        self.#option_form_fields_snake_case = Some(#option_form_fields_type::default());
                    }
                },)*
                #(#field_enum_ident::#option_form_fields_pascal_case(subfield) => {
                    self.#option_form_fields_snake_case
                        .as_mut()
                        .map(|inner_form| inner_form.set_input(subfield, value));
                },)*
                #(#field_enum_ident::#list_form_fields_add_pascal_case => {
                    self.#list_form_fields_snake_case
                        .push(#list_form_fields_type::default());
                },)*
                #(#field_enum_ident::#list_form_fields_pascal_case(i, subfield) => {
                    self.#list_form_fields_snake_case
                        .get_mut(i)
                        .map(|inner_form| inner_form.set_input(subfield, value));
                },)*
                #(#field_enum_ident::#list_form_fields_remove_pascal_case(i) => {
                    if i < self.#list_form_fields_snake_case.len() {
                        self.#list_form_fields_snake_case.remove(i);
                    }
                },)*

                #(#field_enum_ident::#subform_fields_pascal_case(subfield) => {
                    self.#subform_fields_snake_case.set_input(subfield, value);
                },)*
            }
        }
    };

    let impl_submit_attempted = quote! {
        fn submit_attempted(&self) -> bool {
            false #(|| self.#submit_attempted_fields_snake_case)*
        }
    };

    let impl_is_empty = quote! {
        fn is_empty(&self) -> bool {
            true
            #(&& self.#input_fields_snake_case.is_empty())*
            #(&& self.#option_form_fields_snake_case.as_ref().map(|inner_form| inner_form.is_empty()).unwrap_or(true))*
            #(&& self.#list_form_fields_snake_case.iter().all(|inner_form| inner_form.is_empty()))*
            #(&& self.#subform_fields_snake_case.is_empty())*
        }
    };

    let impl_form = quote! {
        impl structform::StructForm<#model> for #form_ident {
            type Field = #field_enum_ident;

            #impl_new
            #impl_submit
            #impl_submit_update
            #impl_set_input
            #impl_submit_attempted
            #impl_is_empty
        }
    };

    (quote! {
        #field_enum

        #impl_form
    })
    .into()
}

fn snake_to_pascal_case(snake: &str) -> String {
    snake
        .split('_')
        .map(|s| {
            let (head, tail) = s.split_at(1);
            format!("{}{}", head.to_uppercase(), tail)
        })
        .collect::<Vec<_>>()
        .join("")
}

fn is_option(field: &Field) -> bool {
    if let Type::Path(TypePath { path, .. }) = &field.ty {
        let path_ident = &path.segments.first().unwrap().ident;
        path_ident == &Ident::new("Option", path_ident.span())
    } else {
        false
    }
}

fn is_vec(field: &Field) -> bool {
    if let Type::Path(TypePath { path, .. }) = &field.ty {
        let path_ident = &path.segments.first().unwrap().ident;
        path_ident == &Ident::new("Vec", path_ident.span())
    } else {
        false
    }
}

fn parse_option_type_generic_type(option_type: &Type) -> Type {
    match option_type {
        Type::Path(TypePath { path, .. }) => match &path.segments.first().unwrap().arguments {
            PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => {
                match args.first().unwrap() {
                    GenericArgument::Type(generic_type) => generic_type.clone(),
                    _ => panic!("Option's type argument was not a generic type"),
                }
            }
            _ => panic!("Option type did not have an angle bracketed generic argument"),
        },
        _ => panic!("Option type did not have a generic argument"),
    }
}

fn parse_vec_type_generic_type(vec_type: &Type) -> Type {
    match vec_type {
        Type::Path(TypePath { path, .. }) => match &path.segments.first().unwrap().arguments {
            PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => {
                match args.first().unwrap() {
                    GenericArgument::Type(generic_type) => generic_type.clone(),
                    _ => panic!("Vec's type argument was not a generic type"),
                }
            }
            _ => panic!("Vec type did not have an angle bracketed generic argument"),
        },
        _ => panic!("Vec type did not have a generic argument"),
    }
}

fn type_to_field_enum_ident(ty: &Type) -> Ident {
    match ty {
        Type::Path(TypePath { path, .. }) => {
            field_enum_ident_transform(&path.segments.first().unwrap().ident)
        }
        _ => panic!("Option's generic type was not a TypePath"),
    }
}

fn field_enum_ident_transform(ident: &Ident) -> Ident {
    Ident::new(&format!("{}Field", ident), ident.span())
}

struct FormContainerAttribute {
    model: Ident,
    submit_with: Option<Ident>,
    flatten: bool,
}

impl parse::Parse for FormContainerAttribute {
    fn parse(parse_buffer: &syn::parse::ParseBuffer<'_>) -> parse::Result<Self> {
        let meta_list = parse_buffer.parse_terminated::<_, syn::token::Comma>(NestedMeta::parse)?;
        let model: String = meta_list
            .iter()
            .filter_map(|arg| match arg {
                NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. }))
                    if path.is_ident("model") =>
                {
                    match lit {
                        Lit::Str(lit) => Some(lit.value()),
                        _ => None,
                    }
                }
                _ => None,
            })
            .next()
            .expect(
                "Expected to find an attribute indicating the model type: #[structform(model = \"???\")]",
            );
        let model = Ident::new(&model, parse_buffer.span());
        let submit_with: Option<String> = meta_list
            .iter()
            .filter_map(|arg| match arg {
                NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. }))
                    if path.is_ident("submit_with") =>
                {
                    match lit {
                        Lit::Str(lit) => Some(lit.value()),
                        _ => None,
                    }
                }
                _ => None,
            })
            .next();
        let submit_with =
            submit_with.map(|submit_with| Ident::new(&submit_with, parse_buffer.span()));
        let flatten = meta_list.iter().any(
            |arg| matches!(arg, NestedMeta::Meta(Meta::Path(path)) if path.is_ident("flatten")),
        );

        Ok(FormContainerAttribute {
            model,
            submit_with,
            flatten,
        })
    }
}

#[derive(Default)]
struct FormFieldAttribute {
    submit_attempted: bool,
    subform: bool,
}

impl parse::Parse for FormFieldAttribute {
    fn parse(parse_buffer: &syn::parse::ParseBuffer<'_>) -> parse::Result<Self> {
        let meta_list = parse_buffer.parse_terminated::<_, syn::token::Comma>(NestedMeta::parse)?;
        let submit_attempted = meta_list.iter().any(|arg| matches!(arg, NestedMeta::Meta(Meta::Path(path)) if path.is_ident("submit_attempted")));
        let subform = meta_list.iter().any(
            |arg| matches!(arg, NestedMeta::Meta(Meta::Path(path)) if path.is_ident("subform")),
        );

        Ok(FormFieldAttribute {
            submit_attempted,
            subform,
        })
    }
}

struct RichField {
    snake_case_ident: Ident,
    pascal_case_ident: Ident,
    ty: FieldType,
}

impl RichField {
    fn names(&self) -> (Ident, Ident) {
        (
            self.snake_case_ident.clone(),
            self.pascal_case_ident.clone(),
        )
    }
}

fn enrich_fields(struct_data: &DataStruct) -> Vec<RichField> {
    struct_data
        .fields
        .iter()
        .map(|field| {
            let snake_case_ident = field
                .ident
                .clone()
                .expect("Only normal structs are supported.");
            let pascal_case_ident = Ident::new(
                &snake_to_pascal_case(&snake_case_ident.to_string()),
                snake_case_ident.span(),
            );
            let attrs = field
                .attrs
                .iter()
                .filter(|attr| attr.path.is_ident("structform"))
                .map(|attr| {
                    attr.parse_args::<FormFieldAttribute>()
                        .expect("failed to parse attrs on a field")
                })
                .next()
                .unwrap_or_default();

            let ty = if attrs.submit_attempted {
                FieldType::SubmitAttempted
            } else if attrs.subform {
                FieldType::Subform {
                    subform_type: field.ty.clone(),
                }
            } else if is_option(field) {
                FieldType::OptionalSubform {
                    subform_type: parse_option_type_generic_type(&field.ty),
                }
            } else if is_vec(field) {
                FieldType::ListSubform {
                    subform_type: parse_vec_type_generic_type(&field.ty),
                }
            } else {
                FieldType::Input {
                    input_type: field.ty.clone(),
                }
            };

            RichField {
                snake_case_ident,
                pascal_case_ident,
                ty,
            }
        })
        .collect()
}

enum FieldType {
    Input { input_type: Type },
    Subform { subform_type: Type },
    OptionalSubform { subform_type: Type },
    ListSubform { subform_type: Type },
    SubmitAttempted,
}