guinea-macros 0.13.2

guinea's macros: actor!, feature!, #[handler], routes! and the backends' #[page]
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
use proc_macro::TokenStream as TokenStream1;
use proc_macro2::{Ident, TokenStream};
use proc_macro_crate::{FoundCrate, crate_name};
use quote::{format_ident, quote, quote_spanned};

use guinea_route_dsl::{Segment, parse_pattern, type_ident};

fn guinea_crate_path() -> proc_macro2::TokenStream {
    match crate_name("guinea") {
        Ok(FoundCrate::Itself) => quote!(crate),
        Ok(FoundCrate::Name(name)) => {
            let ident = syn::Ident::new(&name, proc_macro2::Span::call_site());
            quote!(::#ident)
        }
        Err(_) => quote!(::guinea),
    }
}

/// Where the router's own types live.
///
/// An application usually reaches them through the facade, but a backend crate
/// depends on `guinea-router` directly and has no facade at all - and the
/// generated `RouteChain` impl has to name the same types either way.
/// Where a segment was listed, as the line `ty` was written on: `file!` and
/// friends expand wherever their tokens are spanned.
fn declared_at(ty: &syn::Type) -> TokenStream {
    let gc = crate::handler::guinea_core_crate_path();

    quote_spanned! {syn::spanned::Spanned::span(ty)=>
        #gc::actor::shape::Declared {
            file: ::core::file!(),
            line: ::core::line!(),
            column: ::core::column!(),
            crate_dir: ::core::env!("CARGO_MANIFEST_DIR"),
        }
    }
}

