tablo-macros 0.8.0

Procedural macros for Tablo.
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
//! `#[derive(RecordForm)]` declares the typed value a resource's form writes.
//!
//! See `tablo-core`'s `form` module for the contract.

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote, quote_spanned};
use syn::{DeriveInput, Type, spanned::Spanned};

use crate::fields::{
    Derive, assert_scalar, blank_answer, blank_option, form_attrs, last_segment, named_fields,
    reject_generics,
};

pub fn expand_tokens(input: DeriveInput) -> TokenStream2 {
    match expand_checked(input) {
        Ok(tokens) => tokens,
        Err(error) => error.to_compile_error(),
    }
}

struct FieldSpec {
    ident: syn::Ident,
    ty: Type,
    variant: syn::Ident,
    /// `#[form(embed)]`: an `EmbeddedForm` value, bound whole.
    embed: bool,
    /// `#[form(repeat)]`: a list of `FormInput` values, bound whole.
    repeat: bool,
    /// The parse's blank answer, `Option::None` for a required field.
    blank: TokenStream2,
    /// Whether the field has no blank answer, so its control renders required.
    required: bool,
    /// The control the default schema renders for the field.
    control: DefaultControl,
}

/// The control `controls()` hands over for a field.
enum DefaultControl {
    /// A text field: any scalar without a control key.
    Text,
    /// A checkbox: a `bool`.
    Toggle,
    /// A choice over an `Options` type's list.
    Choice(Box<Type>),
    /// A choice over an `OptionSource`'s rows.
    Relationship(Box<Type>),
    /// A multiple choice over an `OptionSource`'s rows: a many-to-many field, a `Vec` of their
    /// keys, of the element type.
    Links(Box<Type>, Box<Type>),
    /// A choice over the field type's own options, which its column reads as labels.
    OwnOptions,
    /// A file field.
    File,
    /// An embedded value's own schema.
    Embed,
    /// A repeater over the list's items.
    Repeat,
}

struct StructAttrs {
    model: syn::Path,
}

fn expand_checked(input: DeriveInput) -> syn::Result<TokenStream2> {
    expand_as(input, "RecordForm")
}

/// The expansion of `input`, naming `derive` when no `tablo` dependency is found.
pub(crate) fn expand_as(input: DeriveInput, derive: &str) -> syn::Result<TokenStream2> {
    reject_generics(
        &input,
        "#[derive(RecordForm)] does not support generic or lifetime parameters: a record \
         form names one model",
    )?;
    let named = named_fields(
        &input,
        "#[derive(RecordForm)] supports a struct with named fields",
        Some("#[derive(RecordForm)] needs at least one field"),
    )?;
    let attrs = struct_attrs(&input)?;
    let fields = named
        .named
        .iter()
        .map(field_spec)
        .collect::<syn::Result<Vec<_>>>()?;
    let krate = crate::tablo_core_path(&input.ident, derive)?;
    Ok(expand_struct(&krate, &input, &attrs, &fields))
}

fn struct_attrs(input: &DeriveInput) -> syn::Result<StructAttrs> {
    let mut model = None;
    for attr in &input.attrs {
        if !attr.path().is_ident("form") {
            continue;
        }
        attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("model") {
                model = Some(meta.value()?.parse::<syn::Path>()?);
                Ok(())
            } else {
                Err(meta.error("unknown `#[form(..)]` key: expected `model = <Model>`"))
            }
        })?;
    }
    let model = model.ok_or_else(|| {
        syn::Error::new_spanned(
            &input.ident,
            "#[derive(RecordForm)] needs `#[form(model = <Model>)]`",
        )
    })?;
    Ok(StructAttrs { model })
}

