html_form_struct_macro 0.2.0

Procedural macros for html_form_struct.
Documentation
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use std::{
    collections::{BTreeMap, BTreeSet},
    env,
    path::{Path, PathBuf},
};

use heck::{ToSnakeCase, ToUpperCamelCase};
use proc_macro2::{Ident, Span};
use quote::quote;
use scraper::{Html, Selector};
use syn::{parse_quote, Attribute, ItemStruct, LitStr, Token, Visibility};
use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input,
};

#[proc_macro_attribute]
pub fn form_struct(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let args = parse_macro_input!(attr as AttrArgs);
    let item = parse_macro_input!(item as ItemStruct);

    if !item.generics.params.is_empty() || item.generics.where_clause.is_some() {
        return syn::Error::new_spanned(item.generics, "form_struct does not support generics")
            .to_compile_error()
            .into();
    }

    let fields_ok = match &item.fields {
        syn::Fields::Unit => true,
        syn::Fields::Named(fields) => fields.named.is_empty(),
        syn::Fields::Unnamed(fields) => fields.unnamed.is_empty(),
    };

    if !fields_ok {
        return syn::Error::new_spanned(
            item.fields,
            "form_struct requires an empty struct declaration",
        )
        .to_compile_error()
        .into();
    }

    let args = Args {
        path: args.path,
        form: args.form,
        name: item.ident.clone(),
    };

    form_struct_impl(args, item.vis.clone(), item.attrs.clone())
}

#[proc_macro]
pub fn form_struct_(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let args = parse_macro_input!(input as Args);
    form_struct_impl(args, parse_quote!(pub), Vec::new())
}

fn form_struct_impl(
    args: Args,
    vis: Visibility,
    struct_attrs: Vec<Attribute>,
) -> proc_macro::TokenStream {
    // TODO
    let (_html_path, html_source) = match load_html(&args.path) {
        Ok(data) => data,
        Err(err) => return err,
    };

    let doc = Html::parse_document(&html_source);
    let Some(form) = Selector::parse(&args.form.value())
        .ok()
        .and_then(|sel| doc.select(&sel).next())
    else {
        return syn::Error::new_spanned(&args.form, "form selector did not match any elements")
            .to_compile_error()
            .into();
    };

    let mut enums = BTreeMap::<String, BTreeSet<String>>::new();
    let mut fields = BTreeMap::<String, FieldInfo>::new();
    let mut errors = Vec::new();
    let form_selector = args.form.value();

    collect_input_fields(&form, &form_selector, &mut enums, &mut fields, &mut errors);

    collect_select_fields(&form, &form_selector, &mut enums, &mut fields, &mut errors);

    if !errors.is_empty() {
        let mut tokens = proc_macro2::TokenStream::new();
        for err in errors {
            tokens.extend(err.to_compile_error());
        }
        return tokens.into();
    }

    let enum_attrs: Vec<Attribute> = struct_attrs
        .iter()
        .filter(|attr| attr.path().is_ident("derive"))
        .cloned()
        .collect();
    let mut enum_defs = Vec::new();
    let mut field_defs = Vec::new();

    for (name, info) in fields {
        match &info.kind {
            FieldKind::Scalar { ty } => {
                let field_ident = ident_for_field(&name);
                let field_ty = wrap_optional(ty.clone(), !info.required);
                let field_attrs = serde_field_attrs(&info);
                field_defs.push(quote! { #field_attrs pub #field_ident: #field_ty });
            }

            FieldKind::Enum => {
                let enum_ident = ident_for_type(&name);
                let field_ident = ident_for_field(&name);

                let Some(values) = enums.get(&name) else {
                    errors.push(syn::Error::new(
                        Span::call_site(),
                        format!("form '{form_selector}' enum field '{name}' has no values"),
                    ));

                    continue;
                };

                if values.is_empty() {
                    errors.push(syn::Error::new(
                        Span::call_site(),
                        format!("form '{form_selector}' enum field '{name}' has no values"),
                    ));

                    continue;
                }

                let mut variant_names = BTreeMap::<String, Vec<String>>::new();
                for value in values {
                    let variant = ident_for_variant(value);
                    variant_names
                        .entry(variant.to_string())
                        .or_default()
                        .push(value.clone());
                }

                for (variant, originals) in &variant_names {
                    if originals.len() > 1 {
                        errors.push(syn::Error::new(
                            Span::call_site(),
                            format!(
                                "form '{form_selector}' enum field '{name}' has multiple values mapping to '{variant}': {}",
                                originals.join(", ")
                            ),
                        ));
                    }
                }

                let variant_defs = variant_names
                    .iter()
                    .map(|(variant, originals)| {
                        let ident = Ident::new(variant, Span::call_site());
                        let rename = LitStr::new(&originals[0], Span::call_site());
                        quote! {
                            #[serde(rename = #rename)]
                            #ident
                        }
                    })
                    .collect::<Vec<_>>();

                if variant_defs.is_empty() {
                    continue;
                }

                enum_defs.push(quote! {
                    #( #enum_attrs )*
                    #vis enum #enum_ident {
                        #( #variant_defs, )*
                    }
                });

                let field_ty = wrap_optional(quote! { #enum_ident }, !info.required);
                let field_attrs = serde_field_attrs(&info);

                field_defs.push(quote! { #field_attrs pub #field_ident: #field_ty });
            }
        }
    }

    if !errors.is_empty() {
        let mut tokens = proc_macro2::TokenStream::new();
        for err in errors {
            tokens.extend(err.to_compile_error());
        }
        return tokens.into();
    }

    let ident = &args.name;
    //let html_path_literal = html_path.to_string_lossy();

    let expanded = quote! {
        //const _HTML_FORM_STRUCT_TRACK: &str = include_str!(#html_path_literal);

        #( #struct_attrs )*
        #vis struct #ident {
            #( #field_defs, )*
        }

        #( #enum_defs )*
    };

    expanded.into()
}

struct Args {
    path: LitStr,
    form: LitStr,
    name: Ident,
}

struct AttrArgs {
    path: LitStr,
    form: LitStr,
}

impl Parse for Args {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;
        let form: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;
        let name: Ident = input.parse()?;

        if input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
        }

        if !input.is_empty() {
            return Err(input.error("unexpected tokens in macro arguments"));
        }

        Ok(Self { path, form, name })
    }
}

impl Parse for AttrArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;
        let form: LitStr = input.parse()?;

        if input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
        }

        if !input.is_empty() {
            return Err(input.error("unexpected tokens in attribute arguments"));
        }

        Ok(Self { path, form })
    }
}

