bevy-react-macros 0.4.0

Proc-macros for bevy-react's typed React<->Bevy messaging.
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
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Proc-macro support for `bevy-react`.
//!
//! Provides [`react_message`], the attribute that turns a plain struct into a
//! registrable React message payload.
//
// TODO(review): these macros expand to `::serde::` and `::ts_rs::` paths, forcing every
// downstream consumer crate to add `serde` AND `ts_rs` as direct dependencies (works in-repo
// only because examples share the package's deps). Re-export both from the lib (e.g.
// `bevy_react::__private::{serde, ts_rs}`) and reference those paths so consumers need only
// `bevy_react` + `bevy`.

use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, LitStr, Type, parse_macro_input};

/// Turn a struct into a typed React message payload.
///
/// Applying `#[react_message]` derives `serde::Deserialize` and `ts_rs::TS` and
/// implements both `bevy::ecs::event::Event` and `bevy_react::ReactPayload`, so the
/// type can be registered with `App::add_react_handler` / `add_react_message`, routed
/// from a React `emit(name, value)` call, and exported to TypeScript via
/// `App::export_react_typescript`.
///
/// The `emit` name defaults to the struct name with its first letter lowercased
/// (`Count` → `"count"`, `PlayerScore` → `"playerScore"`); override it with
/// `#[react_message(name = "...")]`.
///
/// ```ignore
/// #[react_message]
/// struct Count(usize);            // name = "count"
///
/// #[react_message(name = "hp")]
/// struct Health(u32);             // name = "hp"
/// ```
#[proc_macro_attribute]
pub fn react_message(attr: TokenStream, item: TokenStream) -> TokenStream {
    let name_override = match parse_name_only_attr(attr, "react_message") {
        Ok(name) => name,
        Err(e) => return e.to_compile_error().into(),
    };

    let input = parse_macro_input!(item as DeriveInput);
    let PayloadParts {
        ident,
        impl_generics,
        ty_generics,
        where_clause,
        name,
    } = payload_parts(&input, name_override);

    quote! {
        #[derive(::serde::Deserialize, ::ts_rs::TS)]
        #input

        impl #impl_generics ::bevy::ecs::event::Event for #ident #ty_generics #where_clause {
            type Trigger<'a> = ::bevy::ecs::event::GlobalTrigger;
        }

        impl #impl_generics ::bevy_react::ReactPayload for #ident #ty_generics #where_clause {
            const NAME: &'static str = #name;
        }
    }
    .into()
}

/// Turn a struct into a typed React **request** payload (a React → Bevy call that
/// awaits a typed reply).
///
/// Derives `serde::Deserialize` + `ts_rs::TS` and implements
/// [`bevy_react::ReactRequest`], so the type can be registered with
/// `App::add_react_request_handler` and answered from a React `request(name, value)`
/// call. Observe `On<Request<T>>` and reply with `req.respond(value)`.
///
/// The `response` type is required and points at a type you define separately and
/// derive `serde::Serialize` + `ts_rs::TS` on. The `name` defaults to the struct
/// ident with its first letter lowercased; use a dotted name to get a nested proxy
/// (`#[react_request(name = "board.get", ...)]` → `bevy.board.get`).
///
/// ```ignore
/// #[react_request(name = "board.get", response = Board)]
/// struct BoardGet;                // unit payload → `bevy.board.get()` takes no args
///
/// #[react_request(name = "pieces.move", response = MoveStatus)]
/// struct PiecesMove { piece: String, to: String }
/// ```
#[proc_macro_attribute]
pub fn react_request(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut name_override: Option<String> = None;
    let mut response: Option<Type> = None;
    let arg_parser = syn::meta::parser(|meta| {
        if try_parse_name_arg(&meta, &mut name_override)? {
            Ok(())
        } else if meta.path.is_ident("response") {
            response = Some(meta.value()?.parse::<Type>()?);
            Ok(())
        } else {
            Err(meta.error(
                "unsupported `react_request` argument; expected `name = \"...\"` or `response = Type`",
            ))
        }
    });
    parse_macro_input!(attr with arg_parser);

    let response = match response {
        Some(ty) => ty,
        None => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                "`react_request` requires a `response = Type` argument",
            )
            .to_compile_error()
            .into();
        }
    };

    let input = parse_macro_input!(item as DeriveInput);
    let PayloadParts {
        ident,
        impl_generics,
        ty_generics,
        where_clause,
        name,
    } = payload_parts(&input, name_override);

    quote! {
        #[derive(::serde::Deserialize, ::ts_rs::TS)]
        #input

        impl #impl_generics ::bevy_react::ReactRequest for #ident #ty_generics #where_clause {
            const NAME: &'static str = #name;
            type Response = #response;
        }
    }
    .into()
}