fn router_path(guinea: &TokenStream) -> TokenStream {
    match crate_name("guinea-router") {
        Ok(FoundCrate::Itself) => quote!(crate::router),
        Ok(FoundCrate::Name(name)) => {
            let ident = syn::Ident::new(&name, proc_macro2::Span::call_site());
            quote!(::#ident::router)
        }
        Err(_) => quote!(#guinea::router),
    }
}

/// Where [`LinkValue`](guinea_router::link::LinkValue) lives. Same reason as
/// [`router_path`].
fn link_path(guinea: &TokenStream) -> TokenStream {
    module_of_router(guinea, "link")
}

/// Where the enter-guard machinery lives.
fn enter_path(guinea: &TokenStream) -> TokenStream {
    module_of_router(guinea, "enter")
}

/// Where the two halves of `restorable` live.
fn restore_path(guinea: &TokenStream) -> TokenStream {
    module_of_router(guinea, "restore")
}

fn module_of_router(guinea: &TokenStream, module: &str) -> TokenStream {
    let module = syn::Ident::new(module, proc_macro2::Span::call_site());
    match crate_name("guinea-router") {
        Ok(FoundCrate::Itself) => quote!(crate::#module),
        Ok(FoundCrate::Name(name)) => {
            let ident = syn::Ident::new(&name, proc_macro2::Span::call_site());
            quote!(::#ident::#module)
        }
        Err(_) => quote!(#guinea::#module),
    }
}

/// Where the feature layer's own types live.
///
/// Same reason as [`router_path`]: an application reaches them through the
/// facade, a backend crate depends on `guinea-app` directly and has no facade
/// at all, and the generated `Segment` impls have to name them either way.
fn feature_path(guinea: &TokenStream) -> TokenStream {
    match crate_name("guinea-app") {
        Ok(FoundCrate::Itself) => quote!(crate::feature),
        Ok(FoundCrate::Name(name)) => {
            let ident = syn::Ident::new(&name, proc_macro2::Span::call_site());
            quote!(::#ident::feature)
        }
        Err(_) => quote!(#guinea::feature),
    }
}

/// The module a backend keeps `segment_entry`/`layout_entry` in: the parent of
/// the backend type, since it lives next to what it builds for.
fn backend_module(ty: &syn::Type) -> TokenStream {
    let syn::Type::Path(path) = ty else {
        panic!("routes!: backend must be a path, like `guinea_ratatui::Tui`");
    };

    let mut module = path.path.clone();
    module.segments.pop();
    let module: Vec<syn::PathSegment> = module.segments.into_iter().collect();
    if module.is_empty() {
        panic!("routes!: backend needs the module too, as in `guinea_ratatui::Tui`");
    }
    quote!(#(#module)::*)
}

/// `Debug` and `PartialEq` for a route enum, over its identity fields only.
///
/// A route with a `~` field cannot derive either: a channel has no equality
/// and often no `Debug`. Written out, both read the identity and skip the
/// payload - which is the same statement the router makes when it asks whether
/// this is still the same route.
fn identity_impls(enum_ident: &Ident, leaves: &[guinea_route_dsl::Leaf]) -> TokenStream {
    let eq_arms = leaves.iter().map(|leaf| {
        let variant = type_ident(&leaf.ty);
        let kept: Vec<&Ident> = leaf
            .fields
            .iter()
            .filter(|field| field.identity)
            .map(|field| &field.name)
            .collect();
        let theirs: Vec<Ident> = kept.iter().map(|name| format_ident!("__other_{}", name)).collect();

        quote! {
            (
                #enum_ident::#variant { #(#kept,)* .. },
                #enum_ident::#variant { #(#kept: #theirs,)* .. },
            ) => #(#kept == #theirs &&)* true
        }
    });

    let debug_arms = leaves.iter().map(|leaf| {
        let variant = type_ident(&leaf.ty);
        let kept: Vec<&Ident> = leaf
            .fields
            .iter()
            .filter(|field| field.identity)
            .map(|field| &field.name)
            .collect();
        let labels = kept.iter().map(|name| name.to_string());
        let title = variant.to_string();
        let whole = leaf.fields.len() == kept.len();
        let finish = if whole {
            quote!(.finish())
        } else {
            quote!(.finish_non_exhaustive())
        };

        quote! {
            #enum_ident::#variant { #(#kept,)* .. } => f
                .debug_struct(#title)
                #(.field(#labels, #kept))*
                #finish
        }
    });

    quote! {
        impl ::std::cmp::PartialEq for #enum_ident {
            fn eq(&self, other: &Self) -> bool {
                match (self, other) {
                    #(#eq_arms,)*
                    _ => false,
                }
            }
        }

        impl ::std::fmt::Debug for #enum_ident {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                match self {
                    #(#debug_arms),*
                }
            }
        }
    }
}

/// One struct per segment, carrying exactly what it captured.
///
/// Derived when every field has an identity, hand-written when one does not.
/// The hand-written half is the whole point of `~`: a payload that cannot be
/// compared or printed would otherwise force `PartialEq` and `Debug` on a
/// channel.
fn params_struct(name: &Ident, fields: &[guinea_route_dsl::Field]) -> TokenStream {
    let defs = fields.iter().map(|field| {
        let (name, ty) = (&field.name, &field.ty);
        quote! { pub #name: #ty }
    });

    if fields.is_empty() {
        return quote! {
            #[derive(Clone, Debug, Default, PartialEq)]
            pub struct #name {}
        };
    }

    if fields.iter().all(|field| field.identity) {
        return quote! {
            #[derive(Clone, Debug, PartialEq)]
            pub struct #name { #(#defs),* }
        };
    }

    let kept: Vec<&Ident> = fields
        .iter()
        .filter(|field| field.identity)
        .map(|field| &field.name)
        .collect();
    let labels = kept.iter().map(|name| name.to_string());
    let title = name.to_string();

    quote! {
        #[derive(Clone)]
        pub struct #name { #(#defs),* }

        impl ::std::fmt::Debug for #name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                f.debug_struct(#title)
                    #(.field(#labels, &self.#kept))*
                    .finish_non_exhaustive()
            }
        }

        impl ::std::cmp::PartialEq for #name {
            /// Never, and deliberately.
            ///
            /// The router's one question about a capture is whether it is
            /// still the same one. Part of this one was thrown away rather
            /// than kept, so the honest answer is that it cannot be told - and
            /// "cannot be told" has to mean "reinstall", because the
            /// alternative is a segment quietly holding a channel its caller
            /// replaced.
            fn eq(&self, _other: &Self) -> bool {
                false
            }
        }
    }
}

fn joined<E: std::fmt::Display>(errors: &[E]) -> String {
    errors
        .iter()
        .map(|error| error.to_string())
        .collect::<Vec<_>>()
        .join("\n")
}

pub fn routes_impl(input: TokenStream1) -> TokenStream1 {
    let tree = guinea_route_dsl::parse(input.into());
    let enum_ident = tree.name.clone();
    let leaves = tree.leaves();

    // All of them, not the first: a tree whose captures drifted from its
    // fields usually drifted in several places at once, and reporting one per
    // build turns one edit into several.
    let field_errors = guinea_route_dsl::matcher::check_fields(&leaves);
    if !field_errors.is_empty() {
        panic!("{}", joined(&field_errors));
    }

    let guard_errors = guinea_route_dsl::check_guards(&tree);
    if !guard_errors.is_empty() {
        panic!("{}", joined(&guard_errors));
    }

    let match_tree = match guinea_route_dsl::matcher::build(&leaves) {
        Ok(tree) => tree,
        Err(conflicts) => panic!("{}", joined(&conflicts)),
    };

    let guinea = guinea_crate_path();
    let router = router_path(&guinea);
    let link_mod = link_path(&guinea);
    let enter = enter_path(&guinea);
    let restore_mod = restore_path(&guinea);

    let parse_fn = guinea_route_dsl::matcher::emit_parse(&match_tree, &enum_ident, &link_mod);

    let variant_idents: Vec<Ident> = leaves.iter().map(|l| type_ident(&l.ty)).collect();

    // Identity is generated code rather than a trait bound, which is what lets
    // a route carry something with none. Derived while every field has one,
    // written out when a `~` appears anywhere in the tree - the enum is one
    // item, so one loose field decides for all of it.
    let whole = leaves
        .iter()
        .all(|leaf| leaf.fields.iter().all(|field| field.identity));

    let (enum_derives, enum_impls) = if whole {
        (quote!(#[derive(Debug, PartialEq)]), quote!())
    } else {
        (quote!(), identity_impls(&enum_ident, &leaves))
    };

    let variant_defs = leaves.iter().zip(&variant_idents).map(|(leaf, ident)| {
        let field_defs = leaf.fields.iter().map(|field| {
            let (name, ty) = (&field.name, &field.ty);
            quote! { #name: #ty }
        });
        quote! { #ident { #(#field_defs),* } }
    });

    // One struct per page, carrying exactly what that page captured. This is
    // what reaches `install`, in place of the address it used to dig them out
    // of by position.
    let params_idents: Vec<Ident> = variant_idents
        .iter()
        .map(|ident| format_ident!("{}Params", ident))
        .collect();

    let params_defs = leaves
        .iter()
        .zip(&params_idents)
        .map(|(leaf, params)| params_struct(params, &leaf.fields));

    // A layout is handed what every page under it carries - derived, not
    // declared, so a layout can never ask for something one of its pages does
    // not have. See `RouteTree::layout_params`.
    let layouts = tree.layout_params();
    let layout_params_idents: Vec<Ident> = layouts
        .iter()
        .map(|layout| format_ident!("{}Params", type_ident(&layout.ty)))
        .collect();

    // A layout's own are always identity - `layout_params` leaves `~` fields
    // out, since a comparison is the only reason they exist.
    let layout_params_defs = layouts
        .iter()
        .zip(&layout_params_idents)
        .map(|(layout, params)| params_struct(params, &layout.fields));

    // In chain order: each ancestor layout, then the leaf's own.
    let params_arms = leaves
        .iter()
        .zip(&variant_idents)
        .zip(&params_idents)
        .map(|((leaf, ident), params)| {
            let names: Vec<&Ident> = leaf.fields.iter().map(|field| &field.name).collect();

            let ancestors = leaf.ancestors.iter().map(|ancestor| {
                let position = layouts
                    .iter()
                    .position(|layout| type_ident(&layout.ty) == type_ident(ancestor))
                    .expect("every ancestor of a leaf is a layout of this tree");
                let params = &layout_params_idents[position];
                let taken = layouts[position]
                    .fields
                    .iter()
                    .map(|field| {
                        let name = &field.name;
                        quote! { #name: #name.clone() }
                    });

                quote! {
                    ::std::boxed::Box::new(#params { #(#taken),* })
                        as ::std::boxed::Box<dyn ::std::any::Any>
                }
            });

            quote! {
                #enum_ident::#ident { #(#names),* } => vec![
                    #(#ancestors,)*
                    ::std::boxed::Box::new(#params { #(#names: #names.clone()),* })
                        as ::std::boxed::Box<dyn ::std::any::Any>
                ]
            }
        });

    // `restorable` is the one tier that makes the compiler prove something,
    // and the proof is the code itself: a field that does not survive the
    // round trip fails to build. `quote_spanned!` puts that failure on the
    // field's own declaration rather than somewhere inside an expansion.
    let restorable = leaves.iter().any(|leaf| leaf.restorable);

    let save_arms = leaves.iter().zip(&variant_idents).map(|(leaf, ident)| {
        if !leaf.restorable {
            return quote! { #enum_ident::#ident { .. } => None };
        }

        let names: Vec<&Ident> = leaf.fields.iter().map(|field| &field.name).collect();
        let route = ident.to_string();
        let writes = leaf.fields.iter().map(|field| {
            let name = &field.name;
            let key = name.to_string();
            quote_spanned! {name.span()=>
                __saving.field(#key, #name)?;
            }
        });

        quote! {
            #enum_ident::#ident { #(#names),* } => {
                let mut __saving = #restore_mod::Saving::new(#route);
                #(#writes)*
                __saving.finish()
            }
        }
    });

    let restore_arms = leaves
        .iter()
        .zip(&variant_idents)
        .filter(|(leaf, _)| leaf.restorable)
        .map(|(leaf, ident)| {
            let route = ident.to_string();
            let reads = leaf.fields.iter().map(|field| {
                let name = &field.name;
                let key = name.to_string();
                quote_spanned! {name.span()=>
                    #name: __fields.field(#key)?
                }
            });

            quote! {
                #route => Some(#enum_ident::#ident { #(#reads),* })
            }
        });

    let link_arms = leaves.iter().zip(&variant_idents).map(|(leaf, ident)| {
        let Some(link) = &leaf.link else {
            return quote! { #enum_ident::#ident { .. } => None };
        };

        let field_pats: Vec<&Ident> = leaf.fields.iter().map(|field| &field.name).collect();
        let parts = parse_pattern(link).into_iter().map(|seg| match seg {
            Segment::Literal(lit) => quote! { #lit.to_string() },
            Segment::Capture(name) => {
                let field = format_ident!("{}", name);
                quote! { #link_mod::LinkValue::encode(#field) }
            }
        });

        quote! {
            #enum_ident::#ident { #(#field_pats),* } => {
                let parts: Vec<String> = vec![#(#parts),*];
                Some(format!("/{}", parts.join("/")))
            }
        }
    });

    // The whole external surface: what an installer registers and what a
    // committed manifest is diffed against.
    //
    // The capture's type is written as `LinkValue::NAME` rather than as the
    // declaration spelled it, so `type Context = String` renders as `String`
    // and renaming an alias is not a changed address.
    let tree_name = enum_ident.to_string();
    let declared_links = leaves.iter().filter_map(|leaf| {
        let link = leaf.link.as_ref()?;
        let route = type_ident(&leaf.ty).to_string();
        let guard_names = leaf.guards.iter().map(|ty| type_ident(ty).to_string());
        let leaf_restorable = leaf.restorable;

        // In the order the path names them, which is how the address reads.
        let captures = parse_pattern(link).into_iter().filter_map(|seg| {
            let Segment::Capture(name) = seg else {
                return None;
            };
            let ty = leaf
                .fields
                .iter()
                .find(|field| field.name == name)
                .map(|field| &field.ty)
                .expect("check_fields has passed, so every capture has a field");

            quote! {
                #link_mod::Capture {
                    name: #name,
                    ty: <#ty as #link_mod::LinkValue>::NAME,
                }
            }
            .into()
        });

        Some(quote! {
            #link_mod::DeepLink {
                tree: #tree_name,
                route: #route,
                path: #link,
                captures: &[#(#captures),*],
                guards: &[#(#guard_names),*],
                restorable: #leaf_restorable,
            }
        })
    });

    // A name for every route, addressable or not - navigation hooks want to
    // say where the application went, and most routes have no address to say
    // it with.
    let name_arms = variant_idents.iter().map(|ident| {
        let name = ident.to_string();
        quote! { #enum_ident::#ident { .. } => #name }
    });

    // Default to the facade's backend, so an application that has only one
    // never mentions it.
    let (backend_ty, backend_mod) = match &tree.backend {
        Some(ty) => (quote!(#ty), backend_module(ty)),
        None => (quote!(#guinea::Backend), quote!(#guinea::backend)),
    };

    let chain_consts = leaves.iter().zip(&variant_idents).map(|(leaf, ident)| {
        let const_name = format_ident!("__routes_chain_{}_{}", enum_ident, ident);
        let leaf_ty = &leaf.ty;
        let ancestor_entries = leaf.ancestors.iter().map(|ty| {
            let declared = declared_at(ty);
            quote! { #backend_mod::layout_entry::<#ty>().at(#declared) }
        });
        let len = leaf.ancestors.len() + 1;
        let declared = declared_at(leaf_ty);
        quote! {
            #[allow(non_upper_case_globals)]
            const #const_name: [#router::SegmentEntry<#backend_ty>; #len] = [
                #(#ancestor_entries,)*
                #backend_mod::segment_entry::<#leaf_ty>().at(#declared),
            ];
        }
    });

    // Where each segment sits, so the compiler can answer what it may read.
    // The macro knows one half - who is above whom - and the author declares
    // the other: `Installs` here, `Exports` on each feature.
    let feature = feature_path(&guinea);
    let above = |ancestors: &[syn::Type]| {
        // Innermost first, as a cons list, so the search reads the way the
        // runtime one used to walk.
        ancestors.iter().rev().fold(quote!(()), |tail, ty| {
            quote! { (#ty, #tail) }
        })
    };

    let mut placements: Vec<TokenStream> = Vec::new();
    for leaf in &leaves {
        let ty = &leaf.ty;
        let above = above(&leaf.ancestors);
        placements.push(quote! {
            impl #feature::Segment for #ty {
                type Installs = <#ty as #backend_mod::Page>::Installs;
                type Above = #above;
            }
        });
    }

    for layout in &layouts {
        let ty = &layout.ty;
        // A layout appears once per tree, so the first leaf that sits under it
        // settles what is above it.
        let ancestors = leaves
            .iter()
            .find_map(|leaf| {
                let at = leaf
                    .ancestors
                    .iter()
                    .position(|a| type_ident(a) == type_ident(ty))?;
                Some(leaf.ancestors[..at].to_vec())
            })
            .unwrap_or_default();
        let above = above(&ancestors);
        placements.push(quote! {
            impl #feature::Segment for #ty {
                type Installs = <#ty as #backend_mod::Layout>::Installs;
                type Above = #above;
            }
        });
    }

    // One `const` per route rather than one shared list: the resolved set
    // differs per leaf, and a `const` keeps the guards where the entries are -
    // in static data, built once, with nothing to allocate at navigation time.
    let guard_consts = leaves.iter().zip(&variant_idents).map(|(leaf, ident)| {
        let const_name = format_ident!("__routes_guards_{}_{}", enum_ident, ident);
        let len = leaf.guards.len();
        let stands = leaf.guards.iter().map(|ty| {
            quote! {
                &const { #enter::Stands::<#ty>(::std::marker::PhantomData) }
            }
        });

        quote! {
            #[allow(non_upper_case_globals)]
            const #const_name: [&'static dyn #enter::EnterGuard; #len] = [#(#stands),*];
        }
    });

    let guard_arms = leaves.iter().zip(&variant_idents).map(|(_leaf, ident)| {
        let const_name = format_ident!("__routes_guards_{}_{}", enum_ident, ident);
        quote! { #enum_ident::#ident { .. } => &#const_name }
    });

    let chain_arms = leaves.iter().zip(&variant_idents).map(|(_leaf, ident)| {
        let const_name = format_ident!("__routes_chain_{}_{}", enum_ident, ident);
        quote! { #enum_ident::#ident { .. } => &#const_name }
    });

    let expanded = quote! {
        #[derive(Clone)]
        #enum_derives
        pub enum #enum_ident {
            #(#variant_defs),*
        }

        #enum_impls

        impl #enum_ident {
            /// The address this route answers to, when it agreed to have one.
            pub fn link(&self) -> Option<String> {
                match self {
                    #(#link_arms),*
                }
            }

            /// Every address the application answers to. What an installer
            /// registers, and what a committed manifest is diffed against.
            pub fn deep_links() -> &'static [#link_mod::DeepLink] {
                &[#(#declared_links),*]
            }

            /// What to call this route in a log or a navigation hook. Present
            /// whether or not the route is addressable.
            pub fn name(&self) -> &'static str {
                match self {
                    #(#name_arms),*
                }
            }

            /// Whether any route in this tree survives a restart.
            ///
            /// The tree's answer, not this route's: an application asks it to
            /// decide whether to keep a saved route at all.
            pub const RESTORABLE: bool = #restorable;

            /// This route as text, when it agreed to survive a restart.
            ///
            /// Where the text goes is the application's business - the router
            /// has no more opinion about storage than it has about drawing.
            pub fn save(&self) -> Option<String> {
                match self {
                    #(#save_arms),*
                }
            }

            /// A route read back from what [`save`](Self::save) wrote.
            ///
            /// `None` for anything unrecognised, which includes what an older
            /// build wrote: a saved session outlives the version that made it,
            /// and a route that no longer exists is an ordinary thing to find
            /// rather than an error. The application falls back to wherever it
            /// starts.
            pub fn restore(text: &str) -> Option<Self> {
                let (__route, __fields) = #restore_mod::Restoring::open(text)?;
                match __route.as_str() {
                    #(#restore_arms,)*
                    _ => None,
                }
            }

            #parse_fn

            /// What each segment of this route's chain captured, in chain
            /// order. The router hands each one to its segment's `install`.
            pub fn params(&self) -> Vec<::std::boxed::Box<dyn ::std::any::Any>> {
                match self {
                    #(#params_arms),*
                }
            }
        }

        #(#params_defs)*

        #(#layout_params_defs)*

        #(#chain_consts)*

        #(#guard_consts)*

        #(#placements)*

        impl #router::RouteChain<#backend_ty> for #enum_ident {
            fn chain(&self) -> &'static [#router::SegmentEntry<#backend_ty>] {
                match self {
                    #(#chain_arms),*
                }
            }

            fn params(&self) -> Vec<::std::boxed::Box<dyn ::std::any::Any>> {
                #enum_ident::params(self)
            }

            fn name(&self) -> &'static str {
                #enum_ident::name(self)
            }

            fn describe(&self) -> String {
                format!("{self:?}")
            }

            fn link(&self) -> Option<String> {
                #enum_ident::link(self)
            }

            fn guards(&self) -> &'static [&'static dyn #enter::EnterGuard] {
                match self {
                    #(#guard_arms),*
                }
            }
        }

    };

    expanded.into()
}