#[derive(Clone)]
enum FieldKind {
    Scalar { ty: proc_macro2::TokenStream },
    Enum,
}

#[derive(Clone)]
struct FieldInfo {
    kind: FieldKind,
    required: bool,
    with: Option<LitStr>,
}

fn load_html(path: &LitStr) -> Result<(PathBuf, String), proc_macro::TokenStream> {
    let html_path = resolve_call_site_path(path.value().as_ref())?;
    match std::fs::read_to_string(&html_path) {
        Ok(source) => Ok((html_path, source)),
        Err(err) => Err(
            syn::Error::new_spanned(path, format!("failed to read html file: {err}"))
                .to_compile_error()
                .into(),
        ),
    }
}

fn resolve_call_site_path(path: &Path) -> Result<PathBuf, proc_macro::TokenStream> {
    let manifest_dir = match env::var("CARGO_MANIFEST_DIR") {
        Ok(dir) => dir,

        Err(err) => {
            return Err(proc_macro::TokenStream::from(
                syn::Error::new(
                    Span::call_site(),
                    format!("CARGO_MANIFEST_DIR unavailable: {err}"),
                )
                .to_compile_error(),
            ));
        }
    };

    Ok(PathBuf::from(manifest_dir).join(path))
}

fn collect_input_fields(
    form: &scraper::ElementRef,
    form_selector: &str,
    enums: &mut BTreeMap<String, BTreeSet<String>>,
    fields: &mut BTreeMap<String, FieldInfo>,
    errors: &mut Vec<syn::Error>,
) {
    let mut checkbox_required_errors = BTreeSet::<String>::new();

    let selector = Selector::parse("input[name]").expect("selector is valid");
    for input in form.select(&selector) {
        let Some(name) = input.value().attr("name") else {
            continue;
        };

        let input_type = input.value().attr("type").unwrap_or("text");
        let multiple = input.value().attr("multiple").is_some();

        let mut required = input.value().attr("required").is_some();
        if input_type == "checkbox" && multiple && required {
            if checkbox_required_errors.insert(name.to_string()) {
                errors.push(syn::Error::new(
                    Span::call_site(),
                    format!(
                        "form '{form_selector}' checkbox group '{name}' uses required on individual inputs; this is not representable as a single field"
                    ),
                ));
            }

            required = false;
        }

        let comment = match comment_spec_for_element(&input) {
            Ok(spec) => spec,
            Err(err) => {
                let mut error = syn::Error::new(
                    Span::call_site(),
                    format!("form '{form_selector}' field '{name}' has invalid comment spec"),
                );

                error.combine(err);
                errors.push(error);

                None
            }
        };

        if input_type == "radio" || (input_type == "checkbox" && multiple) {
            let mut info = FieldInfo {
                kind: FieldKind::Enum,
                required,
                with: None,
            };

            if let Some(spec) = comment {
                apply_comment_spec(&mut info, &spec);
            }

            fields
                .entry(name.to_string())
                .and_modify(|existing| merge_field_info(existing, info.clone()))
                .or_insert(info);

            let values = enums.entry(name.to_string()).or_default();

            if let Some(value) = input.value().attr("value") {
                values.insert(value.to_string());
            }

            continue;
        }

        if input_type == "checkbox" {
            let mut info = FieldInfo {
                kind: FieldKind::Scalar {
                    ty: quote! { bool },
                },
                required,
                with: None,
            };

            if let Some(spec) = comment {
                apply_comment_spec(&mut info, &spec);
            }

            fields
                .entry(name.to_string())
                .and_modify(|existing| merge_field_info(existing, info.clone()))
                .or_insert(info);

            continue;
        }

        let ty = if input_type == "number" {
            quote! { i32 }
        } else {
            quote! { String }
        };

        let mut info = FieldInfo {
            kind: FieldKind::Scalar { ty },
            required,
            with: None,
        };

        if let Some(spec) = comment {
            apply_comment_spec(&mut info, &spec);
        }

        fields
            .entry(name.to_string())
            .and_modify(|existing| merge_field_info(existing, info.clone()))
            .or_insert(info);
    }
}