/// Turn a struct into a typed React **event** payload (a Bevy → React broadcast).
///
/// Derives `serde::Serialize` + `ts_rs::TS` and implements
/// [`bevy_react::ReactEvent`]. Send it from a system with the `ReactEvents` param;
/// React listens with `bevy.on(name, cb)`. Register the type with
/// `App::add_react_event::<E>()` so it appears in the generated typings.
///
/// The `name` defaults to the struct ident with its first letter lowercased.
///
/// ```ignore
/// #[react_event(name = "user.disconnected")]
/// struct UserDisconnected { user_id: String }
/// ```
#[proc_macro_attribute]
pub fn react_event(attr: TokenStream, item: TokenStream) -> TokenStream {
    let name_override = match parse_name_only_attr(attr, "react_event") {
        Ok(name) => name,
        Err(e) => return e.to_compile_error().into(),
    };

    let input = parse_macro_input!(item as DeriveInput);
    let PayloadParts {
        ident,
        impl_generics,
        ty_generics,
        where_clause,
        name,
    } = payload_parts(&input, name_override);

    quote! {
        #[derive(::serde::Serialize, ::ts_rs::TS)]
        #input

        impl #impl_generics ::bevy_react::ReactEvent for #ident #ty_generics #where_clause {
            const NAME: &'static str = #name;
        }
    }
    .into()
}