fn field_spec(field: &syn::Field) -> syn::Result<FieldSpec> {
    let ident = field.ident.clone().expect("named field");
    if last_segment(&field.ty).is_some_and(|name| name == "Deferred") {
        return Err(syn::Error::new_spanned(
            &field.ty,
            "a record form binds a relation's foreign key (`author_id`), not the relation: no \
             control posts a relation expression",
        ));
    }
    let attrs = form_attrs(field, Derive::Record)?;
    let variant = format_ident!("{}", pascal_case(&ident.to_string()));
    let is_bool =
        matches!(&field.ty, Type::Path(path) if path.qself.is_none() && path.path.is_ident("bool"));
    let control = if attrs.embed {
        DefaultControl::Embed
    } else if attrs.repeat {
        DefaultControl::Repeat
    } else if let Some(options) = attrs.options.clone() {
        match options {
            Some(named) => DefaultControl::Choice(Box::new(named)),
            None => DefaultControl::OwnOptions,
        }
    } else if let Some(source) = attrs.relationship.clone() {
        match list_element(&field.ty) {
            Some(element) => {
                if attrs.blank.is_some() || attrs.optional {
                    return Err(syn::Error::new_spanned(
                        field,
                        "a many-to-many field links no record when none is chosen: it takes no \
                         `blank` or `optional`",
                    ));
                }
                DefaultControl::Links(Box::new(source), Box::new(element.clone()))
            }
            None => DefaultControl::Relationship(Box::new(source)),
        }
    } else if attrs.file {
        DefaultControl::File
    } else if is_bool {
        DefaultControl::Toggle
    } else {
        DefaultControl::Text
    };
    let links = matches!(control, DefaultControl::Links(..));
    let required =
        !attrs.embed && !attrs.repeat && !links && blank_answer(&field.ty, &attrs).is_none();
    Ok(FieldSpec {
        ident,
        ty: field.ty.clone(),
        variant,
        embed: attrs.embed,
        repeat: attrs.repeat,
        blank: blank_option(&field.ty, &attrs),
        required,
        control,
    })
}

/// `author_id` → `AuthorId`; a raw identifier drops its `r#`.
fn pascal_case(name: &str) -> String {
    name.trim_start_matches("r#")
        .split('_')
        .filter(|part| !part.is_empty())
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().chain(chars).collect::<String>(),
                None => String::new(),
            }
        })
        .collect()
}