fn collect_select_fields(
    form: &scraper::ElementRef,
    form_selector: &str,
    enums: &mut BTreeMap<String, BTreeSet<String>>,
    fields: &mut BTreeMap<String, FieldInfo>,
    errors: &mut Vec<syn::Error>,
) {
    let selector = Selector::parse("select[name]").expect("selector is valid");
    let option_selector = Selector::parse("option").expect("selector is valid");

    for select in form.select(&selector) {
        let Some(name) = select.value().attr("name") else {
            continue;
        };

        if select.value().attr("multiple").is_some() {
            errors.push(syn::Error::new(
                Span::call_site(),
                format!(
                    "form '{form_selector}' select field '{name}' uses multiple, which is not yet supported"
                ),
            ));

            continue;
        }

        let required = select.value().attr("required").is_some();

        let comment = match comment_spec_for_element(&select) {
            Ok(spec) => spec,
            Err(err) => {
                let mut error = syn::Error::new(
                    Span::call_site(),
                    format!("form '{form_selector}' field '{name}' has invalid comment spec"),
                );

                error.combine(err);
                errors.push(error);

                None
            }
        };

        let mut info = FieldInfo {
            kind: FieldKind::Enum,
            required,
            with: None,
        };

        if let Some(spec) = comment {
            apply_comment_spec(&mut info, &spec);
        }

        fields
            .entry(name.to_string())
            .and_modify(|existing| merge_field_info(existing, info.clone()))
            .or_insert(info);

        let values = enums.entry(name.to_string()).or_default();

        for option in select.select(&option_selector) {
            if let Some(value) = option.value().attr("value") {
                values.insert(value.to_string());
            } else if let Some(text) = option.text().next() {
                let trimmed = text.trim();
                if !trimmed.is_empty() {
                    values.insert(trimmed.to_string());
                }
            }
        }
    }
}