/// Turn a named-field struct into a typed **custom filter** for the
/// layer-based `filter` style chain.
///
/// Derives `serde::Deserialize` — adding `#[serde(deny_unknown_fields)]`, so
/// unknown param keys reject like the built-ins; per-field `#[serde(default)]`
/// attributes you write are preserved — plus `ts_rs::TS`, and implements
/// `bevy_react::filters::ReactFilter`. Built-in filters stay hand-written
/// (they share canonical shader layouts); this macro is for custom filters,
/// which pack against their own shader.
///
/// Arguments:
///
/// - `name = "..."` (optional) — the wire name; defaults to the struct ident
///   with its first letter lowercased (`Glow` → `"glow"`).
/// - `shader = "path/to.wgsl"` (required) — loaded with `AssetServer::load`,
///   so a plain asset path relative to your assets dir; `embedded://` paths
///   also work verbatim.
/// - `outset = <number>` (optional, default `0.0`) — extra *logical* px the
///   effect bleeds outside the node's rect.
/// - `time = <bool>` (optional, default `false`) — a time-driven effect
///   re-renders its layer every frame.
///
/// The generated `pack()` fills the shader's `params` vec4 array
/// contiguously **in field declaration order**, mapped by field type. Params
/// pack into at most `MAX_FILTER_PARAM_VECS` (8) vec4s, enforced at resolve
/// time — an over-cap filter is rejected at runtime with a devtools warning,
/// not a compile error. Shader-side, the packed array is `uniforms.params`
/// via `#import bevy_react::filter` (see
/// `crates/core/src/layer/filter_prelude.wgsl` for the full binding
/// contract).
///
/// | field type           | packs as                          | components |
/// |----------------------|-----------------------------------|------------|
/// | `f32`                | scalar                            | 1          |
/// | `Vec2`/`Vec3`/`Vec4` | scalars                           | 2/3/4      |
/// | `[f32; 2..=4]`       | scalars                           | 2–4        |
/// | `Angle`              | radians (a bare wire number is degrees) | 1    |
/// | `Length`             | logical px (px-only; other units reject the filter use via `outset`/`resolve`) | 1 |
/// | `FilterColor`        | linear straight-alpha RGBA        | 4          |
///
/// A multi-component param that would straddle a vec4 boundary pads to the
/// next vec4 (the skipped components stay zero) — a `ParamSlot` never crosses
/// a vec4. Any other field type fails compilation with an error naming the
/// field and listing the supported types.
///
/// ```ignore
/// #[react_filter(name = "glow", shader = "shaders/glow.wgsl", outset = 4.0)]
/// struct GlowParams {
///     intensity: f32,          // params[0].x
///     direction: Vec2,         // params[0].yz
///     tint: FilterColor,       // params[1] (padded past params[0].w)
/// }
/// ```
#[proc_macro_attribute]
pub fn react_filter(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut name_override: Option<String> = None;
    let mut shader: Option<LitStr> = None;
    let mut outset: f32 = 0.0;
    let mut time = false;
    let arg_parser = syn::meta::parser(|meta| {
        if try_parse_name_arg(&meta, &mut name_override)? {
            Ok(())
        } else if meta.path.is_ident("shader") {
            shader = Some(meta.value()?.parse::<LitStr>()?);
            Ok(())
        } else if meta.path.is_ident("outset") {
            outset = match meta.value()?.parse::<syn::Lit>()? {
                syn::Lit::Float(f) => f.base10_parse()?,
                syn::Lit::Int(i) => i.base10_parse()?,
                other => {
                    return Err(syn::Error::new_spanned(
                        other,
                        "`outset` must be a number literal (logical px)",
                    ));
                }
            };
            Ok(())
        } else if meta.path.is_ident("time") {
            time = meta.value()?.parse::<syn::LitBool>()?.value;
            Ok(())
        } else {
            Err(meta.error(
                "unsupported `react_filter` argument; expected `name = \"...\"`, \
                 `shader = \"...\"`, `outset = <number>`, or `time = <bool>`",
            ))
        }
    });
    parse_macro_input!(attr with arg_parser);

    let Some(shader) = shader else {
        return syn::Error::new(
            proc_macro2::Span::call_site(),
            "`react_filter` requires a `shader = \"path/to.wgsl\"` argument",
        )
        .to_compile_error()
        .into();
    };

    let mut input = parse_macro_input!(item as DeriveInput);
    let name = name_override.unwrap_or_else(|| lower_first(&input.ident.to_string()));

    let syn::Data::Struct(data) = &mut input.data else {
        return syn::Error::new_spanned(&input.ident, "`react_filter` requires a struct")
            .to_compile_error()
            .into();
    };
    let syn::Fields::Named(fields) = &mut data.fields else {
        return syn::Error::new_spanned(
            &input.ident,
            "`react_filter` requires named fields (each field becomes a shader param)",
        )
        .to_compile_error()
        .into();
    };

    // Walk the fields in declaration order, assigning each a contiguous
    // no-straddle slot in the packed vec4 array and collecting the generated
    // slot/write/validation code (plus `#[ts(type)]` overrides for field
    // types without a `ts_rs::TS` impl — recorded here, applied only after
    // the walk proves error-free: the error path emits the struct without
    // `#[derive(TS)]`, where a pushed `#[ts]` attr would add a spurious
    // "cannot find attribute `ts`" error alongside the real one).
    let mut ts_overrides: Vec<(usize, &'static str)> = Vec::new();
    let mut errors: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut slots: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut writes: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut length_checks: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut vec_i = 0usize;
    let mut comp = 0usize;
    for (field_i, field) in fields.named.iter().enumerate() {
        let ident = field.ident.clone().expect("named field");
        let field_name = ident.to_string();
        let Some(param) = classify_filter_field(&field.ty) else {
            errors.push(
                syn::Error::new_spanned(
                    &field.ty,
                    format!(
                        "`react_filter` cannot pack field `{field_name}`: supported param types \
                         are f32, Vec2, Vec3, Vec4, [f32; 2..=4], Angle, Length, and FilterColor"
                    ),
                )
                .to_compile_error(),
            );
            continue;
        };
        let len = param.len();
        // No-straddle rule: a param that would cross a vec4 boundary pads to
        // the next vec4; the skipped components stay zero.
        if comp + len > 4 {
            vec_i += 1;
            comp = 0;
        }
        let (v, c) = (vec_i, comp);
        let kind = param.value_kind();
        slots.push(quote! {
            ::bevy_react::filters::ParamSlot {
                name: #field_name,
                kind: #kind,
                vec: #v,
                comp: #c,
                len: #len,
            }
        });
        match &param {
            FilterField::Scalar => writes.push(quote! { params[#v][#c] = self.#ident; }),
            FilterField::Vector(n) => {
                for (i, axis) in ["x", "y", "z", "w"].iter().take(*n).enumerate() {
                    let axis = syn::Ident::new(axis, proc_macro2::Span::call_site());
                    let ci = c + i;
                    writes.push(quote! { params[#v][#ci] = self.#ident.#axis; });
                }
            }
            FilterField::Array(n) => {
                for i in 0..*n {
                    let ci = c + i;
                    writes.push(quote! { params[#v][#ci] = self.#ident[#i]; });
                }
            }
            FilterField::Angle => {
                writes.push(quote! { params[#v][#c] = self.#ident.radians(); });
            }
            FilterField::Length => {
                // Logical px, same packing blur uses; the chain resolver
                // rewrites Length slots to physical px. The infallible pack
                // falls back to 0.0 — a non-px unit can't reach the shader
                // because the generated `outset`/`resolve` reject it first.
                writes.push(quote! {
                    params[#v][#c] = ::bevy_react::filters::length_logical_px(
                        #name, #field_name, self.#ident,
                    )
                    .unwrap_or(0.0);
                });
                length_checks.push(quote! {
                    ::bevy_react::filters::length_logical_px(#name, #field_name, self.#ident)?;
                });
            }
            FilterField::Color => {
                for i in 0..4usize {
                    let ci = c + i;
                    writes.push(quote! { params[#v][#ci] = self.#ident.0[#i]; });
                }
            }
        }
        comp += len;
        if comp == 4 {
            vec_i += 1;
            comp = 0;
        }
        if let Some(ts) = param.ts_override() {
            ts_overrides.push((field_i, ts));
        }
    }
    if errors.is_empty() {
        // Only a fully valid struct gets the `#[ts(type)]` overrides — the
        // emitted struct below carries the `#[derive(TS)]` they need.
        for (field_i, ts) in ts_overrides {
            fields.named[field_i]
                .attrs
                .push(syn::parse_quote!(#[ts(type = #ts)]));
        }
    } else {
        // Emit the struct exactly as written alongside the errors so
        // downstream code still sees the type, without a half-generated
        // `ReactFilter` impl.
        return quote! { #input #(#errors)* }.into();
    }
    let total_vecs = if comp == 0 { vec_i } else { vec_i + 1 };

    let ident = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    // Overriding `resolve` only matters when there are `Length` params to
    // validate; otherwise the trait default (the same body) applies.
    let resolve_override = (!length_checks.is_empty()).then(|| {
        quote! {
            fn resolve(
                &self,
                assets: &::bevy::asset::AssetServer,
            ) -> ::std::result::Result<
                ::std::vec::Vec<::bevy_react::filters::ResolvedFilterPass>,
                ::std::string::String,
            > {
                #(#length_checks)*
                ::bevy_react::filters::resolve_single_pass(self, assets)
            }
        }
    });

    quote! {
        #[derive(::serde::Deserialize, ::ts_rs::TS)]
        #[serde(deny_unknown_fields)]
        #input

        impl #impl_generics ::bevy_react::filters::ReactFilter for #ident #ty_generics #where_clause {
            const NAME: &'static str = #name;
            const USES_TIME: bool = #time;

            fn shader(
                assets: &::bevy::asset::AssetServer,
            ) -> ::bevy::asset::Handle<::bevy::shader::Shader> {
                assets.load(#shader)
            }

            fn outset(&self) -> ::std::result::Result<f32, ::std::string::String> {
                #(#length_checks)*
                ::std::result::Result::Ok(#outset)
            }

            fn pack(
                &self,
            ) -> (
                ::std::vec::Vec<::bevy::math::Vec4>,
                ::std::sync::Arc<[::bevy_react::filters::ParamSlot]>,
            ) {
                static LAYOUT: ::std::sync::LazyLock<
                    ::std::sync::Arc<[::bevy_react::filters::ParamSlot]>,
                > = ::std::sync::LazyLock::new(|| {
                    ::std::sync::Arc::from(::std::vec![#(#slots),*])
                });
                #[allow(unused_mut)]
                let mut params = ::std::vec![::bevy::math::Vec4::ZERO; #total_vecs];
                #(#writes)*
                (params, LAYOUT.clone())
            }

            #resolve_override
        }
    }
    .into()
}

/// How one `react_filter` param field packs, keyed off its declared type's
/// last path segment (or `[f32; N]` array shape).
enum FilterField {
    /// `f32`.
    Scalar,
    /// `Vec2`/`Vec3`/`Vec4` (2–4 scalar components).
    Vector(usize),
    /// `[f32; N]`, `N` in `2..=4`.
    Array(usize),
    /// `Angle` — packs radians.
    Angle,
    /// `Length` — packs logical px (px-only; validated in `outset`/`resolve`).
    Length,
    /// `FilterColor` — packs linear RGBA across 4 components.
    Color,
}

impl FilterField {
    /// Packed component count.
    fn len(&self) -> usize {
        match self {
            Self::Scalar | Self::Angle | Self::Length => 1,
            Self::Vector(n) | Self::Array(n) => *n,
            Self::Color => 4,
        }
    }

    /// The `ValueKind` tokens for this param's `ParamSlot`.
    fn value_kind(&self) -> proc_macro2::TokenStream {
        match self {
            Self::Scalar | Self::Vector(_) | Self::Array(_) => {
                quote!(::bevy_react::animations::ValueKind::Scalar)
            }
            Self::Angle => quote!(::bevy_react::animations::ValueKind::Angle),
            Self::Length => quote!(::bevy_react::animations::ValueKind::Length),
            Self::Color => quote!(::bevy_react::animations::ValueKind::Color),
        }
    }

    /// `#[ts(type = "...")]` override for field types without a `ts_rs::TS`
    /// impl (glam vectors, the wire-flexible `Angle`/`Length`). `f32`,
    /// `[f32; N]`, and `FilterColor` have real impls — no override.
    fn ts_override(&self) -> Option<&'static str> {
        match self {
            Self::Vector(2) => Some("[number, number]"),
            Self::Vector(3) => Some("[number, number, number]"),
            Self::Vector(4) => Some("[number, number, number, number]"),
            Self::Angle | Self::Length => Some("number | string"),
            _ => None,
        }
    }
}

/// Map a field's declared type to its packing, or `None` if unsupported.
/// Matches on the type path's last segment, so `bevy::math::Vec2` and a bare
/// `Vec2` both work.
fn classify_filter_field(ty: &Type) -> Option<FilterField> {
    match ty {
        Type::Path(p) => {
            let seg = p.path.segments.last()?;
            if !seg.arguments.is_empty() {
                return None;
            }
            match seg.ident.to_string().as_str() {
                "f32" => Some(FilterField::Scalar),
                "Vec2" => Some(FilterField::Vector(2)),
                "Vec3" => Some(FilterField::Vector(3)),
                "Vec4" => Some(FilterField::Vector(4)),
                "Angle" => Some(FilterField::Angle),
                "Length" => Some(FilterField::Length),
                "FilterColor" => Some(FilterField::Color),
                _ => None,
            }
        }
        Type::Array(a) => {
            let is_f32 = matches!(&*a.elem, Type::Path(p) if p.path.is_ident("f32"));
            let syn::Expr::Lit(lit) = &a.len else {
                return None;
            };
            let syn::Lit::Int(n) = &lit.lit else {
                return None;
            };
            let n = n.base10_parse::<usize>().ok()?;
            (is_f32 && (2..=4).contains(&n)).then_some(FilterField::Array(n))
        }
        _ => None,
    }
}

/// Consume a `name = "..."` argument if that's what `meta` holds; returns
/// whether it matched, so callers can chain their own arms after it.
fn try_parse_name_arg(
    meta: &syn::meta::ParseNestedMeta,
    out: &mut Option<String>,
) -> syn::Result<bool> {
    if meta.path.is_ident("name") {
        *out = Some(meta.value()?.parse::<LitStr>()?.value());
        Ok(true)
    } else {
        Ok(false)
    }
}

/// Parse an attribute argument list that accepts only `name = "..."` (the
/// `react_message`/`react_event` form; `react_request` adds a `response` arm).
fn parse_name_only_attr(attr: TokenStream, macro_name: &str) -> syn::Result<Option<String>> {
    let mut name_override: Option<String> = None;
    let parser = syn::meta::parser(|meta| {
        if try_parse_name_arg(&meta, &mut name_override)? {
            Ok(())
        } else {
            Err(meta.error(format!(
                "unsupported `{macro_name}` argument; expected `name = \"...\"`"
            )))
        }
    });
    syn::parse::Parser::parse(parser, attr)?;
    Ok(name_override)
}

/// The pieces every `react_*` macro pulls off the annotated struct.
struct PayloadParts<'a> {
    ident: &'a syn::Ident,
    impl_generics: syn::ImplGenerics<'a>,
    ty_generics: syn::TypeGenerics<'a>,
    where_clause: Option<&'a syn::WhereClause>,
    /// The wire name: the `name = "..."` override, or the struct ident with its
    /// first letter lowercased (`Count` → `"count"`).
    name: String,
}

fn payload_parts<'a>(input: &'a DeriveInput, name_override: Option<String>) -> PayloadParts<'a> {
    let ident = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    PayloadParts {
        ident,
        impl_generics,
        ty_generics,
        where_clause,
        name: name_override.unwrap_or_else(|| lower_first(&ident.to_string())),
    }
}

/// Lowercase only the first character of `s` (`Count` → `count`).
fn lower_first(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        Some(first) => first.to_lowercase().collect::<String>() + chars.as_str(),
        None => String::new(),
    }
}