anapao 0.2.0

Library for deterministic simulation tests and reproducible stochastic workflows
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
//! Declarative checked scenario authoring.

/// Authors a complete checked [`Scenario`](crate::types::Scenario) using symbolic node and edge
/// names.
///
/// The macro is an ergonomic front end for [`ScenarioBuilder`](crate::types::ScenarioBuilder):
/// captured expressions are evaluated once, identifiers come from the exact spelling of their
/// symbols, and whole-graph validation remains owned by `ScenarioBuilder::build`.
/// It returns `Result<Scenario, SetupError>`; malformed macro grammar is a compile-time error,
/// while graph and value errors remain recoverable [`SetupError`](crate::error::SetupError)s.
///
/// Invoke it as [`crate::scenario!`] or, after `use anapao::prelude::*`, as `scenario!`. The
/// deliberate public macro set contains this one macro: it does not add assertion, expectation,
/// configuration, or report helper macros. Normal associated functions (with `#[track_caller]`
/// where useful) are the preferred direction for future assertion ergonomics.
///
/// # Grammar
///
/// The canonical top-level order is fixed:
///
/// ```text
/// scenario! {
///     id: scenario_id_expr;
///     title: title_expr;                         // optional
///     description: description_expr;             // optional
///     tags [tag_expr, another_tag_expr,];        // optional
///     variables: variable_runtime_config_expr;   // optional
///     metadata { key_expr => value_expr, }       // optional
///     nodes { node declarations }
///     edges { edge declarations }
///     track [node_symbol, another_symbol,];      // optional
///     end end_condition;                         // repeated zero or more times
/// }
/// ```
///
/// `nodes` and `edges` are required. Sections and declarations use semicolons; property lists,
/// tag lists, metadata blocks, tracking lists, and `any`/`all` lists accept a trailing comma.
/// `id` accepts any expression that implements `TryInto<ScenarioId>`, including `&str`, `String`,
/// and `ScenarioId`. All other values below are ordinary expressions unless a form names a symbol.
///
/// ## Nodes
///
/// A declaration is one of `symbol: Source`, `symbol: Process`, `symbol: Sink`, `symbol: Gate`,
/// `symbol: Custom(family_expr)`, or `symbol: Family { properties }`. The complete family list is
/// `Source`, `Pool`, `Drain`, `SortingGate`, `TriggerGate`, `MixedGate`, `Converter`, `Trader`,
/// `Register`, `Delay`, `Queue`, `Process`, `Sink`, `Gate`, and `Custom(family_expr)`.
///
/// Every family accepts these common properties inside braces: `label: string_expr`,
/// `initial: f64_expr`, `tags [tag_expr,]`, and `metadata { key_expr => value_expr, }`. The
/// configured families also accept exactly one typed escape, `config: checked_config_expr`; it is
/// exclusive with all native config properties. Their complete native property sets are:
///
/// | Family | Native properties |
/// | --- | --- |
/// | `Pool` | `capacity: u64_expr` or `capacity: none`; `allow_negative_start: bool_expr`; `mode: NodeModeConfig`, or `trigger: TriggerMode` and/or `action: ActionMode` |
/// | `Drain` | `mode`, or `trigger` and/or `action` |
/// | `SortingGate` | `mode`, or `trigger` and/or `action` |
/// | `TriggerGate` | `mode`, or `trigger` and/or `action` |
/// | `MixedGate` | `mode`, or `trigger` and/or `action` |
/// | `Converter` | `ignore_disabled_inputs: bool_expr`; `mode`, or `trigger` and/or `action` |
/// | `Trader` | `ignore_disabled_inputs: bool_expr`; `mode`, or `trigger` and/or `action` |
/// | `Register` | `interactive: bool_expr`; `min_value: i64_expr` or `none`; `max_value: i64_expr` or `none` |
/// | `Delay` | `steps: u64_expr`; `mode`, or `trigger` and/or `action` |
/// | `Queue` | `capacity: u64_expr` or `none`; `release_per_step: u64_expr`; `mode`, or `trigger` and/or `action` |
///
/// Omitted config properties retain the checked type's default. `Delay.steps`, a present
/// `Queue.capacity`, and `Queue.release_per_step` must be positive and otherwise return the
/// existing [`SetupError`](crate::error::SetupError). `mode` cannot be combined with `trigger` or
/// `action`; `trigger` and `action` can be supplied independently. Duplicate, unknown, and
/// wrong-family properties are compile-time syntax errors.
///
/// ## Edges, transfers, and connections
///
/// An edge is `edge_symbol: from_symbol -> to_symbol => transfer` with an optional connection
/// suffix. The transfer is exactly one of `fixed(amount_expr)`,
/// `fraction(numerator_expr, denominator_expr)`, `remaining`,
/// `metric_scaled(node_symbol, factor_expr)`, `expression(formula_expr)`, or
/// `transfer(transfer_spec_expr)`.
///
/// With no suffix, the edge has the default resource connection. A resource suffix is
/// `resource { properties }`, where properties are `connection: ResourceConnection_expr` or
/// `token_size: u64_expr` (exclusive), plus optional `enabled: bool_expr` and
/// `metadata { key_expr => value_expr, }`. A present `token_size` must be positive. A state suffix
/// is `state { properties }`, where properties are `connection: StateConnection_expr` (exclusive
/// with the native connection properties), native `role: StateConnectionRole_expr`,
/// `formula: formula_expr`, `target: target_form`, and `resource_filter: filter_expr`, plus
/// optional `enabled: bool_expr` and `metadata { key_expr => value_expr, }`.
///
/// The four `target_form` values are `node`, `resource_connection(edge_symbol)`,
/// `state_connection(edge_symbol)`, and `formula(edge_symbol)`. All edge symbols are registered
/// before construction, so a state target may name an edge declared later.
///
/// ## Tracking and end conditions
///
/// Tracking is `track [node_symbol, another_node_symbol,]`. Each symbol becomes its node-backed
/// `MetricKey`. An `end` statement contains exactly one of:
///
/// ```text
/// max_steps(steps_expr)
/// metric_at_least(node_symbol, scaled_i64_expr)
/// metric_at_most(node_symbol, scaled_i64_expr)
/// node_at_least(node_symbol, scaled_i64_expr)
/// node_at_most(node_symbol, scaled_i64_expr)
/// any [end_condition, another_end_condition,]
/// all [end_condition, another_end_condition,]
/// condition(end_condition_spec_expr)
/// ```
///
/// Multiple `end` statements retain declaration order and become the checked builder's end list,
/// which keeps its established top-level OR semantics. Without an `end` statement, the builder's
/// default `MaxSteps(1)` remains. Thresholds are existing scaled `i64` values; the macro performs
/// no implicit floating-point scaling.
///
/// Node and edge declaration names are Rust identifiers. Their exact `stringify!` spelling is the
/// generated ID, and the node and edge namespaces are separate. Dynamic IDs remain available via
/// [`ScenarioBuilder`](crate::types::ScenarioBuilder). A tracked metric, `metric_scaled`, and
/// metric end conditions use the retained node symbol; unknown and duplicate references go to the
/// checked builder for its stable semantic diagnostic.
///
/// Symbols use their exact `stringify!` spelling as IDs. Node and edge symbols have separate
/// namespaces, so they may have the same spelling. A tracked metric is backed by the node symbol;
/// state targets may refer to edges declared later. Unknown and duplicate references are passed to
/// the checked builder, which supplies its stable semantic diagnostic.
///
/// Expansion uses `$crate` and absolute standard-library paths, so caller imports and a renamed
/// Cargo dependency do not affect it. Each captured expression is evaluated once. It introduces
/// no panic path: only the builder performs semantic validation. The macro produces the checked
/// value, not a serde format or a second validation path.
///
/// # Compatibility
///
/// In public 0.2, this grammar, symbol mapping, single-evaluation behavior, `Result<Scenario,
/// SetupError>` contract, and root/prelude invocation paths are compatibility promises. Keep
/// `ScenarioBuilder` for direct checked Rust authoring and `ScenarioSpec` followed by
/// `Scenario::try_from` for stable serde documents.
#[macro_export]
macro_rules! scenario {
    // Public entry. Section order is intentionally fixed so malformed input has a useful error.
    (
        id: $id:expr;
        $(title: $title:expr;)?
        $(description: $description:expr;)?
        $(tags [$($scenario_tag:expr),* $(,)?];)?
        $(variables: $variables:expr;)?
        $(metadata {$($scenario_key:expr => $scenario_value:expr),* $(,)?})?
        nodes {$($nodes:tt)*}
        edges {$($edges:tt)*}
        $($tail:tt)*
    ) => {{
        (|| -> ::std::result::Result<$crate::types::Scenario, $crate::error::SetupError> {
            let __anapao_id_value = $id;
            let __anapao_id: $crate::types::ScenarioId =
                ::std::convert::TryInto::<$crate::types::ScenarioId>::try_into(__anapao_id_value)
                    .map_err(|error| {
                    $crate::error::SetupError::InvalidParameter {
                        name: ::std::convert::From::from("id"),
                        reason: ::std::string::ToString::to_string(&error),
                    }
                    })?;

            // Each declaration owns one retained typed identity. Metrics are node-backed but use
            // their distinct public type. Vectors deliberately preserve duplicate declarations so
            // insertion, not a macro-side map, owns duplicate errors.
            let mut __anapao_node_ids: ::std::vec::Vec<(&str, $crate::types::NodeId)> =
                ::std::vec::Vec::new();
            let mut __anapao_metric_ids: ::std::vec::Vec<(&str, $crate::types::MetricKey)> =
                ::std::vec::Vec::new();
            $crate::scenario!(@__anapao_register_nodes
                __anapao_node_ids, __anapao_metric_ids; $($nodes)*
            );
            let mut __anapao_edge_ids: ::std::vec::Vec<(&str, $crate::types::EdgeId)> =
                ::std::vec::Vec::new();
            $crate::scenario!(@__anapao_register_edges __anapao_edge_ids; $($edges)*);

            let __anapao_node = |name: &str| {
                let mut __anapao_iter = __anapao_node_ids.iter();
                if let ::std::option::Option::Some((_, id)) = ::std::iter::Iterator::find(
                    &mut __anapao_iter,
                    |(candidate, _)| *candidate == name,
                )
                {
                    ::std::result::Result::Ok(::std::clone::Clone::clone(id))
                } else {
                    $crate::types::NodeId::new(name).map_err(|error| {
                        $crate::error::SetupError::InvalidParameter {
                            name: ::std::format!("nodes.{name}.id"),
                            reason: ::std::string::ToString::to_string(&error),
                        }
                    })
                }
            };
            let __anapao_metric = |name: &str| {
                let mut __anapao_iter = __anapao_metric_ids.iter();
                if let ::std::option::Option::Some((_, id)) = ::std::iter::Iterator::find(
                    &mut __anapao_iter,
                    |(candidate, _)| *candidate == name,
                )
                {
                    ::std::result::Result::Ok(::std::clone::Clone::clone(id))
                } else {
                    $crate::types::MetricKey::new(name).map_err(|error| {
                        $crate::error::SetupError::InvalidParameter {
                            name: ::std::format!("metrics.{name}.id"),
                            reason: ::std::string::ToString::to_string(&error),
                        }
                    })
                }
            };
            let __anapao_edge = |name: &str| {
                let mut __anapao_iter = __anapao_edge_ids.iter();
                if let ::std::option::Option::Some((_, id)) = ::std::iter::Iterator::find(
                    &mut __anapao_iter,
                    |(candidate, _)| *candidate == name,
                )
                {
                    ::std::result::Result::Ok(::std::clone::Clone::clone(id))
                } else {
                    $crate::types::EdgeId::new(name).map_err(|error| {
                        $crate::error::SetupError::InvalidParameter {
                            name: ::std::format!("edges.{name}.id"),
                            reason: ::std::string::ToString::to_string(&error),
                        }
                    })
                }
            };

            let mut __anapao_builder = $crate::types::ScenarioBuilder::new(__anapao_id);
            $(
                let __anapao_value = $title;
                __anapao_builder = __anapao_builder.with_title(__anapao_value);
            )?
            $(
                let __anapao_value = $description;
                __anapao_builder = __anapao_builder.with_description(__anapao_value);
            )?
            $($(
                let __anapao_value = $scenario_tag;
                __anapao_builder = __anapao_builder.with_tag(__anapao_value);
            )*)?
            $(
                let __anapao_value = $variables;
                __anapao_builder = __anapao_builder.with_variables(__anapao_value);
            )?
            $($(
                let __anapao_key = $scenario_key;
                let __anapao_value = $scenario_value;
                __anapao_builder =
                    __anapao_builder.with_metadata(__anapao_key, __anapao_value);
            )*)?

            $crate::scenario!(@__anapao_build_nodes
                __anapao_builder, __anapao_node; $($nodes)*
            );
            $crate::scenario!(@__anapao_build_edges
                __anapao_builder, __anapao_node, __anapao_metric, __anapao_edge; $($edges)*
            );
            let mut __anapao_ends: ::std::vec::Vec<$crate::types::EndConditionSpec> =
                ::std::vec::Vec::new();
            $crate::scenario!(@__anapao_tail
                __anapao_builder, __anapao_ends, __anapao_node, __anapao_metric; $($tail)*
            );
            if !__anapao_ends.is_empty() {
                __anapao_builder = __anapao_builder.with_end_conditions(__anapao_ends);
            }
            __anapao_builder.build()
        })()
    }};

    // Identity pre-registration. The declaration parsers below perform the syntax check too.
    (@__anapao_register_nodes $nodes:ident, $metrics:ident;) => {};
    (@__anapao_register_nodes $nodes:ident, $metrics:ident;
        $name:ident : Custom($family:expr) {$($props:tt)*}; $($rest:tt)*) => {
        $crate::scenario!(@__anapao_register_one_node $nodes, $metrics, $name);
        $crate::scenario!(@__anapao_register_nodes $nodes, $metrics; $($rest)*);
    };
    (@__anapao_register_nodes $nodes:ident, $metrics:ident;
        $name:ident : Custom($family:expr); $($rest:tt)*) => {
        $crate::scenario!(@__anapao_register_one_node $nodes, $metrics, $name);
        $crate::scenario!(@__anapao_register_nodes $nodes, $metrics; $($rest)*);
    };
    (@__anapao_register_nodes $nodes:ident, $metrics:ident;
        $name:ident : $family:ident {$($props:tt)*}; $($rest:tt)*) => {
        $crate::scenario!(@__anapao_register_one_node $nodes, $metrics, $name);
        $crate::scenario!(@__anapao_register_nodes $nodes, $metrics; $($rest)*);
    };
    (@__anapao_register_nodes $nodes:ident, $metrics:ident;
        $name:ident : $family:ident; $($rest:tt)*) => {
        $crate::scenario!(@__anapao_register_one_node $nodes, $metrics, $name);
        $crate::scenario!(@__anapao_register_nodes $nodes, $metrics; $($rest)*);
    };
    (@__anapao_register_one_node $nodes:ident, $metrics:ident, $name:ident) => {{
        let __anapao_name = ::std::stringify!($name);
        let __anapao_node_id = $crate::types::NodeId::new(__anapao_name).map_err(|error| {
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("nodes.{}.id", __anapao_name),
                reason: ::std::string::ToString::to_string(&error),
            }
        })?;
        let __anapao_metric_id = $crate::types::MetricKey::new(__anapao_name).map_err(|error| {
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("metrics.{}.id", __anapao_name),
                reason: ::std::string::ToString::to_string(&error),
            }
        })?;
        $nodes.push((__anapao_name, __anapao_node_id));
        $metrics.push((__anapao_name, __anapao_metric_id));
    }};
    (@__anapao_register_nodes $nodes:ident, $metrics:ident; $($bad:tt)+) => {
        ::std::compile_error!("anapao::scenario!: malformed node declaration")
    };

    (@__anapao_register_edges $edges:ident;) => {};
    (@__anapao_register_edges $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))? resource {$($props:tt)*}; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_register_one_edge $edges, $name);
        $crate::scenario!(@__anapao_register_edges $edges; $($rest)*);
    }};
    (@__anapao_register_edges $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))? state {$($props:tt)*}; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_register_one_edge $edges, $name);
        $crate::scenario!(@__anapao_register_edges $edges; $($rest)*);
    }};
    (@__anapao_register_edges $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))?; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_register_one_edge $edges, $name);
        $crate::scenario!(@__anapao_register_edges $edges; $($rest)*);
    }};
    (@__anapao_register_one_edge $edges:ident, $name:ident) => {{
        let __anapao_name = ::std::stringify!($name);
        let __anapao_id = $crate::types::EdgeId::new(__anapao_name).map_err(|error| {
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("edges.{}.id", __anapao_name),
                reason: ::std::string::ToString::to_string(&error),
            }
        })?;
        $edges.push((__anapao_name, __anapao_id));
    }};
    (@__anapao_register_edges $edges:ident; $($bad:tt)+) => {
        ::std::compile_error!("anapao::scenario!: malformed edge declaration")
    };

    // Node declarations and family constructors.
    (@__anapao_build_nodes $builder:ident, $node_id:ident;) => {};
    (@__anapao_build_nodes $builder:ident, $node_id:ident;
        $name:ident : Custom($family:expr) {$($props:tt)*}; $($rest:tt)*) => {{
        let __anapao_family = $family;
        $crate::scenario!(@__anapao_node Custom($name, __anapao_family), $builder, $node_id;
            $($props)*);
        $crate::scenario!(@__anapao_build_nodes $builder, $node_id; $($rest)*);
    }};
    (@__anapao_build_nodes $builder:ident, $node_id:ident;
        $name:ident : Custom($family:expr); $($rest:tt)*) => {{
        let __anapao_family = $family;
        $crate::scenario!(@__anapao_node Custom($name, __anapao_family), $builder, $node_id;);
        $crate::scenario!(@__anapao_build_nodes $builder, $node_id; $($rest)*);
    }};
    (@__anapao_build_nodes $builder:ident, $node_id:ident;
        $name:ident : $family:ident {$($props:tt)*}; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_node $family($name), $builder, $node_id; $($props)*);
        $crate::scenario!(@__anapao_build_nodes $builder, $node_id; $($rest)*);
    }};
    (@__anapao_build_nodes $builder:ident, $node_id:ident;
        $name:ident : $family:ident; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_node $family($name), $builder, $node_id;);
        $crate::scenario!(@__anapao_build_nodes $builder, $node_id; $($rest)*);
    }};

    // All node families funnel through one property collector. Config type inference is fixed by
    // the explicit annotation in each configured arm.
    (@__anapao_node Source($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_plain_node source, Source, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Process($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_plain_node process, Process, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Sink($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_plain_node sink, Sink, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Gate($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_plain_node gate, Gate, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Custom($name:ident, $family:ident), $builder:ident, $ids:ident; $($props:tt)*) => {{
        let mut __anapao_label: ::std::option::Option<::std::string::String> =
            ::std::option::Option::None;
        let mut __anapao_initial: ::std::option::Option<f64> = ::std::option::Option::None;
        let mut __anapao_tags: ::std::vec::Vec<::std::string::String> = ::std::vec::Vec::new();
        let mut __anapao_metadata: ::std::vec::Vec<(
            ::std::string::String, ::std::string::String
        )> = ::std::vec::Vec::new();
        let mut __anapao_unused = ();
        let mut __anapao_mode: $crate::types::NodeModeConfig =
            ::std::default::Default::default();
        let __anapao_node_name = ::std::stringify!($name);
        $crate::scenario!(@__anapao_node_props Plain, __anapao_unused, __anapao_mode, __anapao_node_name,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata; [];
            $($props)* ,);
        let mut __anapao_node = $crate::types::ScenarioNode::custom(
            $ids(::std::stringify!($name))?, $family
        );
        $crate::scenario!(@__anapao_apply_common __anapao_node,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata);
        $builder.insert_node(__anapao_node)?;
    }};
    (@__anapao_plain_node $ctor:ident, $family:ident, $name:ident,
        $builder:ident, $ids:ident; $($props:tt)*) => {{
        let mut __anapao_label: ::std::option::Option<::std::string::String> =
            ::std::option::Option::None;
        let mut __anapao_initial: ::std::option::Option<f64> = ::std::option::Option::None;
        let mut __anapao_tags: ::std::vec::Vec<::std::string::String> = ::std::vec::Vec::new();
        let mut __anapao_metadata: ::std::vec::Vec<(
            ::std::string::String, ::std::string::String
        )> = ::std::vec::Vec::new();
        let mut __anapao_unused = ();
        let mut __anapao_mode: $crate::types::NodeModeConfig =
            ::std::default::Default::default();
        let __anapao_node_name = ::std::stringify!($name);
        $crate::scenario!(@__anapao_node_props Plain, __anapao_unused, __anapao_mode, __anapao_node_name,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata; [];
            $($props)* ,);
        let mut __anapao_node =
            $crate::types::ScenarioNode::$ctor($ids(::std::stringify!($name))?);
        $crate::scenario!(@__anapao_apply_common __anapao_node,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata);
        $builder.insert_node(__anapao_node)?;
    }};

    (@__anapao_node Pool($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node PoolConfig, pool, Pool, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Drain($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node DrainConfig, drain, Drain, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node SortingGate($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node SortingGateConfig, sorting_gate, SortingGate, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node TriggerGate($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node TriggerGateConfig, trigger_gate, TriggerGate, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node MixedGate($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node MixedGateConfig, mixed_gate, MixedGate, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Converter($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node ConverterConfig, converter, Converter, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Trader($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node TraderConfig, trader, Trader, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Register($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node RegisterConfig, register, Register, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Delay($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node DelayConfig, delay, Delay, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node Queue($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        $crate::scenario!(@__anapao_config_node QueueConfig, queue, Queue, $name, $builder, $ids; $($props)*)
    };
    (@__anapao_node $family:ident($name:ident), $builder:ident, $ids:ident; $($props:tt)*) => {
        ::std::compile_error!("anapao::scenario!: unknown node family")
    };

    (@__anapao_config_node $config_ty:ident, $ctor:ident, $family:ident, $name:ident,
        $builder:ident, $ids:ident; $($props:tt)*) => {{
        let mut __anapao_config: $crate::types::$config_ty = ::std::default::Default::default();
        let mut __anapao_mode: $crate::types::NodeModeConfig =
            ::std::default::Default::default();
        let mut __anapao_label: ::std::option::Option<::std::string::String> =
            ::std::option::Option::None;
        let mut __anapao_initial: ::std::option::Option<f64> = ::std::option::Option::None;
        let mut __anapao_tags: ::std::vec::Vec<::std::string::String> = ::std::vec::Vec::new();
        let mut __anapao_metadata: ::std::vec::Vec<(
            ::std::string::String, ::std::string::String
        )> = ::std::vec::Vec::new();
        let __anapao_node_name = ::std::stringify!($name);
        $crate::scenario!(@__anapao_node_props $family, __anapao_config, __anapao_mode, __anapao_node_name,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata; [];
            $($props)* ,);
        let mut __anapao_node = $crate::types::ScenarioNode::$ctor(
            $ids(::std::stringify!($name))?, __anapao_config
        );
        $crate::scenario!(@__anapao_apply_common __anapao_node,
            __anapao_label, __anapao_initial, __anapao_tags, __anapao_metadata);
        $builder.insert_node(__anapao_node)?;
    }};

    (@__anapao_apply_common $node:ident, $label:ident, $initial:ident, $tags:ident, $metadata:ident) => {
        if let ::std::option::Option::Some(__anapao_value) = $label {
            $node = $node.with_label(__anapao_value);
        }
        if let ::std::option::Option::Some(__anapao_value) = $initial {
            $node = $node.with_initial_value(__anapao_value);
        }
        for __anapao_value in $tags {
            $node = $node.with_tag(__anapao_value);
        }
        for (__anapao_key, __anapao_value) in $metadata {
            $node = $node.with_metadata(__anapao_key, __anapao_value);
        }
    };

    // Common node properties.
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*];) => {};
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; , $($rest:tt)*) => {
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)*]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; label: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_label [$($seen)*]);
        let __anapao_value = $value;
        $label = ::std::option::Option::Some(::std::convert::Into::into(__anapao_value));
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* label]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; initial: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_initial [$($seen)*]);
        let __anapao_value = $value;
        $initial = ::std::option::Option::Some(__anapao_value);
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* initial]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; tags [$($value:expr),* $(,)?], $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_tags [$($seen)*]);
        $(
            let __anapao_value = $value;
            $tags.push(::std::convert::Into::into(__anapao_value));
        )*
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* tags]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; metadata {$($key:expr => $value:expr),* $(,)?}, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_metadata [$($seen)*]);
        $(
            let __anapao_key = $key;
            let __anapao_value = $value;
            $metadata.push((::std::convert::Into::into(__anapao_key),
                ::std::convert::Into::into(__anapao_value)));
        )*
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* metadata]; $($rest)*);
    };

    // Typed config is exclusive with every native config property.
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; config: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_family_config $family);
        $crate::scenario!(@__anapao_assert_config [$($seen)*]);
        $crate::scenario!(@__anapao_assert_no_native_config [$($seen)*]);
        let __anapao_value = $value;
        $config = __anapao_value;
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* config]; $($rest)*);
    };

    // Pool.
    (@__anapao_node_props Pool, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; capacity: none, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard capacity [$($seen)*]);
        $config = $config.without_capacity();
        $crate::scenario!(@__anapao_node_props Pool, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* capacity]; $($rest)*);
    };
    (@__anapao_node_props Pool, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; capacity: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard capacity [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_capacity(__anapao_value);
        $crate::scenario!(@__anapao_node_props Pool, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* capacity]; $($rest)*);
    };
    (@__anapao_node_props Pool, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; allow_negative_start: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard allow_negative_start [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_allow_negative_start(__anapao_value);
        $crate::scenario!(@__anapao_node_props Pool, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* allow_negative_start]; $($rest)*);
    };

    // Converter/trader and register fields.
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; ignore_disabled_inputs: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_family_converter_trader $family);
        $crate::scenario!(@__anapao_native_guard ignore_disabled_inputs [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_ignore_disabled_inputs(__anapao_value);
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* ignore_disabled_inputs]; $($rest)*);
    };
    (@__anapao_node_props Register, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; interactive: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard interactive [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_interactive(__anapao_value);
        $crate::scenario!(@__anapao_node_props Register, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* interactive]; $($rest)*);
    };
    (@__anapao_node_props Register, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; min_value: none, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard min_value [$($seen)*]);
        $config = $config.without_min_value();
        $crate::scenario!(@__anapao_node_props Register, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* min_value]; $($rest)*);
    };
    (@__anapao_node_props Register, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; min_value: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard min_value [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_min_value(__anapao_value);
        $crate::scenario!(@__anapao_node_props Register, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* min_value]; $($rest)*);
    };
    (@__anapao_node_props Register, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; max_value: none, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard max_value [$($seen)*]);
        $config = $config.without_max_value();
        $crate::scenario!(@__anapao_node_props Register, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* max_value]; $($rest)*);
    };
    (@__anapao_node_props Register, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; max_value: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard max_value [$($seen)*]);
        let __anapao_value = $value;
        $config = $config.with_max_value(__anapao_value);
        $crate::scenario!(@__anapao_node_props Register, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* max_value]; $($rest)*);
    };

    // Positive Delay/Queue fields return the established SetupError paths without unchecked
    // extraction.
    (@__anapao_node_props Delay, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; steps: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard steps [$($seen)*]);
        let __anapao_value = $value;
        let __anapao_value = ::std::num::NonZeroU64::new(__anapao_value).ok_or_else(||
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("nodes.{}.config.delay_steps", $node_name),
                reason: ::std::convert::From::from("must be greater than 0"),
            })?;
        $config = $config.with_delay_steps(__anapao_value);
        $crate::scenario!(@__anapao_node_props Delay, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* steps]; $($rest)*);
    };
    (@__anapao_node_props Queue, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; capacity: none, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard capacity [$($seen)*]);
        $config = $config.without_capacity();
        $crate::scenario!(@__anapao_node_props Queue, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* capacity]; $($rest)*);
    };
    (@__anapao_node_props Queue, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; capacity: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard capacity [$($seen)*]);
        let __anapao_value = $value;
        let __anapao_value = ::std::num::NonZeroU64::new(__anapao_value).ok_or_else(||
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("nodes.{}.config.capacity", $node_name),
                reason: ::std::convert::From::from("must be greater than 0"),
            })?;
        $config = $config.with_capacity(__anapao_value);
        $crate::scenario!(@__anapao_node_props Queue, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* capacity]; $($rest)*);
    };
    (@__anapao_node_props Queue, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; release_per_step: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_native_guard release_per_step [$($seen)*]);
        let __anapao_value = $value;
        let __anapao_value = ::std::num::NonZeroU64::new(__anapao_value).ok_or_else(||
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!("nodes.{}.config.release_per_step", $node_name),
                reason: ::std::convert::From::from("must be greater than 0"),
            })?;
        $config = $config.with_release_per_step(__anapao_value);
        $crate::scenario!(@__anapao_node_props Queue, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* release_per_step]; $($rest)*);
    };

    // Mode applies to all configurable mode families except Register. Trigger/action are the
    // field-level shorthand for constructing NodeModeConfig.
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; mode: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_family_mode $family);
        $crate::scenario!(@__anapao_native_guard mode [$($seen)*]);
        $crate::scenario!(@__anapao_assert_trigger [$($seen)*]);
        $crate::scenario!(@__anapao_assert_action [$($seen)*]);
        let __anapao_value = $value;
        $mode = __anapao_value;
        $config = $config.with_mode(::std::clone::Clone::clone(&$mode));
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* mode]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; trigger: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_family_mode $family);
        $crate::scenario!(@__anapao_native_guard trigger [$($seen)*]);
        $crate::scenario!(@__anapao_assert_mode [$($seen)*]);
        let __anapao_value = $value;
        $mode.trigger_mode = __anapao_value;
        $config = $config.with_mode(::std::clone::Clone::clone(&$mode));
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* trigger]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; action: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_family_mode $family);
        $crate::scenario!(@__anapao_native_guard action [$($seen)*]);
        $crate::scenario!(@__anapao_assert_mode [$($seen)*]);
        let __anapao_value = $value;
        $mode.action_mode = __anapao_value;
        $config = $config.with_mode(::std::clone::Clone::clone(&$mode));
        $crate::scenario!(@__anapao_node_props $family, $config, $mode, $node_name, $label, $initial,
            $tags, $metadata; [$($seen)* action]; $($rest)*);
    };
    (@__anapao_node_props $family:ident, $config:ident, $mode:ident, $node_name:ident, $label:ident, $initial:ident,
        $tags:ident, $metadata:ident; [$($seen:ident)*]; $($bad:tt)+) => {
        ::std::compile_error!("anapao::scenario!: unknown, duplicate, or wrong-family node property")
    };

    (@__anapao_family_converter_trader Converter) => {};
    (@__anapao_family_converter_trader Trader) => {};
    (@__anapao_family_converter_trader $other:ident) => {
        ::std::compile_error!("anapao::scenario!: ignore_disabled_inputs is only valid for Converter or Trader")
    };
    (@__anapao_family_config Pool) => {};
    (@__anapao_family_config Drain) => {};
    (@__anapao_family_config SortingGate) => {};
    (@__anapao_family_config TriggerGate) => {};
    (@__anapao_family_config MixedGate) => {};
    (@__anapao_family_config Converter) => {};
    (@__anapao_family_config Trader) => {};
    (@__anapao_family_config Register) => {};
    (@__anapao_family_config Delay) => {};
    (@__anapao_family_config Queue) => {};
    (@__anapao_family_config $other:ident) => {
        ::std::compile_error!("anapao::scenario!: typed config is only valid for configured node families")
    };
    (@__anapao_family_mode Pool) => {};
    (@__anapao_family_mode Drain) => {};
    (@__anapao_family_mode SortingGate) => {};
    (@__anapao_family_mode TriggerGate) => {};
    (@__anapao_family_mode MixedGate) => {};
    (@__anapao_family_mode Converter) => {};
    (@__anapao_family_mode Trader) => {};
    (@__anapao_family_mode Delay) => {};
    (@__anapao_family_mode Queue) => {};
    (@__anapao_family_mode $other:ident) => {
        ::std::compile_error!("anapao::scenario!: mode is not valid for this node family")
    };

    // Duplicate/exclusivity checks. Native fields all share the config exclusion check, while
    // the per-field scanner emits compile-time diagnostics for duplicates.
    (@__anapao_native_guard $field:ident [$($seen:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_typed_config [$($seen)*]);
        $crate::scenario!(@__anapao_assert_field $field [$($seen)*]);
    };
    (@__anapao_assert_no_typed_config []) => {};
    (@__anapao_assert_no_typed_config [config $($rest:ident)*]) => {
        ::std::compile_error!("anapao::scenario!: typed config cannot be mixed with native config fields")
    };
    (@__anapao_assert_no_typed_config [$head:ident $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_typed_config [$($rest)*])
    };
    (@__anapao_assert_no_native_config []) => {};
    (@__anapao_assert_no_native_config [label $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_config [$($rest)*])
    };
    (@__anapao_assert_no_native_config [initial $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_config [$($rest)*])
    };
    (@__anapao_assert_no_native_config [tags $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_config [$($rest)*])
    };
    (@__anapao_assert_no_native_config [metadata $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_config [$($rest)*])
    };
    (@__anapao_assert_no_native_config [$other:ident $($rest:ident)*]) => {
        ::std::compile_error!("anapao::scenario!: typed config cannot be mixed with native config fields")
    };

    // A compact token equality dispatcher avoids a separate scanner arm set for every field.
    (@__anapao_assert_field $field:ident []) => {};
    (@__anapao_assert_field $field:ident [$head:ident $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_compare_field $field $head; [$($rest)*]);
    };
    (@__anapao_compare_field label label; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate label property") };
    (@__anapao_compare_field initial initial; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate initial property") };
    (@__anapao_compare_field tags tags; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate tags property") };
    (@__anapao_compare_field metadata metadata; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate metadata property") };
    (@__anapao_compare_field config config; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate config property") };
    (@__anapao_compare_field capacity capacity; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate capacity property") };
    (@__anapao_compare_field allow_negative_start allow_negative_start; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate allow_negative_start property") };
    (@__anapao_compare_field ignore_disabled_inputs ignore_disabled_inputs; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate ignore_disabled_inputs property") };
    (@__anapao_compare_field interactive interactive; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate interactive property") };
    (@__anapao_compare_field min_value min_value; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate min_value property") };
    (@__anapao_compare_field max_value max_value; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate max_value property") };
    (@__anapao_compare_field steps steps; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate steps property") };
    (@__anapao_compare_field release_per_step release_per_step; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate release_per_step property") };
    (@__anapao_compare_field mode mode; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate mode property") };
    (@__anapao_compare_field trigger trigger; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate trigger property") };
    (@__anapao_compare_field action action; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate action property") };
    (@__anapao_compare_field connection connection; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate connection property") };
    (@__anapao_compare_field token_size token_size; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate token_size property") };
    (@__anapao_compare_field enabled enabled; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate enabled property") };
    (@__anapao_compare_field role role; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate role property") };
    (@__anapao_compare_field formula formula; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate formula property") };
    (@__anapao_compare_field target target; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate target property") };
    (@__anapao_compare_field resource_filter resource_filter; [$($rest:ident)*]) => { ::std::compile_error!("anapao::scenario!: duplicate resource_filter property") };
    (@__anapao_compare_field $field:ident $head:ident; [$($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_field $field [$($rest)*])
    };
    (@__anapao_assert_label [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field label [$($seen)*]) };
    (@__anapao_assert_initial [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field initial [$($seen)*]) };
    (@__anapao_assert_tags [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field tags [$($seen)*]) };
    (@__anapao_assert_metadata [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field metadata [$($seen)*]) };
    (@__anapao_assert_config [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field config [$($seen)*]) };
    (@__anapao_assert_mode [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field mode [$($seen)*]) };
    (@__anapao_assert_trigger [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field trigger [$($seen)*]) };
    (@__anapao_assert_action [$($seen:ident)*]) => { $crate::scenario!(@__anapao_assert_field action [$($seen)*]) };

    // Edge declarations. A connection block is an optional suffix after the transfer.
    (@__anapao_build_edges $builder:ident, $nodes:ident, $metrics:ident, $edges:ident;) => {};
    (@__anapao_build_edges $builder:ident, $nodes:ident, $metrics:ident, $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))? resource {$($props:tt)*}; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_edge_transfer $builder, $nodes, $metrics, $edges,
            $name, $from, $to; $transfer $(($($args)*))? resource {$($props)*});
        $crate::scenario!(@__anapao_build_edges $builder, $nodes, $metrics, $edges; $($rest)*);
    }};
    (@__anapao_build_edges $builder:ident, $nodes:ident, $metrics:ident, $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))? state {$($props:tt)*}; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_edge_transfer $builder, $nodes, $metrics, $edges,
            $name, $from, $to; $transfer $(($($args)*))? state {$($props)*});
        $crate::scenario!(@__anapao_build_edges $builder, $nodes, $metrics, $edges; $($rest)*);
    }};
    (@__anapao_build_edges $builder:ident, $nodes:ident, $metrics:ident, $edges:ident;
        $name:ident : $from:ident -> $to:ident =>
        $transfer:ident $(($($args:tt)*))?; $($rest:tt)*) => {{
        $crate::scenario!(@__anapao_edge_transfer $builder, $nodes, $metrics, $edges,
            $name, $from, $to; $transfer $(($($args)*))?);
        $crate::scenario!(@__anapao_build_edges $builder, $nodes, $metrics, $edges; $($rest)*);
    }};

    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; fixed($value:expr) $($suffix:tt)*) => {{
        let __anapao_value = $value;
        let __anapao_transfer = $crate::types::TransferSpec::Fixed { amount: __anapao_value };
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; fraction($numerator:expr, $denominator:expr) $($suffix:tt)*) => {{
        let __anapao_numerator = $numerator;
        let __anapao_denominator = $denominator;
        let __anapao_transfer = $crate::types::TransferSpec::Fraction {
            numerator: __anapao_numerator, denominator: __anapao_denominator
        };
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; remaining $($suffix:tt)*) => {{
        let __anapao_transfer = $crate::types::TransferSpec::Remaining;
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; metric_scaled($metric:ident, $factor:expr) $($suffix:tt)*) => {{
        let __anapao_factor = $factor;
        let __anapao_transfer = $crate::types::TransferSpec::MetricScaled {
            metric: $metrics(::std::stringify!($metric))?, factor: __anapao_factor
        };
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; expression($formula:expr) $($suffix:tt)*) => {{
        let __anapao_formula = $formula;
        let __anapao_transfer = $crate::types::TransferSpec::Expression {
            formula: ::std::convert::Into::into(__anapao_formula)
        };
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $builder:ident, $nodes:ident, $metrics:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident; transfer($transfer:expr) $($suffix:tt)*) => {{
        let __anapao_transfer = $transfer;
        $crate::scenario!(@__anapao_edge_finish $builder, $nodes, $edges,
            $name, $from, $to, __anapao_transfer; $($suffix)*);
    }};
    (@__anapao_edge_transfer $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: malformed transfer")
    };

    (@__anapao_edge_finish $builder:ident, $nodes:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident, $transfer:ident;) => {{
        let __anapao_edge = $crate::types::ScenarioEdge::resource(
            $edges(::std::stringify!($name))?, $nodes(::std::stringify!($from))?,
            $nodes(::std::stringify!($to))?, $transfer,
            <$crate::types::ResourceConnection as ::std::default::Default>::default()
        );
        $builder.insert_edge(__anapao_edge)?;
    }};
    (@__anapao_edge_finish $builder:ident, $nodes:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident, $transfer:ident; resource {$($props:tt)*}) => {{
        let mut __anapao_connection =
            <$crate::types::ResourceConnection as ::std::default::Default>::default();
        let mut __anapao_enabled: bool = true;
        let mut __anapao_metadata: ::std::vec::Vec<(
            ::std::string::String, ::std::string::String
        )> = ::std::vec::Vec::new();
        let __anapao_edge_name = ::std::stringify!($name);
        $crate::scenario!(@__anapao_resource_props __anapao_connection, __anapao_edge_name,
            __anapao_enabled, __anapao_metadata; []; $($props)* ,);
        let mut __anapao_edge = $crate::types::ScenarioEdge::resource(
            $edges(::std::stringify!($name))?, $nodes(::std::stringify!($from))?,
            $nodes(::std::stringify!($to))?, $transfer, __anapao_connection
        ).with_enabled(__anapao_enabled);
        for (__anapao_key, __anapao_value) in __anapao_metadata {
            __anapao_edge = __anapao_edge.with_metadata(__anapao_key, __anapao_value);
        }
        $builder.insert_edge(__anapao_edge)?;
    }};
    (@__anapao_edge_finish $builder:ident, $nodes:ident, $edges:ident,
        $name:ident, $from:ident, $to:ident, $transfer:ident; state {$($props:tt)*}) => {{
        let mut __anapao_connection =
            <$crate::types::StateConnection as ::std::default::Default>::default();
        let mut __anapao_enabled: bool = true;
        let mut __anapao_metadata: ::std::vec::Vec<(
            ::std::string::String, ::std::string::String
        )> = ::std::vec::Vec::new();
        $crate::scenario!(@__anapao_state_props __anapao_connection,
            __anapao_enabled, __anapao_metadata, $edges; []; $($props)* ,);
        let mut __anapao_edge = $crate::types::ScenarioEdge::state(
            $edges(::std::stringify!($name))?, $nodes(::std::stringify!($from))?,
            $nodes(::std::stringify!($to))?, $transfer, __anapao_connection
        ).with_enabled(__anapao_enabled);
        for (__anapao_key, __anapao_value) in __anapao_metadata {
            __anapao_edge = __anapao_edge.with_metadata(__anapao_key, __anapao_value);
        }
        $builder.insert_edge(__anapao_edge)?;
    }};
    (@__anapao_edge_finish $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: malformed connection suffix")
    };

    // Resource connection fields.
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*];) => {};
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*]; , $($rest:tt)*) => {
        $crate::scenario!(@__anapao_resource_props $connection, $edge_name, $enabled, $metadata;
            [$($seen)*]; $($rest)*);
    };
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*]; connection: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field connection [$($seen)*]);
        $crate::scenario!(@__anapao_assert_field token_size [$($seen)*]);
        let __anapao_value = $value;
        $connection = __anapao_value;
        $crate::scenario!(@__anapao_resource_props $connection, $edge_name, $enabled, $metadata;
            [$($seen)* connection]; $($rest)*);
    };
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*]; token_size: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field token_size [$($seen)*]);
        $crate::scenario!(@__anapao_assert_field connection [$($seen)*]);
        let __anapao_value = $value;
        let __anapao_value = ::std::num::NonZeroU64::new(__anapao_value).ok_or_else(||
            $crate::error::SetupError::InvalidParameter {
                name: ::std::format!(
                    "edges.{}.connection.resource.token_size", $edge_name
                ),
                reason: ::std::convert::From::from("must be greater than 0"),
            })?;
        $connection = $connection.with_token_size(__anapao_value);
        $crate::scenario!(@__anapao_resource_props $connection, $edge_name, $enabled, $metadata;
            [$($seen)* token_size]; $($rest)*);
    };
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*]; enabled: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field enabled [$($seen)*]);
        let __anapao_value = $value;
        $enabled = __anapao_value;
        $crate::scenario!(@__anapao_resource_props $connection, $edge_name, $enabled, $metadata;
            [$($seen)* enabled]; $($rest)*);
    };
    (@__anapao_resource_props $connection:ident, $edge_name:ident, $enabled:ident, $metadata:ident;
        [$($seen:ident)*]; metadata {$($key:expr => $value:expr),* $(,)?}, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field metadata [$($seen)*]);
        $(
            let __anapao_key = $key;
            let __anapao_value = $value;
            $metadata.push((::std::convert::Into::into(__anapao_key),
                ::std::convert::Into::into(__anapao_value)));
        )*
        $crate::scenario!(@__anapao_resource_props $connection, $edge_name, $enabled, $metadata;
            [$($seen)* metadata]; $($rest)*);
    };
    (@__anapao_resource_props $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: unknown or duplicate resource connection property")
    };

    // State connection fields and targets.
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*];) => {};
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; , $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)*]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; connection: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field connection [$($seen)*]);
        $crate::scenario!(@__anapao_assert_no_native_connection [$($seen)*]);
        let __anapao_value = $value;
        $connection = __anapao_value;
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* connection]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; role: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard role [$($seen)*]);
        let __anapao_value = $value;
        $connection = $connection.with_role(__anapao_value);
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* role]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; formula: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard formula [$($seen)*]);
        let __anapao_value = $value;
        $connection = $connection.with_formula(__anapao_value);
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* formula]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; target: node, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard target [$($seen)*]);
        $connection = $connection.with_target($crate::types::StateTarget::Node);
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* target]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; target: resource_connection($edge:ident), $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard target [$($seen)*]);
        $connection = $connection.with_target($crate::types::StateTarget::ResourceConnection(
            $edges(::std::stringify!($edge))?
        ));
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* target]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; target: state_connection($edge:ident), $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard target [$($seen)*]);
        $connection = $connection.with_target($crate::types::StateTarget::StateConnection(
            $edges(::std::stringify!($edge))?
        ));
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* target]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; target: formula($edge:ident), $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard target [$($seen)*]);
        $connection = $connection.with_target($crate::types::StateTarget::Formula(
            $edges(::std::stringify!($edge))?
        ));
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* target]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; resource_filter: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_state_native_guard resource_filter [$($seen)*]);
        let __anapao_value = $value;
        $connection = $connection.with_resource_filter(__anapao_value);
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* resource_filter]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; enabled: $value:expr, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field enabled [$($seen)*]);
        let __anapao_value = $value;
        $enabled = __anapao_value;
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* enabled]; $($rest)*);
    };
    (@__anapao_state_props $connection:ident, $enabled:ident, $metadata:ident, $edges:ident;
        [$($seen:ident)*]; metadata {$($key:expr => $value:expr),* $(,)?}, $($rest:tt)*) => {
        $crate::scenario!(@__anapao_assert_field metadata [$($seen)*]);
        $(
            let __anapao_key = $key;
            let __anapao_value = $value;
            $metadata.push((::std::convert::Into::into(__anapao_key),
                ::std::convert::Into::into(__anapao_value)));
        )*
        $crate::scenario!(@__anapao_state_props $connection, $enabled, $metadata, $edges;
            [$($seen)* metadata]; $($rest)*);
    };
    (@__anapao_state_props $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: unknown, duplicate, or malformed state connection property")
    };
    (@__anapao_state_native_guard $field:ident [$($seen:ident)*]) => {
        $crate::scenario!(@__anapao_assert_field connection [$($seen)*]);
        $crate::scenario!(@__anapao_assert_field $field [$($seen)*]);
    };
    (@__anapao_assert_no_native_connection []) => {};
    (@__anapao_assert_no_native_connection [enabled $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_connection [$($rest)*])
    };
    (@__anapao_assert_no_native_connection [metadata $($rest:ident)*]) => {
        $crate::scenario!(@__anapao_assert_no_native_connection [$($rest)*])
    };
    (@__anapao_assert_no_native_connection [$other:ident $($rest:ident)*]) => {
        ::std::compile_error!("anapao::scenario!: typed connection cannot be mixed with native connection fields")
    };

    (@__anapao_tail $builder:ident, $out:ident, $nodes:ident, $metrics:ident;
        track [$($tracked:ident),* $(,)?]; $($ends:tt)*) => {
        $(
            $builder = $builder.with_tracked_metric(
                $metrics(::std::stringify!($tracked))?
            );
        )*
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($ends)*);
    };
    (@__anapao_tail $builder:ident, $out:ident, $nodes:ident, $metrics:ident;
        $($ends:tt)*) => {
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($ends)*);
    };

    // End-condition parsing. Recursive lists use comma-separated conditions.
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;) => {};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end max_steps($value:expr); $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MaxSteps { steps: __anapao_value });
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end metric_at_least($metric:ident, $value:expr); $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MetricAtLeast {
            metric: $metrics(::std::stringify!($metric))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end metric_at_most($metric:ident, $value:expr); $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MetricAtMost {
            metric: $metrics(::std::stringify!($metric))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end node_at_least($node:ident, $value:expr); $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::NodeAtLeast {
            node_id: $nodes(::std::stringify!($node))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end node_at_most($node:ident, $value:expr); $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::NodeAtMost {
            node_id: $nodes(::std::stringify!($node))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end condition($condition:expr); $($rest:tt)*) => {{
        let __anapao_condition = $condition;
        $out.push(__anapao_condition);
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end any [$($items:tt)*]; $($rest:tt)*) => {{
        let mut __anapao_items = ::std::vec::Vec::new();
        $crate::scenario!(@__anapao_end_items __anapao_items, $nodes, $metrics; $($items)* ,);
        $out.push($crate::types::EndConditionSpec::Any(__anapao_items));
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $out:ident, $nodes:ident, $metrics:ident;
        end all [$($items:tt)*]; $($rest:tt)*) => {{
        let mut __anapao_items = ::std::vec::Vec::new();
        $crate::scenario!(@__anapao_end_items __anapao_items, $nodes, $metrics; $($items)* ,);
        $out.push($crate::types::EndConditionSpec::All(__anapao_items));
        $crate::scenario!(@__anapao_top_ends $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_top_ends $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: malformed end condition")
    };

    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;) => {};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident; , $($rest:tt)*) => {
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    };
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        max_steps($value:expr), $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MaxSteps { steps: __anapao_value });
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        metric_at_least($metric:ident, $value:expr), $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MetricAtLeast {
            metric: $metrics(::std::stringify!($metric))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        metric_at_most($metric:ident, $value:expr), $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::MetricAtMost {
            metric: $metrics(::std::stringify!($metric))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        node_at_least($node:ident, $value:expr), $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::NodeAtLeast {
            node_id: $nodes(::std::stringify!($node))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        node_at_most($node:ident, $value:expr), $($rest:tt)*) => {{
        let __anapao_value = $value;
        $out.push($crate::types::EndConditionSpec::NodeAtMost {
            node_id: $nodes(::std::stringify!($node))?, value_scaled: __anapao_value
        });
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        condition($condition:expr), $($rest:tt)*) => {{
        let __anapao_condition = $condition;
        $out.push(__anapao_condition);
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        any [$($items:tt)*], $($rest:tt)*) => {{
        let mut __anapao_nested = ::std::vec::Vec::new();
        $crate::scenario!(@__anapao_end_items __anapao_nested, $nodes, $metrics; $($items)* ,);
        $out.push($crate::types::EndConditionSpec::Any(__anapao_nested));
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $out:ident, $nodes:ident, $metrics:ident;
        all [$($items:tt)*], $($rest:tt)*) => {{
        let mut __anapao_nested = ::std::vec::Vec::new();
        $crate::scenario!(@__anapao_end_items __anapao_nested, $nodes, $metrics; $($items)* ,);
        $out.push($crate::types::EndConditionSpec::All(__anapao_nested));
        $crate::scenario!(@__anapao_end_items $out, $nodes, $metrics; $($rest)*);
    }};
    (@__anapao_end_items $($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: malformed recursive end condition")
    };

    ($($bad:tt)*) => {
        ::std::compile_error!("anapao::scenario!: expected canonical id/fields/nodes/edges/track/end order")
    };
}