fn expand_struct(
    krate: &TokenStream2,
    input: &DeriveInput,
    attrs: &StructAttrs,
    fields: &[FieldSpec],
) -> TokenStream2 {
    let ident = &input.ident;
    let vis = &input.vis;
    let model = &attrs.model;
    let field_enum = format_ident!("{}Field", ident);
    let enum_doc = format!("One variant per field of [`{ident}`].");

    let variants: Vec<&syn::Ident> = fields.iter().map(|f| &f.variant).collect();
    let mut parts = Parts::default();
    for field in fields {
        field_parts(krate, model, &field_enum, field, &mut parts);
    }
    let Parts {
        claims,
        hydrates,
        reads,
        creates,
        updates,
        asserts,
        links,
        link_variants,
        includes,
    } = parts;
    let controls_ident = format_ident!("{}Controls", ident);
    let controls_doc = format!(
        "One control per field of [`{ident}`], each chosen from the field: arrange them into a \
         layout in `ResourceDef::form`, adjusting any with its builder's modifiers. A control \
         left out follows the layout."
    );
    let mut control_fields = Vec::new();
    let mut control_inits = Vec::new();
    let mut control_arms = Vec::new();
    for field in fields {
        let name = &field.ident;
        let variant = &field.variant;
        let path = quote! { <#model>::fields().#name() };
        let (ty, init) = match &field.control {
            DefaultControl::Text => (
                quote! { #krate::__macro::TextField },
                quote! { #krate::__macro::Field::text(#path) },
            ),
            DefaultControl::Toggle => (
                quote! { #krate::__macro::CustomField },
                quote! { #krate::__macro::Field::toggle(#path) },
            ),
            DefaultControl::Relationship(source) => (
                quote! { #krate::__macro::ChoiceField },
                quote! { #krate::__macro::Field::choice(#path).relationship::<#source>() },
            ),
            DefaultControl::Links(source, _) => (
                quote! { #krate::__macro::ChoiceField },
                quote! {
                    #krate::__macro::Field::choice::<
                        #model,
                        #krate::__macro::List<<#source as #krate::__macro::OptionSource>::Model>,
                    >(#path)
                    .relationship::<#source>()
                    .multiple()
                },
            ),
            DefaultControl::Choice(options) => (
                quote! { #krate::__macro::ChoiceField },
                quote! {
                    #krate::__macro::Field::choice(#path)
                        .options(<#options as #krate::__macro::Options>::options())
                },
            ),
            DefaultControl::OwnOptions => {
                let ty = &field.ty;
                (
                    quote! { #krate::__macro::ChoiceField },
                    quote! {
                        #krate::__macro::Field::choice(#path)
                            .options(<#ty as #krate::__macro::Options>::options())
                    },
                )
            }
            DefaultControl::File => (
                quote! { #krate::__macro::FileField },
                quote! { #krate::__macro::Field::file(#path) },
            ),
            DefaultControl::Repeat => (
                quote! { #krate::__macro::RepeaterField },
                quote! { #krate::__macro::Field::repeater(#path) },
            ),
            DefaultControl::Embed => {
                let ty = &field.ty;
                (
                    quote! { #krate::__macro::Schema },
                    quote! {
                        <#ty as #krate::__macro::EmbeddedForm>::build_schema::<#model>(
                            ::std::convert::Into::into(#path),
                        )
                    },
                )
            }
        };
        // Only the derive hands a control to its form: no other `Schema<#ident>` places one.
        let init = quote! { #krate::__macro::Retype::retype::<#ident>(#init) };
        let doc = format!(
            "The `{}` control.",
            name.to_string().trim_start_matches("r#")
        );
        control_fields.push(quote! {
            #[doc = #doc]
            pub #name: #ty<#ident>
        });
        control_arms.push(quote! {
            #field_enum::#variant => #krate::__macro::IntoSchema::into_schema(#init)
        });
        control_inits.push(quote! { #name: #init });
    }
    let names: Vec<&syn::Ident> = fields.iter().map(|f| &f.ident).collect();
    let bindings: Vec<syn::Ident> = names
        .iter()
        .map(|name| format_ident!("__read_{}", name))
        .collect();

    quote! {
        #[doc = #enum_doc]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #vis enum #field_enum {
            #(#[allow(missing_docs)] #variants,)*
        }

        #[doc = #controls_doc]
        #vis struct #controls_ident {
            #(#control_fields,)*
        }

        impl #ident {
            /// Builds every field's control from the field: a `bool` is a
            /// toggle, `#[form(options = T)]` a choice over `T`'s options,
            /// `#[form(options)]` a choice over the field type's options,
            /// `#[form(relationship = R)]` a choice over `R`'s records (a multiple choice over a
            /// `Vec` of their keys), `#[form(file)]` a file field,
            /// `#[form(embed)]` the embedded value's schema, `#[form(repeat)]` a repeater
            /// over the list's items, and any other field a text field.
            #vis fn controls() -> #controls_ident {
                #controls_ident {
                    #(#control_inits,)*
                }
            }
        }

        impl #krate::__macro::RecordForm for #ident {
            type Model = #model;
            type Field = #field_enum;

            fn control(field: #field_enum) -> #krate::__macro::Schema<Self> {
                match field {
                    #(#control_arms,)*
                }
            }

            fn fields() -> ::std::vec::Vec<#krate::__macro::FormField<#field_enum>> {
                ::std::vec![#(#claims),*]
            }

            fn includes() -> #krate::__macro::Includes<#model> {
                let includes = #krate::__macro::Includes::new();
                #(#includes)*
                includes
            }

            fn links(
                &self,
            ) -> ::std::vec::Vec<(#field_enum, ::std::vec::Vec<::std::string::String>)> {
                ::std::vec![#(#links),*]
            }

            fn hydrate(
                cx: &#krate::__macro::Cx,
                record: &#model,
            ) -> #krate::__macro::FormValues {
                #(#asserts)*
                let mut out = #krate::__macro::FormValues::new();
                #(#hydrates)*
                out
            }

            fn parse(
                cx: &#krate::__macro::Cx,
                values: &#krate::__macro::FormValues,
            ) -> ::std::result::Result<Self, ::std::vec::Vec<#krate::__macro::FieldError>> {
                let mut errors: ::std::vec::Vec<#krate::__macro::FieldError> = ::std::vec::Vec::new();
                #(#reads)*
                match (#(#bindings,)*) {
                    (#(::std::option::Option::Some(#bindings),)*) if errors.is_empty() => {
                        ::std::result::Result::Ok(Self { #(#names: #bindings),* })
                    }
                    _ => ::std::result::Result::Err(errors),
                }
            }

            fn into_create(self) -> <#model as #krate::__macro::Model>::Create {
                let create = <<#model as #krate::__macro::Model>::Create as ::std::default::Default>::default();
                #(#creates)*
                create
            }

            fn into_update<'a>(
                self,
                record: &'a mut #model,
                named: &::std::collections::HashSet<#field_enum>,
            ) -> ::std::option::Option<<#model as #krate::__macro::Model>::Update<'a>> {
                // A many-to-many field assigns no column: the write links its records.
                let links: &[#field_enum] = &[#(#link_variants),*];
                if named.iter().all(|field| links.contains(field)) {
                    return ::std::option::Option::None;
                }
                let mut update = record.update();
                #(#updates)*
                ::std::option::Option::Some(update)
            }

            fn exec_update<'a>(
                update: <#model as #krate::__macro::Model>::Update<'a>,
                ex: &'a mut dyn #krate::__macro::Executor,
            ) -> impl ::std::future::Future<Output = #krate::__macro::DbResult<()>>
                   + ::std::marker::Send
                   + 'a {
                update.exec(ex)
            }
        }
    }
}

/// The tokens each generated method holds for the fields, in declaration order.
#[derive(Default)]
struct Parts {
    claims: Vec<TokenStream2>,
    hydrates: Vec<TokenStream2>,
    reads: Vec<TokenStream2>,
    creates: Vec<TokenStream2>,
    updates: Vec<TokenStream2>,
    asserts: Vec<TokenStream2>,
    /// A many-to-many field's `links` entry, variant and include.
    links: Vec<TokenStream2>,
    link_variants: Vec<TokenStream2>,
    includes: Vec<TokenStream2>,
}

/// Adds `field`'s tokens to `parts`.
fn field_parts(
    krate: &TokenStream2,
    model: &syn::Path,
    field_enum: &syn::Ident,
    field: &FieldSpec,
    parts: &mut Parts,
) {
    let name = &field.ident;
    let ty = &field.ty;
    let variant = &field.variant;
    let name_str = name.to_string();
    let name_str = name_str.trim_start_matches("r#");
    let path = quote! { <#model>::fields().#name() };
    let binding = format_ident!("__read_{}", name);
    if let DefaultControl::Links(source, element) = &field.control {
        // The model's field is the `via` list of records, not this list of their keys: no
        // column is assigned, and the write links the records instead.
        let key = quote_spanned! {ty.span()=>
            #krate::__macro::links_key::<#model, #source, #element>(#path)
        };
        parts.claims.push(quote! {
            #krate::__macro::FormField {
                field: #field_enum::#variant,
                name: #name_str,
                required: ::std::vec::Vec::new(),
                keys: ::std::vec![#key],
            }
        });
        parts.hydrates.push(quote! {
            if let ::std::option::Option::Some(value) =
                #krate::__macro::write_links(&record.#name)
            {
                out.insert(#key, value);
            }
        });
        parts.reads.push(quote_spanned! {ty.span()=>
            let #binding: ::std::option::Option<#ty> = #krate::__macro::take_leaf(
                #krate::__macro::parse_list::<#element>(&#key, values),
                &mut errors,
            );
        });
        parts.links.push(quote! {
            (
                #field_enum::#variant,
                self.#name.iter().map(#krate::__macro::FormScalar::to_form).collect(),
            )
        });
        parts.link_variants.push(quote! { #field_enum::#variant });
        parts.includes.push(quote! {
            let includes = #krate::__macro::links_include::<#model, #source>(includes, #path);
        });
        return;
    }
    let key = quote_spanned! {ty.span()=>
        #krate::__macro::form_key::<#model, #ty>(#path)
    };
    let setter = format_ident!("set_{}", name_str);
    parts.asserts.push(quote! { let _: &#ty = &record.#name; });
    parts
        .creates
        .push(quote! { let create = create.#name(self.#name); });
    parts.updates.push(quote! {
        if named.contains(&#field_enum::#variant) {
            update.#setter(self.#name);
        }
    });
    if field.embed {
        parts.claims.push(quote! {
            #krate::__macro::embedded_field::<#model, #ty, _>(
                #path,
                #field_enum::#variant,
                #name_str,
            )
        });
        parts.hydrates.push(quote! {
            #krate::__macro::EmbeddedForm::write_form(&record.#name, #path, &mut out);
        });
        parts.reads.push(quote! {
            let #binding = #krate::__macro::take_value(
                <#ty as #krate::__macro::EmbeddedForm>::read_form(#path, values),
                &mut errors,
            );
        });
    } else if field.repeat {
        // Toasty names a list's path `List<T>`, not the field's `Vec<T>`.
        let key = quote_spanned! {ty.span()=>
            #krate::__macro::form_key::<#model, _>(#path)
        };
        parts.claims.push(quote! {
            #krate::__macro::FormField {
                field: #field_enum::#variant,
                name: #name_str,
                required: ::std::vec::Vec::new(),
                keys: ::std::vec![#key],
            }
        });
        parts.hydrates.push(quote_spanned! {ty.span()=>
            out.insert(#key, #krate::__macro::write_items(&record.#name));
        });
        parts.reads.push(quote_spanned! {ty.span()=>
            let #binding: ::std::option::Option<#ty> = #krate::__macro::take_value(
                #krate::__macro::parse_items(cx, &#key, values),
                &mut errors,
            );
        });
    } else {
        let blank = &field.blank;
        let required = field.required;
        let assert = assert_scalar(krate, ty);
        parts.claims.push(quote! {
            {
                #assert
                let key = #key;
                #krate::__macro::FormField {
                    field: #field_enum::#variant,
                    name: #name_str,
                    required: if #required {
                        ::std::vec![::std::clone::Clone::clone(&key)]
                    } else {
                        ::std::vec::Vec::new()
                    },
                    keys: ::std::vec![key],
                }
            }
        });
        parts.hydrates.push(quote_spanned! {ty.span()=>
            out.insert(#key, #krate::__macro::FormScalar::to_form(&record.#name));
        });
        parts.reads.push(quote_spanned! {ty.span()=>
            let #binding = #krate::__macro::take_leaf(
                #krate::__macro::parse_scalar::<#ty>(&#key, values, #blank),
                &mut errors,
            );
        });
    }
}

/// The element type of a `Vec<T>`, or `None` for any other type.
fn list_element(ty: &Type) -> Option<&Type> {
    let Type::Path(path) = ty else {
        return None;
    };
    let last = path.path.segments.last()?;
    match (last.ident.to_string().as_str(), &last.arguments) {
        ("Vec", syn::PathArguments::AngleBracketed(args)) => match args.args.first() {
            Some(syn::GenericArgument::Type(element)) if args.args.len() == 1 => Some(element),
            _ => None,
        },
        _ => None,
    }
}

#[cfg(test)]
mod tests;