hblank-macros 0.5.0

Macros for Hblank component fixtures
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//! Procedural macros for Hblank.

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{
    Attribute, Data, DeriveInput, Error, Expr, ExprLit, Fields, FnArg, ItemFn, Lit, LitStr, Meta,
    Path, Type,
    parse::{Parse, ParseStream},
    parse_macro_input,
};

#[proc_macro_derive(HblankProps, attributes(hblank))]
pub fn derive_hblank_props(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    expand_hblank_props(input)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand_hblank_props(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    let name = input.ident;
    let Data::Struct(data) = input.data else {
        return Err(Error::new_spanned(
            name,
            "HblankProps can only be derived for structs with named fields",
        ));
    };
    let Fields::Named(fields) = data.fields else {
        return Err(Error::new_spanned(
            name,
            "HblankProps requires named fields",
        ));
    };

    let mut definitions = Vec::with_capacity(fields.named.len());
    let mut readers = Vec::with_capacity(fields.named.len());
    let mut writers = Vec::with_capacity(fields.named.len());

    for field in fields.named {
        let options = field_options(&field.attrs)?;
        if options.skip {
            continue;
        }
        let ident = field
            .ident
            .ok_or_else(|| Error::new_spanned(&field.ty, "HblankProps requires named fields"))?;
        let ty = field.ty;
        let id = ident.to_string();
        let kind = control_kind(&ty, &options);
        let label = options.label.unwrap_or_else(|| humanize(&id));
        let docs = docs(&field.attrs);
        let definition = quote! {
            ::hblank::ControlDefinition {
                id: #id,
                label: #label,
                docs: #docs,
                kind: #kind,
            }
        };
        let (reader, writer) =
            control_accessors(&ty, &ident, &id, &definition, options.adapter.as_ref());

        definitions.push(definition);
        readers.push(reader);
        writers.push(writer);
    }

    Ok(quote! {
        impl ::hblank::HblankProps for #name {
            fn definitions(&self) -> &'static [::hblank::ControlDefinition] {
                const DEFINITIONS: &[::hblank::ControlDefinition] = &[
                    #(#definitions),*
                ];
                DEFINITIONS
            }

            fn control_value(&self, id: &str) -> Option<::hblank::ControlValue> {
                match id {
                    #(#readers,)*
                    _ => None,
                }
            }

            fn set_control(
                &mut self,
                id: &str,
                value: ::hblank::ControlValue,
            ) -> Result<(), ::hblank::ControlError> {
                match id {
                    #(#writers,)*
                    _ => Err(::hblank::ControlError::UnknownControl(id.to_owned())),
                }
            }

            fn clone_box(&self) -> Box<dyn ::hblank::HblankProps> {
                Box::new(self.clone())
            }

            fn as_any(&self) -> &dyn ::std::any::Any {
                self
            }
        }
    })
}

#[proc_macro_derive(HblankEnum, attributes(hblank))]
pub fn derive_hblank_enum(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    expand_hblank_enum(input)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand_hblank_enum(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    let name = input.ident;
    let Data::Enum(data) = input.data else {
        return Err(Error::new_spanned(
            name,
            "HblankEnum can only be derived for enums",
        ));
    };

    let mut variants = Vec::with_capacity(data.variants.len());
    let mut names = Vec::with_capacity(data.variants.len());
    for variant in data.variants {
        if !matches!(variant.fields, Fields::Unit) {
            return Err(Error::new_spanned(
                variant,
                "HblankEnum variants cannot contain data",
            ));
        }
        let ident = variant.ident;
        let label = field_label(&variant.attrs)?.unwrap_or_else(|| humanize(&ident.to_string()));
        variants.push(ident);
        names.push(label);
    }

    Ok(quote! {
        impl ::hblank::HblankEnum for #name {
            const VARIANTS: &'static [&'static str] = &[#(#names),*];

            fn variant_name(&self) -> &'static str {
                match self {
                    #(Self::#variants => #names),*
                }
            }

            fn from_variant_name(value: &str) -> Option<Self> {
                match value {
                    #(#names => Some(Self::#variants),)*
                    _ => None,
                }
            }
        }
    })
}

#[derive(Default)]
struct ComponentArgs {
    title: Option<LitStr>,
    group: Option<LitStr>,
    docs: Option<Path>,
    handle: Option<Type>,
}