fn wrap_optional(ty: proc_macro2::TokenStream, optional: bool) -> proc_macro2::TokenStream {
    if optional {
        quote! { ::core::option::Option<#ty> }
    } else {
        ty
    }
}

fn serde_field_attrs(info: &FieldInfo) -> proc_macro2::TokenStream {
    if let Some(with) = info.with.as_ref() {
        return quote! { #[serde(with = #with)] };
    }

    proc_macro2::TokenStream::new()
}

fn comment_spec_for_element(
    element: &scraper::ElementRef,
) -> Result<Option<CommentSpec>, syn::Error> {
    let mut node = element.prev_sibling();
    while let Some(sibling) = node {
        match sibling.value() {
            scraper::node::Node::Comment(comment) => {
                return parse_comment_spec(comment);
            }

            scraper::node::Node::Text(text) => {
                if text.trim().is_empty() {
                    node = sibling.prev_sibling();
                    continue;
                }
                return Ok(None);
            }

            _ => return Ok(None),
        }
    }

    Ok(None)
}

#[derive(Default)]
struct CommentSpec {
    type_override: Option<proc_macro2::TokenStream>,
    with: Option<LitStr>,
}

fn parse_comment_spec(comment: &str) -> Result<Option<CommentSpec>, syn::Error> {
    let trimmed = comment.trim();
    let Some(rest) = trimmed.strip_prefix("form_struct:") else {
        return Ok(None);
    };

    let mut spec = CommentSpec::default();

    for token in rest.split_whitespace() {
        if let Some((key, value)) = token.split_once('=') {
            let value = value.trim_matches('"');
            if key == "type" {
                let ty: syn::Type = syn::parse_str(value).map_err(|err| {
                    syn::Error::new(Span::call_site(), format!("invalid type override: {err}"))
                })?;

                spec.type_override = Some(quote! { #ty });
            } else if key == "with" && !value.is_empty() {
                spec.with = Some(LitStr::new(value, Span::call_site()));
            }
        }
    }

    Ok(Some(spec))
}

fn apply_comment_spec(info: &mut FieldInfo, spec: &CommentSpec) {
    let Some(override_ty) = &spec.type_override else {
        apply_serde_overrides(info, spec);
        return;
    };

    apply_serde_overrides(info, spec);

    info.kind = FieldKind::Scalar {
        ty: override_ty.clone(),
    };
}

fn apply_serde_overrides(info: &mut FieldInfo, spec: &CommentSpec) {
    if let Some(with) = spec.with.clone() {
        info.with = Some(with);
    }
}

fn merge_field_info(existing: &mut FieldInfo, incoming: FieldInfo) {
    existing.required |= incoming.required;

    if incoming.with.is_some() {
        existing.with = incoming.with;
    }

    if let FieldKind::Scalar { .. } = incoming.kind {
        existing.kind = incoming.kind;
    }
}

fn ident_for_field(name: &str) -> Ident {
    let field = match name.to_snake_case() {
        x if x.is_empty() => "field".to_string(),
        x if x.chars().next().is_some_and(|ch| ch.is_ascii_digit()) => format!("field_{x}"),
        x if syn::parse_str::<Ident>(&x).is_err() => format!("r#{x}"),
        x => x,
    };

    Ident::new(&field, Span::call_site())
}

fn ident_for_type(name: &str) -> Ident {
    let ty = match name.to_upper_camel_case() {
        x if x.is_empty() => "Generated".to_string(),
        x if x.chars().next().is_some_and(|ch| ch.is_ascii_digit()) => format!("T{x}"),
        x if syn::parse_str::<Ident>(&x).is_err() => format!("{x}Type"),
        x => x,
    };

    Ident::new(&ty, Span::call_site())
}

fn ident_for_variant(value: &str) -> Ident {
    let variant = match value.to_upper_camel_case() {
        x if x.is_empty() => "Unknown".to_string(),
        x if x.chars().next().is_some_and(|ch| ch.is_ascii_digit()) => format!("V{x}"),
        x if syn::parse_str::<Ident>(&x).is_err() => format!("{x}Value"),
        x => x,
    };

    Ident::new(&variant, Span::call_site())
}