#[proc_macro_attribute]
pub fn component(args: TokenStream, input: TokenStream) -> TokenStream {
    let mut component_args = ComponentArgs::default();
    let parser = syn::meta::parser(|meta| {
        if meta.path.is_ident("title") {
            component_args.title = Some(meta.value()?.parse()?);
        } else if meta.path.is_ident("group") {
            component_args.group = Some(meta.value()?.parse()?);
        } else if meta.path.is_ident("docs") {
            component_args.docs = Some(meta.value()?.parse()?);
        } else if meta.path.is_ident("handle") {
            component_args.handle = Some(meta.value()?.parse()?);
        } else {
            return Err(meta.error("expected one of: title, group, docs, handle"));
        }
        Ok(())
    });
    syn::parse_macro_input!(args with parser);
    let function = parse_macro_input!(input as ItemFn);
    expand_component(component_args, &function)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand_component(
    args: ComponentArgs,
    function: &ItemFn,
) -> syn::Result<proc_macro2::TokenStream> {
    validate_synchronous_non_generic(function, "components")?;
    let props_type = render_props_type(function, "components")?;
    let function_name = &function.sig.ident;
    let module_name = format_ident!("__hblank_component_{}", function_name);
    let function_docs = docs(&function.attrs);
    let title = args.title.unwrap_or_else(|| {
        LitStr::new(&humanize(&function_name.to_string()), function_name.span())
    });
    let group = args
        .group
        .map_or_else(|| quote!(module_path!()), |group| quote!(#group));
    let doc_page = args
        .docs
        .map_or_else(|| quote!(), |docs| quote!(.with_docs(#docs())));
    let handle_helper = args.handle.map_or_else(
        || quote!(),
        |handle| {
            quote! {
                pub(crate) fn render_with_handle(
                    props: &#props_type,
                    window: &mut ::hblank::gpui::Window,
                    cx: &mut ::hblank::gpui::App,
                ) -> (::hblank::gpui::AnyElement, #handle) {
                    super::#function_name(props, window, cx).into_erased_parts()
                }
            }
        },
    );

    Ok(quote! {
        #function

        #[doc(hidden)]
        pub(crate) mod #module_name {
            use super::*;

            pub(crate) fn id() -> ::std::string::String {
                ::hblank::canonical_source_id(file!(), stringify!(#function_name))
            }

            pub(crate) fn assert_props(_: &#props_type) {}

            #handle_helper

            pub(crate) fn build() -> ::hblank::ComponentDefinition {
                fn render(
                    props: &dyn ::hblank::HblankProps,
                    window: &mut ::hblank::gpui::Window,
                    cx: &mut ::hblank::gpui::App,
                ) -> ::hblank::gpui::AnyElement {
                    let props = props
                        .as_any()
                        .downcast_ref::<#props_type>()
                        .expect("Hblank component received the wrong props type");
                    ::hblank::gpui::IntoElement::into_any_element(
                        super::#function_name(props, window, cx),
                    )
                }

                ::hblank::ComponentDefinition::new::<#props_type>(
                    ::hblank::ComponentMetadata {
                        id: id(),
                        title: #title,
                        group: #group,
                        docs: #function_docs,
                        declaration: stringify!(#function),
                        source: file!(),
                        line: line!(),
                    },
                    render,
                )
                #doc_page
            }
        }

        ::hblank::__private::inventory::submit! {
            ::hblank::ComponentRegistration { build: #module_name::build }
        }
    })
}

struct RenderHandleInput {
    component: Path,
    props: Expr,
    window: Expr,
    cx: Expr,
}

impl Parse for RenderHandleInput {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let component = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let props = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let window = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let cx = input.parse()?;
        Ok(Self {
            component,
            props,
            window,
            cx,
        })
    }
}

#[proc_macro]
pub fn render_handle(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as RenderHandleInput);
    let props = input.props;
    let window = input.window;
    let cx = input.cx;
    match component_module_path(&input.component) {
        Ok(module) => quote!(#module::render_with_handle(#props, #window, #cx)).into(),
        Err(error) => error.into_compile_error().into(),
    }
}

struct CustomDocInput {
    renderer: Path,
    payload: Expr,
}

impl Parse for CustomDocInput {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let renderer = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let payload = input.parse()?;
        Ok(Self { renderer, payload })
    }
}

#[proc_macro_attribute]
pub fn doc_block(args: TokenStream, input: TokenStream) -> TokenStream {
    if !args.is_empty() {
        return Error::new(
            proc_macro2::Span::call_site(),
            "Hblank custom doc blocks take no attributes",
        )
        .into_compile_error()
        .into();
    }
    let function = parse_macro_input!(input as ItemFn);
    let function_name = &function.sig.ident;
    let module_name = format_ident!("__hblank_doc_block_{}", function_name);
    quote! {
        #function

        #[doc(hidden)]
        pub(crate) mod #module_name {
            pub(crate) fn id() -> ::std::string::String {
                concat!(module_path!(), "::", stringify!(#function_name)).to_owned()
            }

            pub(crate) const RENDER: ::hblank::CustomDocRenderer = super::#function_name;
        }

        ::hblank::__private::inventory::submit! {
            ::hblank::CustomDocBlockRegistration {
                id: concat!(module_path!(), "::", stringify!(#function_name)),
                render: #module_name::RENDER,
            }
        }
    }
    .into()
}

#[proc_macro]
pub fn custom_doc(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as CustomDocInput);
    let payload = input.payload;
    match doc_block_module_path(&input.renderer) {
        Ok(module) => quote!(::hblank::DocBlock::custom(#module::id(), #payload)).into(),
        Err(error) => error.into_compile_error().into(),
    }
}

#[proc_macro_attribute]
pub fn theme_hook(args: TokenStream, input: TokenStream) -> TokenStream {
    if !args.is_empty() {
        return Error::new(
            proc_macro2::Span::call_site(),
            "Hblank theme hooks take no attributes",
        )
        .into_compile_error()
        .into();
    }
    let function = parse_macro_input!(input as ItemFn);
    let function_name = &function.sig.ident;
    quote! {
        #function

        const _: ::hblank::ThemeHook = #function_name;

        ::hblank::__private::inventory::submit! {
            ::hblank::ThemeHookRegistration {
                id: concat!(module_path!(), "::", stringify!(#function_name)),
                apply: #function_name,
            }
        }
    }
    .into()
}

#[derive(Default)]
struct FixtureArgs {
    component: Option<Path>,
    title: Option<LitStr>,
}

#[proc_macro]
pub fn fixture_ref(input: TokenStream) -> TokenStream {
    let fixture = parse_macro_input!(input as Path);
    match fixture_module_path(&fixture) {
        Ok(module) => quote!(#module::id()).into(),
        Err(error) => error.into_compile_error().into(),
    }
}

#[proc_macro_attribute]
pub fn fixture(args: TokenStream, input: TokenStream) -> TokenStream {
    let mut fixture_args = FixtureArgs::default();
    let parser = syn::meta::parser(|meta| {
        if meta.path.is_ident("component") {
            fixture_args.component = Some(meta.value()?.parse()?);
        } else if meta.path.is_ident("title") {
            fixture_args.title = Some(meta.value()?.parse()?);
        } else {
            return Err(meta.error("expected one of: component, title"));
        }
        Ok(())
    });
    syn::parse_macro_input!(args with parser);
    let function = parse_macro_input!(input as ItemFn);
    expand_fixture(fixture_args, &function)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand_fixture(args: FixtureArgs, function: &ItemFn) -> syn::Result<proc_macro2::TokenStream> {
    validate_synchronous_non_generic(function, "fixtures")?;
    if !function.sig.inputs.is_empty() {
        return Err(Error::new_spanned(
            &function.sig.inputs,
            "Hblank fixture variants take no arguments and return component props",
        ));
    }
    let component = args.component.ok_or_else(|| {
        Error::new_spanned(
            &function.sig.ident,
            "Hblank fixture variants require component = path::to::component",
        )
    })?;
    let component_module = component_module_path(&component)?;
    let function_name = &function.sig.ident;
    let module_name = format_ident!("__hblank_fixture_{}", function_name);
    let function_docs = docs(&function.attrs);
    let title = args.title.unwrap_or_else(|| {
        LitStr::new(&humanize(&function_name.to_string()), function_name.span())
    });

    Ok(quote! {
        #function

        #[doc(hidden)]
        pub(crate) mod #module_name {
            use super::*;

            pub(crate) fn id() -> ::std::string::String {
                ::hblank::canonical_source_id(file!(), stringify!(#function_name))
            }

            pub(crate) fn build() -> ::hblank::FixtureRegistrationData {
                let defaults = super::#function_name();
                #component_module::assert_props(&defaults);
                ::hblank::FixtureRegistrationData::new(
                    ::hblank::FixtureRegistrationMetadata {
                        id: id(),
                        title: #title,
                        docs: #function_docs,
                        declaration: stringify!(#function),
                        source: file!(),
                        line: line!(),
                    },
                    #component_module::id(),
                    ::std::boxed::Box::new(defaults),
                )
            }
        }

        ::hblank::__private::inventory::submit! {
            ::hblank::FixtureRegistration { build: #module_name::build }
        }
    })
}

fn validate_synchronous_non_generic(function: &ItemFn, subject: &str) -> syn::Result<()> {
    if function.sig.asyncness.is_some() {
        return Err(Error::new_spanned(
            &function.sig,
            format!("Hblank {subject} must be synchronous"),
        ));
    }
    if !function.sig.generics.params.is_empty() {
        return Err(Error::new_spanned(
            &function.sig.generics,
            format!("Hblank {subject} cannot be generic"),
        ));
    }
    Ok(())
}

fn render_props_type<'a>(function: &'a ItemFn, subject: &str) -> syn::Result<&'a Type> {
    if function.sig.inputs.len() != 3 {
        return Err(Error::new_spanned(
            &function.sig.inputs,
            format!("Hblank {subject} take exactly (&Props, &mut gpui::Window, &mut gpui::App)"),
        ));
    }
    let first = function
        .sig
        .inputs
        .first()
        .ok_or_else(|| Error::new_spanned(&function.sig, "missing props argument"))?;
    let FnArg::Typed(first) = first else {
        return Err(Error::new_spanned(
            first,
            format!("the first Hblank {subject} argument must be &Props"),
        ));
    };
    let Type::Reference(props_reference) = first.ty.as_ref() else {
        return Err(Error::new_spanned(
            &first.ty,
            format!("the first Hblank {subject} argument must be &Props"),
        ));
    };
    if props_reference.mutability.is_some() {
        return Err(Error::new_spanned(
            &first.ty,
            "component props are immutable; mutate them through harness controls",
        ));
    }
    Ok(props_reference.elem.as_ref())
}

fn doc_block_module_path(renderer: &Path) -> syn::Result<Path> {
    let mut module = renderer.clone();
    let Some(last) = module.segments.last_mut() else {
        return Err(Error::new_spanned(
            renderer,
            "doc block renderer path cannot be empty",
        ));
    };
    last.ident = format_ident!("__hblank_doc_block_{}", last.ident);
    Ok(module)
}

fn fixture_module_path(fixture: &Path) -> syn::Result<Path> {
    let mut module = fixture.clone();
    let Some(last) = module.segments.last_mut() else {
        return Err(Error::new_spanned(fixture, "fixture path cannot be empty"));
    };
    last.ident = format_ident!("__hblank_fixture_{}", last.ident);
    Ok(module)
}

fn component_module_path(component: &Path) -> syn::Result<Path> {
    let mut module = component.clone();
    let Some(last) = module.segments.last_mut() else {
        return Err(Error::new_spanned(
            component,
            "component path cannot be empty",
        ));
    };
    last.ident = format_ident!("__hblank_component_{}", last.ident);
    Ok(module)
}

fn docs(attributes: &[Attribute]) -> String {
    attributes
        .iter()
        .filter_map(|attribute| {
            if !attribute.path().is_ident("doc") {
                return None;
            }
            let Meta::NameValue(name_value) = &attribute.meta else {
                return None;
            };
            let Expr::Lit(ExprLit {
                lit: Lit::Str(value),
                ..
            }) = &name_value.value
            else {
                return None;
            };
            Some(value.value().trim().to_owned())
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[derive(Default)]
struct FieldOptions {
    label: Option<String>,
    skip: bool,
    multiline: bool,
    min: Option<f64>,
    max: Option<f64>,
    step: Option<f64>,
    adapter: Option<Path>,
}

impl FieldOptions {
    const fn has_number_constraints(&self) -> bool {
        self.min.is_some() || self.max.is_some() || self.step.is_some()
    }
}

fn control_kind(ty: &Type, options: &FieldOptions) -> proc_macro2::TokenStream {
    let mut kind = options.adapter.as_ref().map_or_else(
        || quote!(<#ty as ::hblank::__private::ControlField>::KIND),
        |adapter| {
            quote!(
                <<#adapter as ::hblank::HblankControlAdapter<#ty>>::Value
                    as ::hblank::__private::ControlField>::KIND
            )
        },
    );
    if options.multiline {
        kind = quote!((#kind).multiline());
    }
    if options.has_number_constraints() {
        let min = option_f64(options.min);
        let max = option_f64(options.max);
        let step = options.step.unwrap_or(1.0);
        kind = quote! {
            (#kind).constrained(::hblank::NumberConstraints {
                min: #min,
                max: #max,
                step: #step,
            })
        };
    }
    kind
}

fn control_accessors(
    ty: &Type,
    ident: &syn::Ident,
    id: &str,
    definition: &proc_macro2::TokenStream,
    adapter: Option<&Path>,
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
    adapter.map_or_else(
        || {
            (
                quote! {
                    #id => Some(
                        <#ty as ::hblank::__private::ControlField>::to_control_value(&self.#ident)
                    )
                },
                quote! {
                    #id => {
                        let definition = #definition;
                        definition.validate(&value)?;
                        <#ty as ::hblank::__private::ControlField>::set_control_value(
                            &mut self.#ident,
                            #id,
                            value,
                        )
                    }
                },
            )
        },
        |adapter| {
            (
                quote! {
                    #id => {
                        let control = <#adapter as ::hblank::HblankControlAdapter<#ty>>::to_control(
                            &self.#ident,
                        );
                        Some(
                            <<#adapter as ::hblank::HblankControlAdapter<#ty>>::Value
                                as ::hblank::__private::ControlField>::to_control_value(&control)
                        )
                    }
                },
                quote! {
                    #id => {
                        let definition = #definition;
                        definition.validate(&value)?;
                        let mut control =
                            <#adapter as ::hblank::HblankControlAdapter<#ty>>::to_control(
                                &self.#ident,
                            );
                        <<#adapter as ::hblank::HblankControlAdapter<#ty>>::Value
                            as ::hblank::__private::ControlField>::set_control_value(
                                &mut control,
                                #id,
                                value,
                            )?;
                        <#adapter as ::hblank::HblankControlAdapter<#ty>>::apply_control(
                            &mut self.#ident,
                            control,
                        );
                        Ok(())
                    }
                },
            )
        },
    )
}

fn field_options(attributes: &[Attribute]) -> syn::Result<FieldOptions> {
    let mut options = FieldOptions::default();
    for attribute in attributes {
        if !attribute.path().is_ident("hblank") {
            continue;
        }
        attribute.parse_nested_meta(|meta| {
            if meta.path.is_ident("label") {
                let value: LitStr = meta.value()?.parse()?;
                options.label = Some(value.value());
            } else if meta.path.is_ident("skip") {
                options.skip = true;
            } else if meta.path.is_ident("multiline") {
                options.multiline = true;
            } else if meta.path.is_ident("min") {
                options.min = Some(parse_number(meta.value()?.parse()?)?);
            } else if meta.path.is_ident("max") {
                options.max = Some(parse_number(meta.value()?.parse()?)?);
            } else if meta.path.is_ident("step") {
                options.step = Some(parse_number(meta.value()?.parse()?)?);
            } else if meta.path.is_ident("adapter") {
                options.adapter = Some(meta.value()?.parse()?);
            } else {
                return Err(
                    meta.error("expected one of: label, skip, multiline, min, max, step, adapter")
                );
            }
            Ok(())
        })?;
    }
    if let Some(step) = options.step
        && (!step.is_finite() || step <= 0.0)
    {
        return Err(Error::new(
            proc_macro2::Span::call_site(),
            "control step must be finite and greater than zero",
        ));
    }
    if options.min.is_some_and(|value| !value.is_finite())
        || options.max.is_some_and(|value| !value.is_finite())
    {
        return Err(Error::new(
            proc_macro2::Span::call_site(),
            "control bounds must be finite",
        ));
    }
    if let (Some(min), Some(max)) = (options.min, options.max)
        && min > max
    {
        return Err(Error::new(
            proc_macro2::Span::call_site(),
            "control min cannot exceed max",
        ));
    }
    Ok(options)
}

fn parse_number(expression: Expr) -> syn::Result<f64> {
    match expression {
        Expr::Lit(ExprLit {
            lit: Lit::Int(value),
            ..
        }) => value.base10_parse(),
        Expr::Lit(ExprLit {
            lit: Lit::Float(value),
            ..
        }) => value.base10_parse(),
        Expr::Unary(unary) if matches!(unary.op, syn::UnOp::Neg(_)) => {
            Ok(-parse_number(*unary.expr)?)
        }
        expression => Err(Error::new_spanned(expression, "expected a numeric literal")),
    }
}

fn option_f64(value: Option<f64>) -> proc_macro2::TokenStream {
    value.map_or_else(|| quote!(None), |value| quote!(Some(#value)))
}

fn field_label(attributes: &[Attribute]) -> syn::Result<Option<String>> {
    Ok(field_options(attributes)?.label)
}

fn humanize(identifier: &str) -> String {
    let mut output = String::with_capacity(identifier.len() + 4);
    let mut previous_lowercase = false;
    for (index, character) in identifier.chars().enumerate() {
        if character == '_' || character == '-' {
            if !output.ends_with(' ') {
                output.push(' ');
            }
            previous_lowercase = false;
            continue;
        }
        if character.is_uppercase() && previous_lowercase {
            output.push(' ');
        }
        if index == 0 {
            output.extend(character.to_uppercase());
        } else {
            output.push(character);
        }
        previous_lowercase = character.is_lowercase();
    }
    output
}