frame-suite 0.1.0

Core traits, semantic abstractions, and foundational interfaces for the FRAME Suite runtime architecture
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
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
// SPDX-License-Identifier: MPL-2.0
//
// Part of Auguth Labs open-source softwares.
// Built for the Substrate framework.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2026 Auguth Labs (OPC) Pvt Ltd, India

// ===============================================================================
// ```````````````````````````````` PLUGINS SUITE ````````````````````````````````
// ===============================================================================

//! Pluggable, type-safe execution framework for composing runtime behavior via plugin
//! models and families.
//!
//! This module offers two complementary abstractions for extensible, type-safe
//! runtime behaviour:
//!
//! - Read [Plugin Model](#plugin-model) to understand the **fundamental unit**
//!   of computation - a single operation plugin, analogous to a pure function
//!   or a procedure that may mutate its input and produce an output.
//!
//! - Read [Plugin Families](#plugin-families) when modelling a **cohesive
//!   state-machine-like component** composed of multiple related operations
//!   (methods). A family groups several operation-specific plugin models under
//!   one logical root, while the concrete implementation variant of each
//!   operation is plugged via a single family model.
//!
//! > **Note:** Plugin families are built on top of plugin models.  
//! > To correctly design or use families, one must first understand the
//! > plugin model abstraction, since families internally orchestrate multiple
//! > models as their operational building blocks.
//!
//! In short:
//!
//! ```text
//! Simple, single-step transformation?                 -> Plugin Model
//! Multi-operation logical component / state machine? -> Plugin Family (built from Plugin Models)
//! ```
//!
//! Both approaches share the same execution infrastructure and compile-time
//! type-safety guarantees, but differ in how behaviour is structured,
//! composed, and ultimately resolved.
//!
//! # Plugin Model
//!
//! A **plugin model** is a type-safe, swappable unit of computation with optional
//! context. It enables deterministic, composable, and runtime-configurable behavior.
//!
//! Each model:
//! - Implements [`PurePluginModel<Input, Context, Output>`] or
//!   [`MutablePluginModel<Input, Context, Output>`]
//! - Produces an output from input (and optional context)
//!
//! Execution:
//! - `compute(input, &context) -> output`
//! - `compute_mut(&mut input, &context) -> output`
//!
//! Context:
//! - Provided via [`ModelContext`]
//! - Defined using [`plugin_context`](crate::plugin_context)
//!
//! Tooling:
//! - Declare via [`plugin_types`](crate::plugin_types)
//! - Define via [`plugin_model`](crate::plugin_model)
//! - Execute via [`plugin_output`](crate::plugin_output)
//! - Test with [`plugin_test`](crate::plugin_test)
//!
//! ## Motivation
//!
//! Conventional trait-based designs couple the **contract** and the
//! **implementation** into a single resolution step:
//!
//! ```text
//! Sub-set Contract (Trait Bounds)
//!        |
//!        v
//! Concrete Type (Implementation)
//! ```
//!
//! The implementation is chosen first, and the contract is something it must
//! satisfy. Any **stronger bounds or richer behavior** remain internal to the
//! concrete type and cannot be independently selected or composed.
//!
//! This leads to key limitations:
//!
//! - Behavior is fixed once the type is chosen
//! - Stronger capabilities cannot be surfaced or selected explicitly
//! - Context-driven or configuration-based behavior becomes difficult
//!
//! ## Behaviour Model
//!
//! Plugin models treat behavior as a **compatibility problem between two
//! independent entities**:
//!
//! ```text
//! Sub-set Contract (Pallet)
//!        <->
//! Super-set Capability (Model + Bounds + Context)
//! ```
//!
//! These are defined independently and only come together through
//! **compatibility matching**.
//!
//! Matching rule:
//!
//! - The super-set must satisfy all requirements of the sub-set
//! - The sub-set must allow the super-set's stronger bounds
//!
//! Only when both conditions hold does a valid composition exist.
//!
//! ## Benefits
//!
//! - Decoupled contract and behavior
//! - Multiple interchangeable implementations
//! - Context-driven execution
//! - Late selection via configuration
//! - Full compile-time verification
//!
//! Behaviour is not implemented, it is resolved by matching a sub-set contract
//! with a compatible super-set capability.
//!
//! ## Example: Sorter Plugin
//!
//! This example shows how a pallet can dynamically select sorting strategies
//! at runtime via plugin types.
//!
//! ```ignore
//!
//! // ----- Support Crate -------
//!
//! /// A generic sorter plugin trait.
//! ///
//! /// This trait defines a plugin point where the actual sorting logic
//! /// is provided by an associated plugin model and its context.
//! pub trait Sorter<Input> {
//!     /// The output type produced by the sorter.
//!     type Output;
//!
//!     // Declare the associated plugin model and context types.
//!     // These will be supplied by downstream crates (e.g., pallets or runtime).
//!     plugin_types! {
//!         input: Input,
//!         output: Self::Output,
//!         model: Model,
//!         context: Context,
//!     }
//!
//!     plugin_output! {
//!         /// Execute the sorting logic using the injected plugin model.
//!         ///
//!         /// The actual implementation is resolved at compile time based on
//!         /// the associated `Model` and `Context` types.
//!         fn sort
//!         input: values,
//!         model: Self::Model,
//!         context: Self::Context,
//!     }
//! }
//!
//! // ----- Pallet Crate -------
//!
//! /// Pallet configuration exposing plugin hook points.
//! ///
//! /// The runtime will decide which concrete model and context to use.
//! pub trait Config: frame_system::Config {
//!     /// Input type consumed by the sorter plugin.
//!     type InputX;
//!
//!     /// Output type produced by the sorter plugin.
//!     type OutputX;
//!
//!     // Declare pallet-level plugin types that must satisfy the plugin contract.
//!     plugin_types! {
//!         input: Self::InputX,
//!         output: Self::OutputX,
//!         model: ModelX,     // Concrete model chosen by the runtime
//!         context: ContextX, // Concrete context provider chosen by the runtime
//!     }
//! }
//!
//! /// Implement the generic sorter plugin for the pallet.
//! ///
//! /// The pallet simply forwards execution to the configured model.
//! impl<T: Config> Sorter<T::InputX> for Pallet<T> {
//!     type Output = T::OutputX;
//!     type Model = T::ModelX;
//!     type Context = T::ContextX;
//! }
//!
//! /// Helper function demonstrating how the plugin is executed generically.
//! fn try_sort<T: Config>(values: &T::InputX) -> T::OutputX {
//!     <Pallet<T> as Sorter<T::InputX>>::sort(values)
//! }
//!
//! // ----- Runtime Crate -------
//!
//! /// Define a generic plugin model.
//! /// This model sorts the generic type `Vector` in ascending order and then
//! /// purges all elements greater than the runtime `until` threshold.
//! /// Such many models like this can live in plugin-registries
//! plugin_model! {
//!     name: CappedSort,
//!     input: Vector,
//!     output: Vector,
//!     context: UntilConfig<Number>,
//!     others: [Number],
//!     bounds: [
//!         // Elements must be comparable and clonable
//!         Number: Unsigned + Clone + Ord,
//!         // Vector must be iterable and rebuildable after filtering
//!         Vector: IntoIterator<Item = Number> + FromIterator<Number> + Clone,
//!     ],
//!     compute: |values, ctx| {
//!         // Clone input so original remains unchanged
//!         let mut v: Vector = values.clone();
//!
//!         // Retrieve runtime threshold from context
//!         let until = ctx.0.clone();
//!
//!         // Sort from small to large
//!         let mut temp: Vec<Number> = v.into_iter().collect();
//!         temp.sort();
//!
//!         // Find first element greater than `until`
//!         // Purge that element and everything after it
//!         let filtered = temp
//!             .into_iter()
//!             .take_while(|x| *x <= until)
//!             .collect::<Vec<_>>();
//!
//!         // Rebuild the output vector from the filtered values
//!         filtered.into_iter().collect()
//!     }
//! }
//!
//! /// Context data structure holding the threshold value.
//! /// Such many models's contexts like this can live in
//! /// plugin-registries, as models and its contexts are tightly coupled
//! struct UntilConfig<Number>(Number);
//!
//! /// Define a concrete context provider supplying the threshold.
//! plugin_context! {
//!     name: MyContext,
//!     context: UntilConfig<u8>,
//!     value: UntilConfig(10),
//! }
//!
//! /// Inject the concrete model and context into the runtime configuration.
//! impl Config for Runtime {
//!     type InputX = Vec<u8>;
//!     type OutputX = Vec<u8>;
//!     type ModelX = CappedSort;   // Uses capped sorting logic
//!     type ContextX = MyContext;  // Provides the `until` threshold
//! }
//!
//! // Example behavior:
//! // Input:  vec![12, 3, 8, 25, 5]
//! // Sorted: [3, 5, 8, 12, 25]
//! // until = 10
//! // Output: [3, 5, 8]   // elements > 10 are purged
//!
//! ```
//!
//! The pallet only assumes a generic “sorter” transformation. The runtime injects
//! `CappedSort`, which sorts values ascending and purges elements greater than a
//! contextual `until` threshold. The pallet sees only the minimal contract,
//! while the runtime provides richer, context-driven logic.
//!
//! # Plugin Families
//!
//! A **plugin family** extends a single plugin model into a **unified logical
//! plugin** with multiple related operations.
//!
//! Unlike a model (one computation), a family represents a cohesive component
//! (e.g., state machine or service) whose operations are selected via *child*
//! markers, while a **family type** maps them to concrete models.
//!
//! This lets callers use a single interface while deferring implementation
//! choice to runtime configuration.
//!
//! ## Motivation
//!
//! A single model fits simple transformations:
//!
//! ```text
//! Model   -> Implementation
//! Context -> Parameters
//! ```
//!
//! But real systems need:
//!
//! - Multiple related operations
//! - Multiple strategy variants
//! - Configurable behaviour
//!
//! A **plugin family** groups these operations under one unit, where:
//!
//! - Children = operations
//! - Family type = model mapping
//!
//! Result: structured, state-machine-like design with compile-time resolution.
//!
//! ## State-Machine Style Logical Plugin
//!
//! A plugin family acts as a logical component exposing multiple operations,
//! similar to a state machine or service:
//!
//! ```text
//! Family Root (Unified Interface)
//!   ├── Child A -> Operation A
//!   ├── Child B -> Operation B
//!   └── Child C -> Operation C -> Concrete Model (via Family Type)
//! ```
//!
//! - **Root**: unified special-interface
//! - **Child**: operation selector
//! - **Family type**: maps each operation to a concrete model
//!
//! Calling a child is equivalent to invoking a method on the plugin.
//! The concrete model is resolved by the family type, with context passed
//! through to execution.
//!
//! ### Family Contract Consistency
//!
//! All models within a **family type** are expected to share the same
//! execution contract:
//!
//! ```text
//! Input   -> shared
//! Output  -> shared
//! Context -> shared
//! ```
//!
//! This allows the family to behave as a **uniform pluggable component**,
//! where operations can be invoked without knowledge of the underlying model.
//!
//! ```text
//! Family Type
//!   ├── Child A -> ModelA<Input, Context, Output>
//!   ├── Child B -> ModelB<Input, Context, Output>
//!   └── Child C -> ModelC<Input, Context, Output>
//! ```
//!
//! With a consistent `(Input, Output, Context)` signature, callers interact
//! through the root interface while the compiler resolves the concrete model.
//!
//! While not strictly enforced, consistent contracts are recommended for
//! clarity and interchangeability.
//!
//! ### Trait Bound Consistency
//!
//! Plugin models define `(Input, Output, Context)` generically via trait bounds.
//!
//! Within a **family type**, the concrete types must satisfy the **combined
//! bounds** of all possible models.
//!
//! ```text
//! ModelA requires: Input: Ord
//! ModelB requires: Input: Clone
//! ```
//!
//! -> Caller must provide:
//!
//! ```text
//! Input: Ord + Clone
//! ```
//!
//! The family contract therefore reflects the **union of required bounds**:
//!
//! ```text
//! Family Contract
//!   Input: Ord + Clone
//!   Output: ...
//! ```
//!
//! This guarantees that any selected model can be resolved safely at compile time.
//!
//! ## Plugin Family as a Logical Plugin State Machine
//!
//! ```text
//!                         +---------------------------------------+
//!                         |           FAMILY ROOT                  |
//!                         |   Unified logical plugin interface    |
//!                         +--------------------+------------------+
//!                                              |
//!                                      Concrete Family Type
//!                                              |
//!          +-----------------------------------+-----------------------------------+
//!          |                                   |                                   |
//!   +--------------+                    +--------------+                    +--------------+
//!   |   Child A    |                    |   Child B    |                    |   Child C    |
//!   | Operation A  |                    | Operation B  |                    | Operation C  |
//!   +------+-------+                    +------+-------+                    +------+-------+
//!          |                                   |                                   |
//!   +------+-------+                    +------+-------+                    +------+-------+
//!   |   Model A    |                    |   Model B    |                    |   Model C    |
//!   | selected by  |                    | selected by  |                    | selected by  |
//!   | Family Type  |                    | Family Type  |                    | Family Type  |
//!   +------+-------+                    +------+-------+                    +------+-------+
//!          |                                   |                                   |
//!          +---------------------------- Context ----------------------------------+
//!                                   (shared across models)
//! ```
//!
//! In this structure:
//!
//! - The **family root** represents the unified logical plugin interface.
//! - Each **child marker** represents one operation of that interface.
//! - The **family type** determines which concrete model implements each
//!   operation.
//!
//! All models belonging to the same family share the **same context type**.
//! The context is therefore represented as a single input flowing into the
//! resolved model during execution.
//!
//! ### Resolution Flow
//!
//! ```text
//! Caller invokes:
//!   FamilyRoot + FamilyType + ChildX
//!
//! Compiler resolves:
//!   (FamilyType, ChildX) -> ConcreteModel
//!
//! Execution:
//!   ConcreteModel.compute(input, context)
//! ```
//!
//! This allows callers to treat the family as a single logical plugin while
//! the compiler statically resolves the concrete model for each operation.
//!
//! ## Declaration Model
//!
//! A plugin family is constructed using three complementary macros:
//!
//! - [`declare_family`](crate::declare_family)
//! - [`plugin_model`](crate::plugin_model)
//! - [`define_family`](crate::define_family)
//!
//! Together they define the **operations**, **models**, and **family
//! implementation** that make up a plugin family.
//!
//! ### 1. Declaring the Family Interface
//!
//! The [`declare_family`](crate::declare_family) macro defines the **family
//! root trait** and a set of **child marker types** representing operations
//! of the logical plugin.
//!
//! ```text
//! Family Root
//!   ├── ChildA
//!   ├── ChildB
//!   └── ChildC
//! ```
//!
//! The root trait represents the unified plugin interface, while each child
//! marker identifies one operation that the family exposes.
//!
//!
//! ### 2. Defining Plugin Models
//!
//! Concrete behaviour is implemented using [`plugin_model`](crate::plugin_model).
//!
//! Each plugin model implements a specific `(Input, Context, Output)`
//! computation and can later be attached to a family operation.
//!
//! ```text
//! ModelA<Input, Context, Output>
//! ModelB<Input, Context, Output>
//! ModelC<Input, Context, Output>
//! ```
//!
//! Models remain independent units of computation and can be reused across
//! different families.
//!
//!
//! ### 3. Defining the Family Implementation
//!
//! The [`define_family`](crate::define_family) macro creates a **concrete
//! family type** that binds each child operation to a specific model.
//!
//! ```text
//! FamilyType
//!   ├── ChildA -> ModelA
//!   ├── ChildB -> ModelB
//!   └── ChildC -> ModelC
//! ```
//!
//! This family type represents a concrete implementation of the family root
//! and determines which models are used for each operation.
//!
//!
//! ### Resolution Model
//!
//! When a caller invokes an operation, it refers only to the **family root**
//! and a **child marker**.
//!
//! The compiler then resolves the concrete model using the configured
//! family type:
//!
//! ```text
//! (FamilyType, Child) -> ConcreteModel
//! ```
//!
//! The resolved model is then executed using the provided `(Input, Context)`
//! values.
//!
//! This design allows callers to interact with the family as a single logical
//! plugin while the runtime configuration determines the concrete behaviour
//! through the selected **family type**.
//!
//!
//! ## Immutable vs Mutable Operational Variants
//!
//! A family may host immutable (`PurePluginModel`) and/or mutable
//! (`MutablePluginModel`) variants for its operations. However, mutability forms
//! part of the execution contract:
//!
//! - Immutable execution resolves only to pure models.
//! - Mutable execution resolves only to mutable models.
//!
//! Even if both coexist in the same family hierarchy, they are not
//! interchangeable at the usage site because the caller's expected execution
//! semantics are part of the type-level interface.
//!
//! ## Example: Family-Based Model Resolution
//!
//! This example demonstrates how a plugin **family** defines a semantic
//! extension point on a caller trait and how concrete models attach
//! themselves to that family. The runtime then selects the active model
//! by supplying an appropriate context.
//!
//! In this design the **family is declared by the caller trait**, because
//! the trait owns the extension point. Concrete plugin models merely
//! register themselves under that family.
//!
//! ### Caller Trait - Declaring the Plugin Family
//!
//! The caller trait defines the **plugin contract** and declares the family
//! that models may attach to.
//!
//! ```ignore
//! declare_family! {
//!     root: pub MathFamilyRoot,
//!     child: [MaybePlusOne]
//! }
//!
//! pub trait MathTrait {
//!     type Input: AtLeast8BitUnsigned;
//!     type Output: AtLeast8BitUnsigned;
//!
//!     plugin_types! {
//!         input: Self::Input,
//!         output: Self::Output,
//!         root: MathFamilyRoot,
//!         family: MathFamily
//!         context: MathContext,
//!     }
//!
//!     plugin_output! {
//!         fn request,
//!         input: Self::Input,
//!         output: Self::Output,
//!         root: MathFamilyRoot,
//!         family: Self::MathFamily
//!         child: MaybePlusOne,
//!         context: Self::MathContext,
//!     }
//! }
//! ```
//!
//! Here:
//!
//! - `MathFamily` defines the semantic plugin domain.
//! - `MaybePlusOne` acts as a **child selector**, representing an optional
//!   increment strategy.
//!
//! The trait itself does **not specify which model is used**.
//!
//! ### Plugin Models - Registering Implementations
//!
//! Plugin models implement behavior for a specific `(Family, Child, Context)`
//! combination. Multiple models may attach to the same child selector.
//!
//! ```ignore
//! pub struct AddOneContext;
//!
//! plugin_model! {
//!     name: AddOne,
//!     input: Value,
//!     context: AddOneContext,
//!     bounds: [Value: AtLeast8BitUnsigned],
//!     compute: |v, _ctx| {
//!         v.clone().saturating_add(One::one())
//!     }
//! }
//!
//! define_family! {
//!     root: MathFamilyRoot,
//!     family: OneFamily,
//!     input: Value,
//!     context: AddOneContext
//!     bounds: [Value: AtLeast8BitUnsigned]
//!     child: [                      
//!         MaybePlusOne => AddOne,
//!     ],
//! }
//!```
//!
//! ```ignore
//! pub struct AddNothingContext;
//!
//! plugin_model! {
//!     name: AddNothing,
//!     input: mut Value,
//!     context: AddNothingContext,
//!     bounds: [Value: Clone],
//!     compute: |v, _ctx| {
//!         v.clone()
//!     }
//! }
//!
//! define_family! {
//!     root: MathFamilyRoot,
//!     family: NoneFamily,
//!     input: Value,
//!     context: AddNothingContext
//!     bounds: [Value: Clone]
//!     child: [                      
//!         MaybePlusOne => AddNothing,
//!     ],
//! }
//! ```
//!
//! Both models attach to the same family and child selector but differ
//! in unified family type, context and execution behavior.
//!
//! ### Pallet Wiring - Remaining Generic
//!
//! The pallet implements the caller trait without committing to a concrete
//! model. It simply forwards the family and context from its configuration.
//!
//! ```ignore
//! struct Pallet<T: Config>(PhantomData<T>);
//!
//! impl<T: Config> MathTrait for Pallet<T> {
//!     type Input = T::XInput;
//!     type Output = T::XOutput;
//!     type MathFamily = T::XMathFamily;
//!     type MathContext = T::XMathContext;
//! }
//! ```
//!
//! This keeps the pallet generic and reusable across runtimes.
//!
//! ### Runtime Injection - Selecting the Active Model
//!
//! The runtime chooses the concrete behavior by supplying a context that
//! matches one of the registered models.
//!
//! ```ignore
//! pub trait Config {
//!     type XInput: AtLeast8BitUnsigned;
//!     type XOutput: AtLeast8BitUnsigned;
//!
//!     plugin_types! {
//!         input: Self::XInput,
//!         output: Self::XOutput,
//!         root: MathFamilyRoot,
//!         family: XMathFamily,
//!         context: XMathContext,
//!     }
//! }
//!
//! plugin_context! {
//!     name: MyContext,
//!     context: AddOneContext,
//!     value: AddOneContext,
//! }
//!
//! pub struct Runtime;
//!
//! impl Config for Runtime {
//!     type XInput = u8;
//!     type XOutput = u8;
//!     type XMathFamily = OneFamily;
//!     type XMathContext = AddOneContext;
//!
//!     // Also can be plugged towards
//!     // type XMathFamily = NoneFamily;
//!     // type XMathContext = AddNothingContext;
//! }
//! ```
//!
//! ### Resolution Flow
//!
//! ```text
//! Runtime selects:
//!   Family  = OneFamily
//!   Child   = MaybePlusOne
//!   Context = AddOneContext
//!
//! Matching Model:
//!   AddOne<Input=u8, Context=AddOneContext, Output=u8>
//! ```
//!
//! If the runtime instead supplied `NoneFamily` & `AddNothingContext`, the alternative model
//! would be selected automatically.
//!
//! The caller trait never names a concrete model. Instead, the compiler resolves
//! the correct implementation purely from the type-level contract:
//!
//! ```text
//! (Family, Child, Context) -> Model
//! ```
//!
//! This enables fully static, type-safe plugin resolution without runtime
//! dispatch or registration tables.

// ===============================================================================
// ````````````````````````````````` CORE TRAITS `````````````````````````````````
// ===============================================================================

/// Core trait implemented by all **immutable plugin models**.
///
/// A plugin model is typically a zero-sized (stateless) struct that defines
/// a specific computation strategy. Each model represents a logically distinct
/// variant within a plugin and may optionally depend on external context
/// to compute its result.
///
/// This trait defines the **pure computation contract**: the input is owned by
/// caller immutably and must not be mutated. The model returns a new output value
/// derived from the input and context.
///
/// ## Generics
/// - `Input`: Type of owned-data consumed by the model.
/// - `Context`: External parameters or configuration required by the model.
/// - `Output`: Type of value produced by the model.
///
/// ## Determinism
/// Implementations are expected to be stateless and deterministic, producing
/// the same output for the same input and context.
pub trait PurePluginModel<Input, Context, Output>: Default {
    /// Computes the model's output for a given immutable input and context.
    fn compute(&self, input: Input, context: &Context) -> Output;
}

/// Trait implemented by **mutable plugin models** that may transform their
/// input in-place while still producing an output.
///
/// Unlike [`PurePluginModel`], this trait explicitly allows mutation of the input,
/// making it suitable for in-place normalization, sorting, accumulation,
/// or other performance-sensitive transformations that avoid extra allocations.
///
/// Mutation is **explicit and opt-in**, preserving clarity between pure and
/// state-transforming computations.
///
/// ## Generics
/// - `Mutate`: Type of data that will be mutated in-place.
/// - `Context`: External parameters or configuration required by the model.
/// - `Output`: Type of value produced by the model.
///
/// ## Semantics
/// - The input may be modified during computation.
/// - The returned output may be derived from either the original or mutated state.
/// - Implementations should still remain stateless with respect to internal storage.
pub trait MutablePluginModel<Mutate, Context, Output>: Default {
    /// Computes the model's output while mutating the input in-place.
    fn compute_mut(&self, input: &mut Mutate, context: &Context) -> Output;
}

/// Represents a source of context for models.
///
/// Models can retrieve context from an implementor of this trait.
pub trait ModelContext {
    /// Associated type representing the actual context.
    type Context;

    /// Returns the context for a model.
    fn context() -> Self::Context;
}

/// Placeholder type for models that **do not require any external context**.
impl ModelContext for () {
    type Context = ();

    fn context() -> () {
        ()
    }
}

// ===============================================================================
// ```````````````````````````````` PLUGIN TYPES `````````````````````````````````
// ===============================================================================

/// Declares **associated plugin types** inside a trait.
///
/// Supports both:
/// - **concrete plugin model binding**, or
/// - **plugin family binding** for late model selection.
///
/// Exactly one of `model` or `family` must be specified.
/// Exactly one of `input` or `input: mut` must be specified.
///
/// ## Syntax
///
/// ### Immutable Concrete Model
///
/// ```ignore
/// plugin_types! {
///     input: InputType,        // Required: immutable input type
///     output: OutputType,      // Required: output type
///     model: ModelAssoc,       // Required: associated plugin model
///     context: ContextAssoc,   // Required: associated context provider
/// }
/// ```
///
/// ### Mutable Concrete Model
///
/// ```ignore
/// plugin_types! {
///     input: mut MutateType,   // Required: mutable input type
///     output: OutputType,      // Required: output type
///     model: ModelAssoc,       // Required: associated plugin model
///     context: ContextAssoc,   // Required: associated context provider
/// }
/// ```
///
/// ### Plugin Family (Immutable or Mutable)
///
/// ```ignore
/// plugin_types! {
///     input: InputType,        // or: input: mut MutateType
///     output: OutputType,      // Required: output type
///     borrow: ['a],            // Optional: lifetime parameters of input/output
///     root: PluginFamilyRoot,  // Required: plugin family root trait
///     family: FamilyAssoc,     // Required: associated plugin family type
///     context: ContextAssoc,   // Required: associated context provider
///     provides: [Send + Sync], // Optional: bounds on context
/// }
/// ```
///
/// ## Lifetimes
///
/// - `lifetimes` expands to `<...>` on the **family associated type**
/// - Enables lifetime-parameterized associated types (GATs)
///
/// ## Context Bounds
///
/// You may restrict the family's **context type** using `provides`.
///
/// ```ignore
/// plugin_types! {
///     input: Input,
///     output: Output,
///     root: PluginFamilyRoot,
///     family: FamilyAssoc,    
///     context: MyContext,
///     provides: [Send + Sync + 'static],
/// }
/// ```
///
/// Expands roughly to:
///
/// ```ignore
/// type MyContext: ModelContext<Context: Send + Sync + 'static>;
/// ```
///
/// ## Input / Output Constraints
///
/// ### Plugin Models (Immutable & Mutable)
/// `Input` and `Output` must not contain generics or lifetime-based types (no GATs)
///
/// ### Plugin Families
/// - `Input` and `Output` may include **lifetimes**
/// - Enabled via parameter `borrow: ['a]`
/// - Type generics are still not supported
///
/// ```text
/// Model   -> concrete associated types only (no generics, no lifetimes, no GATS)
/// Family  -> supports lifetimes only (via borrow)
/// ```
///
/// ## Examples
///
/// ### Concrete Model
///
/// ```ignore
/// pub trait Increment {
///     type Input;
///     type Output;
///
///     plugin_types! {
///         input: Self::Input,
///         output: Self::Output,
///         model: AddOneModel,
///         context: AddOneCtx,
///     }
/// }
/// ```
///
/// ### Plugin Family
///
/// ```ignore
/// pub trait MathOps {
///     type Input;
///     type Output;
///
///     plugin_types! {
///         input: Self::Input,
///         output: Self::Output,
///         root: MathFamilyRoot,
///         family: MathFamily,
///         context: MathContext,
///     }
/// }
/// ```
#[macro_export]
macro_rules! plugin_types {

    // Immutable model arm
    (
        input: $InputTy:ty,
        output: $OutputTy:ty,
        $(#[$model_meta:meta])*
        model: $ModelAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident $(,)?
    ) => {
        $(#[$model_meta])*
        type $ModelAssoc: $crate::plugins::PurePluginModel<
                $InputTy,
                <Self::$ContextAssoc as $crate::plugins::ModelContext>::Context,
                $OutputTy,
            > + Default;

        $(#[$ctx_meta])*
        type $ContextAssoc:
            $crate::plugins::ModelContext;
    };

    // Mutable model arm
    (
        input: mut $MutateTy:ty,
        output: $OutputTy:ty,
        $(#[$model_meta:meta])*
        model: $ModelAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident $(,)?
    ) => {
        $(#[$model_meta])*
        type $ModelAssoc: $crate::plugins::MutablePluginModel<
                $MutateTy,
                <Self::$ContextAssoc as $crate::plugins::ModelContext>::Context,
                $OutputTy,
            > + Default;

        $(#[$ctx_meta])*
        type $ContextAssoc:
            $crate::plugins::ModelContext;
    };

    // Family model arm
    (
        input: $(mut)? $InputTy:ty,
        output: $OutputTy:ty,
        $(borrow: [$($borrow_lt:lifetime)* $(,)?],)?
        root: $Root:ident,
        $(#[$model_meta:meta])*
        family: $FamilyAssoc:ident,
        $(#[$ctx_meta:meta])*
        context: $ContextAssoc:ident
        $(, provides: [$($provider:tt)*])? $(,)?
    ) => {
        $(#[$model_meta])*
        type $FamilyAssoc $(<$($borrow_lt)*>)? : $Root<
                $InputTy,
                <Self::$ContextAssoc as $crate::plugins::ModelContext>::Context,
                $OutputTy,
            >;

        $(#[$ctx_meta])*
        type $ContextAssoc:
            $crate::plugins::ModelContext$(<Context: $($provider)*>)?;
    };

}

// ===============================================================================
// ``````````````````````````````` PLUGIN CONTEXT ````````````````````````````````
// ===============================================================================

/// Generates a stateless plugin context marker type for a plugin model or a
/// plugin family.
///
/// This macro defines:
/// 1. A zero-sized **marker struct** representing the plugin context provider.
/// 2. An implementation of the [`ModelContext`] trait for that marker,
///    including a constructor function `context()` returning a value on demand.
///
/// The `value` construction is on-demand (via [`ModelContext::context`]) which
/// allows the context to depend on other constants, statics, or computed values
/// while remaining compile-time friendly.
///
/// When `marker` is specified, the generated struct stores them in `PhantomData`
/// fields so the type system correctly tracks them without affecting runtime behavior.
///
/// - Type generics are tracked using `PhantomData<(T, ...)>`, preserving the
///   usual marker semantics for type parameters.
///
/// ## Syntax
///
/// ```ignore
/// plugin_context! {
///     #[attributes...]                // Optional: struct-level attributes (docs, derives, etc.)
///     name: pub ContextName,          // Required: visibility and name of the context marker struct
///     context: ContextType,           // Required: type representing the context data
///
///     marker: [T, U],                // Optional: phantom-data parameters applied to the marker
///     bounds: [T: Default, U: Clone], // Optional: trait bounds for the generated impl
///
///     value: ContextExpression,       // Required: expression producing the context value
/// }
/// ```
///
/// ## Attributes
/// Optional doc comments or other attributes can be attached to the generated
/// marker by placing them above the macro invocation.
///
/// ## Generics Support
///
/// - Only **type generics** (`T`, `U`, etc.) are supported via `marker: [...]`
/// - Lifetime generics are **not supported**
/// - The generics are tracked using `PhantomData` and do not affect runtime behavior
///
/// ## Examples
///
/// ### Basic Context
///
/// ```ignore
/// plugin_context! {
///     name: pub ElectionContext,
///     context: PhragmenConfig,
///     value: PhragmenConfig { sequential: true }
/// }
/// ```
///
/// ### Generic Context
///
/// ```ignore
/// plugin_context! {
///     name: pub GenericContext,
///     marker: [T],
///     context: PhragmenConfig<T>,
///     value: PhragmenConfig { sequential: true }
/// }
/// ```
///
#[macro_export]
macro_rules! plugin_context {
    (
        $(#[$meta:meta])*
        name: $vis:vis $Name:ident,
        context: $ContextType:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        value: $ContextLiteral:expr $(,)?
    ) => {
        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            []
            [$($($marker_gen),*)?]
        );

        impl $(< $($marker_gen,)* >)?
        $crate::plugins::ModelContext
        for $Name $(< $($marker_gen,)* >)?
        $(where $($bounds)*)?
        {
            type Context = $ContextType;

            fn context() -> Self::Context {
                $ContextLiteral
            }
        }
    };
}

// ===============================================================================
// ```````````````````````````````` PLUGIN OUTPUT ````````````````````````````````
// ===============================================================================

/// Generates a strongly-typed associated function that executes a plugin model
/// and returns its computed output.
///
/// The macro expands to a function which:
/// - Instantiates the plugin model using `Default`
/// - Constructs the execution context via [`ModelContext::context`]
/// - Wraps the input, model, and context into the appropriate execution source
/// - Executes the model and returns the resulting output
///
/// This removes boilerplate wiring of model construction, context resolution,
/// and execution, while preserving full compile-time type safety.
///
/// Exactly one of the following must be specified:
/// - `model` -> directly executes a concrete plugin model
/// - `root` + `family` + `child` -> resolves a model from a plugin family
///
/// Exactly one of:
/// - `input:`     -> immutable execution contract ([`PurePluginModel`])
/// - `input: mut` -> mutable execution contract ([`MutablePluginModel`])
///
/// Optional:
/// - `borrow` declares function-level generic parameters for input and output
/// specialization in family models.
///
/// ## Syntax
///
/// ### Immutable Concrete Model
///
/// ```ignore
/// plugin_output! {
///     pub fn run_model,        // Required: function visibility and name to generate
///     input: MyInput,          // Required: immutable input type
///     output: MyOutput,        // Required: output type produced by the model
///     model: MyModel,          // Required: concrete immutable plugin model type
///     context: MyContext,      // Required: context provider implementing ModelContext
/// }
/// ```
///
/// Expands to:
/// `pub fn run_model(input: MyInput) -> MyOutput { ... }`
///
/// ### Mutable Concrete Model
///
/// ```ignore
/// plugin_output! {
///     pub fn run_model_mut,    // Required: function visibility and name to generate
///     input: mut MyInput,      // Required: mutable input type
///     output: MyOutput,        // Required: output type produced by the model
///     model: MyModel,          // Required: concrete mutable plugin model type
///     context: MyContext,      // Required: context provider implementing ModelContext
/// }
/// ```
///
/// ### Immutable Family-Selected Model
///
/// ```ignore
/// plugin_output! {
///     pub fn run_family,       // Required: function visibility and name to generate
///     input: MyInput<'a>,      // Required: immutable input type
///     output: MyOutput,        // Required: output type produced by the model
///     borrow: ['a],            // Optional: function-level liftimes over input/output
///     root: MyFamilyRoot,      // Required: plugin family root trait
///     family: MyFamily,        // Required: concrete plugin family type
///     child: MyChildMarker,    // Required: child model identifier within the family
///     context: MyContext,      // Required: context provider implementing ModelContext
/// }
/// ```
///
/// ### Mutable Family-Selected Model
///
/// ```ignore
/// plugin_output! {
///     pub fn run_family_mut,   // Required: function visibility and name to generate
///     input: mut MyInput,      // Required: mutable input type
///     output: MyOutput<'a>,    // Required: output type produced by the model
///     borrow: ['a],            // Optional: function-level liftimes over input/output
///     root: MyFamilyRoot,      // Required: plugin family root trait
///     family: MyFamily,        // Required: concrete plugin family type
///     child: MyChildMarker,    // Required: child model identifier within the family
///     context: MyContext,      // Required: context provider implementing ModelContext
/// }
/// ```
///
/// ## Semantics
///
/// - `model` form executes a fixed concrete plugin model.
/// - `root` + `family` + `child` form defers model selection to the plugin family,
///   where the concrete model is resolved at compile time using the
///   `(Input, Context, Output, Family)` signature.
/// - `Context` acts as the nominal discriminator within a family, while
///   `Input` and `Output` are validated once concretely resolved at the call site.
/// - Mutable variants may mutate the input in-place, but resolution remains
///   entirely static through trait bounds.
/// - All resolution is performed at compile time; no dynamic dispatch is used.
///
/// ## Input / Output Constraints
///
/// - Plugin models (immutable & mutable) use **non-GAT types only**
///   - No generics
///   - No lifetime-based types (no GATs)
///
/// - Plugin families may use **lifetimes only**
///   - Enabled via `borrow: ['a]`
///   - Type generics are not supported
///
/// ```text
/// Model   -> concrete types only
/// Family  -> supports lifetimes only
/// ```
#[macro_export]
macro_rules! plugin_output {
    // Immutable model function
    (
        $(#[$meta:meta])*
        $vis:vis fn $name:ident,
        input: $Input:ty,
        output: $Output:ty,
        model: $ModelType:ty,
        context: $ContextType:ty $(,)?
    ) => {
        $(#[$meta])*
        $vis fn $name (input: $Input) -> $Output
        {
            // Instantiate the model
            let model = <$ModelType>::default();

            // Construct the context
            let context: <$ContextType as $crate::plugins::ModelContext>::Context =
                <$ContextType as $crate::plugins::ModelContext>::context();

            // Compute and return output
            $crate::plugins::PurePluginModel::<_, _, _>::compute(&model, input, &context)
        }
    };

    // Mutable model function
    (
        $(#[$meta:meta])*
        $vis:vis fn $name:ident,
        input: mut $Input:ty,
        output: $Output:ty,
        model: $ModelType:ty,
        context: $ContextType:ty $(,)?
    ) => {
        $(#[$meta])*
        $vis fn $name(input: &mut $Input) -> $Output
        {
            // Instantiate the model
            let model = <$ModelType>::default();

            // Construct the context
            let context: <$ContextType as $crate::plugins::ModelContext>::Context =
                <$ContextType as $crate::plugins::ModelContext>::context();

            // Compute and return output
            $crate::plugins::MutablePluginModel::<_, _, _>::compute_mut(&model, input, &context)
        }
    };

    // Immutable Family Child-specific function
    (
        $(#[$meta:meta])*
        $vis:vis fn $name:ident,
        input: $Input:ty,
        output: $Output:ty,
        $(borrow: [$($borrow_lt:lifetime)* $(,)?],)?
        root: $Root:ident,
        family: $Family:ty,
        child: $Child:ident,
        context: $ContextType:ty $(,)?
    ) => {
        #[doc = concat!(
            "Plugin invocation pure-function for the child - [`",
            stringify!($Child),
            "`] of the plugin family - [`",
            stringify!($Root),
            "`]"
        )]
        $(#[$meta])*
        $vis fn $name $(<$($borrow_lt)*>)? (input: $Input) -> $Output
        {
            // Resolve the concrete plugin model from the family using the root trait
            let model =
                <$Family as $Root<$Input,<$ContextType as $crate::plugins::ModelContext>::Context,
                    $Output>>::$Child::default();

            // Construct the execution context via the context provider.
            let context =
                <$ContextType as $crate::plugins::ModelContext>::context();

            // Execute the immutable plugin model and return the computed output.
            <<$Family as $Root<$Input,<$ContextType as $crate::plugins::ModelContext>::Context,
                    $Output>>::$Child
                    as $crate::plugins::PurePluginModel<
                        $Input,
                        <$ContextType as $crate::plugins::ModelContext>::Context,
                        $Output,
                    >>::compute(&model, input, &context)
        }
    };

    // Mutable Family Child-specific function
    (
        $(#[$meta:meta])*
        $vis:vis fn $name:ident,
        input: mut $Input:ty,
        output: $Output:ty,
        $(borrow: [$($borrow_lt:lifetime)* $(,)?],)?
        root: $Root:ident,
        family: $Family:ty,
        child: $Child:ident,
        context: $ContextType:ty $(,)?
    ) => {
        #[doc = concat!(
            "Plugin invocation mutable-function for the child - [`",
            stringify!($Child),
            "`] of the plugin family - [`",
            stringify!($Root),
            "`]"
        )]
        $(#[$meta])*
        $vis fn $name $(<$($borrow_lt)*>)? (input: &mut $Input) -> $Output
        {
            // Resolve the concrete plugin model from the family using the root trait
            let model =
                <$Family as $Root<$Input,<$ContextType as $crate::plugins::ModelContext>::Context,
                    $Output>>::$Child::default();

            // Construct the execution context via the context provider.
            let context =
                <$ContextType as $crate::plugins::ModelContext>::context();

            // Execute the immutable plugin model and return the computed output.
            <<$Family as $Root<$Input,<$ContextType as $crate::plugins::ModelContext>::Context,
                    $Output>>::$Child
                    as $crate::plugins::MutablePluginModel<
                        $Input,
                        <$ContextType as $crate::plugins::ModelContext>::Context,
                        $Output,
                    >>::compute_mut(&model, input, &context)
        }
    };
}

// ===============================================================================
// ```````````````````````````````` PLUGIN TESTS `````````````````````````````````
// ===============================================================================

/// Generates **table-driven unit tests** for plugin models.
///
/// It supports both **immutable** and **mutable** models, with or without context,
/// and with either explicit or inferred output types.
///
/// For each test case, the macro:
/// - Instantiates the plugin model using `Default`
/// - Constructs the required context (if any)
/// - Executes the model's computation (`compute` for immutable models,
///   `compute_mut` for mutable models)
/// - Asserts that the computed output matches the expected value
/// - Optionally asserts the final mutated input state for mutable models
///
/// Each test case expands into an **independent `#[test]` function**, ensuring
/// clear isolation and accurate failure reporting.
///
/// ## Features
///
/// - Supports **immutable** (`PurePluginModel`) and **mutable** (`MutablePluginModel`) models
/// - Supports **context-aware** and **context-free** plugin models
/// - Supports **explicit output types** or **implicit output = input**
/// - Optional assertion of the **mutated input value** for mutable models
/// - Generates one `#[test]` function per case
/// - Avoids boilerplate while preserving full type safety
/// - Mirrors the exact runtime execution contract of plugin models
///
/// ## Supported Forms
///
/// The macro supports the same four combinations for both immutable and mutable models:
///
/// | Context | Output |
/// +-------+------+
/// | Yes    | Explicit |
/// | Yes    | Inferred (output = input) |
/// | No     | Explicit |
/// | No     | Inferred (output = input) |
///
/// Mutable models are declared by using `input: mut Type`, which indicates that the
/// model will receive `&mut Type` and may transform the input in-place.
///
/// ## Syntax
///
/// ```ignore
/// plugin_test! {
///     model: ModelType,              // Plugin model type to test
///     input: InputType | mut InputType, // `mut` enables mutable model testing
///     output: OutputType,            // Optional: defaults to `InputType` if omitted
///     context: ContextType,          // Optional: required if model uses context
///     value: context_expr,           // Optional: expression constructing the context
///     cases: {
///         (test_name, input_expr, expected_output),
///         (test_name_2, input_expr_2, expected_output_2, expected_mutated_input), // mutable only
///     }
/// }
/// ```
///
/// - `model`: Plugin model type implementing `PurePluginModel` or `MutablePluginModel`
/// - `input`: Input type consumed by the model (`mut` indicates in-place mutation)
/// - `output`: Output type produced by the model (defaults to input type if omitted)
/// - `context`: Context type required by the model (omit for `()`)
/// - `value`: Expression that constructs the context instance
/// - `cases`: List of test tuples
///
/// Each case tuple has the form:
/// - `(name, input, expected_output)` for immutable models
/// - `(name, input, expected_output)` for mutable models when only output is asserted
/// - `(name, input, expected_output, expected_mutated_input)` to also verify
///   the final mutated state of the input
///
/// When the fourth element is provided, the macro additionally checks that the
/// input was correctly transformed in-place.
///
/// ## Notes
///
/// - Each test case expands into a **separate `#[test]` function**
/// - Context and input types must match the model's trait implementation
/// - Output inference (`output = input`) follows the same rule as `plugin_model!`
/// - Compilation fails if input, context, or output types are incompatible
///
/// This macro is intended for **testing plugin model logic in isolation** and
/// should not be used for testing pallet storage, dispatchables, or runtime configuration.
#[macro_export]
macro_rules! plugin_test {
    // Helper: choose output type, defaulting to input if not provided
    (@output_ty $InputType:ty) => { $InputType };
    (@output_ty $InputType:ty, $OutputType:ty) => { $OutputType };

    // WITH CONTEXT, EXPLICIT OUTPUT
    (
        model: $ModelName:ty,
        input: $InputTy:ty,
        output: $OutputTy:ty,
        context: $ContextTy:ty,
        value: $ContextExpr:expr,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr)),* $(,)? }
    ) => {
        $(
            #[test] // generate a #[test] function for each case
            fn $test_name() {
                let model = <$ModelName>::default();           // instantiate model
                let context: $ContextTy = $ContextExpr;        // construct context
                let input: $InputTy = $input_expr;             // test input
                let result: $OutputTy =                        // compute output
                    <$ModelName as $crate::plugins::PurePluginModel<
                        $InputTy,
                        $ContextTy,
                        $OutputTy
                    >>::compute(&model, input, &context);
                assert_eq!(result, $expected);                // verify result
            }
        )*
    };

    // WITH CONTEXT, OUTPUT = INPUT
    (
        model: $ModelName:ty,
        input: $InputTy:ty,
        context: $ContextTy:ty,
        value: $ContextExpr:expr,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                type Output = $InputTy;
                let model = <$ModelName>::default();
                let context: $ContextTy = $ContextExpr;
                let input: $InputTy = $input_expr;
                let result: Output =
                    <$ModelName as $crate::plugins::PurePluginModel<
                        $InputTy,
                        $ContextTy,
                        Output
                    >>::compute(&model, input, &context);
                assert_eq!(result, $expected);
            }
        )*
    };

    // No Context, EXPLICIT OUTPUT
    (
        model: $ModelName:ty,
        input: $InputTy:ty,
        output: $OutputTy:ty,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                let model = <$ModelName>::default();
                let context: () = Default::default();
                let input: $InputTy = $input_expr;
                let result: $OutputTy =
                    <$ModelName as $crate::plugins::PurePluginModel<
                        $InputTy,
                        (),
                        $OutputTy
                    >>::compute(&model, input, &context);
                assert_eq!(result, $expected);
            }
        )*
    };

    // No Context, OUTPUT = INPUT
    (
        model: $ModelName:ty,
        input: $InputTy:ty,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                type Output = $InputTy;
                let model = <$ModelName>::default();
                let context: () = Default::default();
                let input: $InputTy = $input_expr;
                let result: Output =
                    <$ModelName as $crate::plugins::PurePluginModel<
                        $InputTy,
                        (),
                        Output
                    >>::compute(&model, input, &context);
                assert_eq!(result, $expected);
            }
        )*
    };

    // WITH CONTEXT, EXPLICIT OUTPUT (MUTABLE)
    (
        model: $ModelName:ty,
        input: mut $InputTy:ty,
        output: $OutputTy:ty,
        context: $ContextTy:ty,
        value: $ContextExpr:expr,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr $(, $expected_input:expr)?)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                let model = <$ModelName>::default();
                let context: $ContextTy = $ContextExpr;
                let mut input: $InputTy = $input_expr;

                let result: $OutputTy =
                    <$ModelName as $crate::plugins::MutablePluginModel<
                        $InputTy,
                        $ContextTy,
                        $OutputTy
                    >>::compute_mut(&model, &mut input, &context);

                assert_eq!(result, $expected);

                $(
                    assert_eq!(input, $expected_input);
                )?
            }
        )*
    };

    // WITH CONTEXT, OUTPUT = INPUT (MUTABLE)
    (
        model: $ModelName:ty,
        input: mut $InputTy:ty,
        context: $ContextTy:ty,
        value: $ContextExpr:expr,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr $(, $expected_input:expr)?)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                type Output = $InputTy;
                let model = <$ModelName>::default();
                let context: $ContextTy = $ContextExpr;
                let mut input: $InputTy = $input_expr;

                let result: Output =
                    <$ModelName as $crate::plugins::MutablePluginModel<
                        $InputTy,
                        $ContextTy,
                        Output
                    >>::compute_mut(&model, &mut input, &context);

                assert_eq!(result, $expected);

                $(
                    assert_eq!(input, $expected_input);
                )?
            }
        )*
    };

    // No Context, EXPLICIT OUTPUT (MUTABLE)
    (
        model: $ModelName:ty,
        input: mut $InputTy:ty,
        output: $OutputTy:ty,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr $(, $expected_input:expr)?)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                let model = <$ModelName>::default();
                let context: () = Default::default();
                let mut input: $InputTy = $input_expr;

                let result: $OutputTy =
                    <$ModelName as $crate::plugins::MutablePluginModel<
                        $InputTy,
                        (),
                        $OutputTy
                    >>::compute_mut(&model, &mut input, &context);

                assert_eq!(result, $expected);

                $(
                    assert_eq!(input, $expected_input);
                )?
            }
        )*
    };

    // No Context, OUTPUT = INPUT (MUTABLE)
    (
        model: $ModelName:ty,
        input: mut $InputTy:ty,
        cases: { $(($test_name:ident, $input_expr:expr, $expected:expr $(, $expected_input:expr)?)),* $(,)? }
    ) => {
        $(
            #[test]
            fn $test_name() {
                type Output = $InputTy;
                let model = <$ModelName>::default();
                let context: () = Default::default();
                let mut input: $InputTy = $input_expr;

                let result: Output =
                    <$ModelName as $crate::plugins::MutablePluginModel<
                        $InputTy,
                        (),
                        Output
                    >>::compute_mut(&model, &mut input, &context);

                assert_eq!(result, $expected);

                $(
                    assert_eq!(input, $expected_input);
                )?
            }
        )*
    };
}

// ===============================================================================
// ```````````````````````````````` PLUGIN MODEL `````````````````````````````````
// ===============================================================================

/// Defines a plugin model in a fully generic, and type-safe way.
///
/// The macro generates:
/// - A `struct` representing the plugin model (deriving `Default`)
/// - An implementation of either [`PurePluginModel`] or [`MutablePluginModel`]
///
/// This removes repetitive boilerplate while ensuring that the relationships
/// between input, output, and context types are enforced at compile time.
///
/// Exactly one of:
/// - `input:`     -> immutable model (`PurePluginModel`)
/// - `input: mut` -> mutable model (`MutablePluginModel`)
///
/// Optionally:
/// - `context:` enables contextual execution (otherwise context defaults to `()`)
/// - `root:` + `child:` attaches the model to a plugin family for late resolution
///
/// ## Syntax
///
/// ### Immutable Model (No Context)
///
/// ```ignore
/// plugin_model! {
///     name: pub ModelName,          // Required: struct visibility and struct name of the plugin model
///     input: InputType,             // Required: generic immutable input type
///     output: OutputType,           // Optional: output type (defaults to input if omitted)
///     others: [T1, T2],             // Optional: additional generic parameters
///     bounds: [TraitBounds],        // Required: trait bounds for generics
///     compute: |input, ctx| { ... } // Required: compute logic (`ctx` is `()`)
/// }
/// ```
///
/// ### Immutable Model with Context
///
/// ```ignore
/// plugin_model! {
///     name: pub ModelName,          // Required: struct visibility and struct name of the plugin model
///     input: InputType,             // Required: generic immutable input type
///     output: OutputType,           // Optional: output type (defaults to input if omitted)
///     others: [T1, T2],             // Optional: additional generic parameters
///     context: ContextType,         // Required: context struct used during execution
///     bounds: [TraitBounds],        // Required: trait bounds for generics
///     compute: |input, ctx| { ... } // `ctx: &ContextType`
/// }
/// ```
///
/// ### Mutable Model
///
/// ```ignore
/// plugin_model! {
///     name: pub ModelName,          // Required: struct visibility and struct name of the plugin model
///     input: mut InputType,         // Required: mutable input type (`&mut InputType`)
///     output: OutputType,           // Optional: output type (defaults to immutable input type)
///     others: [T1, T2],             // Optional: additional generic parameters
///     context: ContextType,         // Optional: context struct (defaults to `()`)
///     bounds: [TraitBounds],        // Required: trait bounds for generics
///     compute: |input, ctx| { ... } // Uses `compute_mut`
/// }
/// ```
///
/// ## Output Type Semantics
///
/// - If `output` is omitted, the output type defaults to the **immutable input type**,
///   even for mutable models (it does **not** default to `()`).
/// - This rule applies to both immutable and mutable plugin models.
/// - If a unit output `()` is desired, it must be specified explicitly as:
///
/// ```ignore
/// output: Output,
/// bounds: [Output: Default]
/// ```
///
/// The `Default` bound is required so `compute`'s block can construct the output value.
///
/// ## Semantics
///
/// - Each model is fully generic over its input, output, and optional context.
/// - If `context` is omitted, the model uses `()` as its context type.
/// - If `root` and `child` are provided, the model becomes a member of a plugin
///   family and is selected indirectly using the `(root, child, context)`
///   resolution lattice.
/// - Immutable variants use `compute` with shared input references.
/// - Mutable variants use `compute_mut` and may mutate the input in-place.
///
/// All constraints are enforced purely through trait bounds and associated
/// types, guaranteeing compile-time correctness of model wiring and resolution.
#[macro_export]
macro_rules! plugin_model {

    // Helper Rule: `@output_ty`
    (@output_ty $Input:tt) => { $Input };
    (@output_ty $Input:tt, $Output:tt) => { $Output };

    // Variant 1: No Context, Single Input, Single Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: $Input:ident,
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $Output)?
        > $crate::plugins::PurePluginModel<
            $Input,
            (),
            $crate::plugin_model!(@output_ty $Input $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: $Input,
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty $Input $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 2: No Context, Single Input, Tuple Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $($Output),+)?
        > $crate::plugins::PurePluginModel<
            $Input,
            (),
            $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: $Input,
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 3: No Context, Tuple Input, Single Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: ($($Input:ident),+),
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $Output)?
        > $crate::plugins::PurePluginModel<
            ($($Input),+),
            (),
            $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: ($($Input),+),
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 4: No Context, Tuple Input, Tuple Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $crate::plugins::PurePluginModel<
            ($($Input),+),
            (),
            $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: ($($Input),+),
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 5: Context, Single Input, Single Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: $Input:ident,
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $Output)?
        > $crate::plugins::PurePluginModel<
            $Input,
            $Context,
            $crate::plugin_model!(@output_ty $Input $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: $Input,
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty $Input $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 6: Context, Single Input, Tuple Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $($Output),+)?
        > $crate::plugins::PurePluginModel<
            $Input,
            $Context,
            $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: $Input,
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 7: Context, Tuple Input, Single Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: ($($Input:ident),+),
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $Output)?
        > $crate::plugins::PurePluginModel<
            ($($Input),+),
            $Context,
            $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: ($($Input),+),
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 8: Context, Tuple Input, Tuple Output
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $crate::plugins::PurePluginModel<
            ($($Input),+),
            $Context,
            $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute(
                &self,
                $input_arg: ($($Input),+),
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 9: No Context, Single Input, Single Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut $Input:ident,
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $Output)?
        > $crate::plugins::MutablePluginModel<
            $Input,
            (),
            $crate::plugin_model!(@output_ty $Input $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut $Input,
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty $Input $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 10: No Context, Single Input, Tuple Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $($Output),+)?
        > $crate::plugins::MutablePluginModel<
            $Input,
            (),
            $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut $Input,
                $ctx_arg: &()
            ) -> $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 11: No Context, Tuple Input, Single Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut ($($Input:ident),+),
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $Output)?
        > $crate::plugins::MutablePluginModel<
            ($($Input),+),
            (),
            $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut ($($Input),+),
                $ctx_arg: &(),
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 12: No Context, Tuple Input, Tuple Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $crate::plugins::MutablePluginModel<
            ($($Input),+),
            (),
            $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut ($($Input),+),
                $ctx_arg: &(),
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 13: Context, Single Input, Single Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut $Input:ident,
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $Output)?
        > $crate::plugins::MutablePluginModel<
            $Input,
            $Context,
            $crate::plugin_model!(@output_ty $Input $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut $Input,
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty $Input $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 14: Context, Single Input, Tuple Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $Input
            $(, $($Output),+)?
        > $crate::plugins::MutablePluginModel<
            $Input,
            $Context,
            $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut $Input,
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty $Input $(, ($($Output),+))?) {
                $body
            }
        }
    };

    // Variant 15: Context, Tuple Input, Single Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut ($($Input:ident),+),
        $(output: $Output:ident ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $Output)?
        > $crate::plugins::MutablePluginModel<
            ($($Input),+),
            $Context,
            $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut ($($Input),+),
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, $Output)?) {
                $body
            }
        }
    };

    // Variant 16: Context, Tuple Input, Tuple Output (MUTABLE)
    (
        $(#[$name_meta:meta])*
        name: $vis:vis $ModelName:ident,
        input: mut ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        $(others: [$($other_gen:tt),* $(,)? ] ,)?
        context: $Context:ty,
        bounds: [$($bounds:tt)*],
        $(#[$compute_meta:meta])*
        compute: |$input_arg:ident, $ctx_arg:ident| $body:block $(,)?
    ) => {
        #[derive(Debug, Default)]
        $(#[$name_meta])*
        $vis struct $ModelName;

        impl<
            $($($other_gen ,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $crate::plugins::MutablePluginModel<
            ($($Input),+),
            $Context,
            $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        > for $ModelName
        where
            $($bounds)*
        {
            $(#[$compute_meta])*
            fn compute_mut(
                &self,
                $input_arg: &mut ($($Input),+),
                $ctx_arg: &$Context
            ) -> $crate::plugin_model!(@output_ty ($($Input),+) $(, ($($Output),+))?) {
                $body
            }
        }

    };

}

// ===============================================================================
// ``````````````````````````````` DECLARE FAMILY ````````````````````````````````
// ===============================================================================

/// Declares a plugin model family using marker types.
///
/// This macro generates a **family root trait** and one or more **child markers**
/// used to represent operations within that family.
///
/// The generated markers are purely type-level and contain no runtime data.
/// Concrete plugin implementations attach to a `(Root, Child)` pair via
/// [`plugin_model!`](crate::plugin_model), allowing models to be selected later through context
/// and trait bounds.
///
/// ## Parameters
/// - `root`: Declares the **family root trait**.
///   - Can include a visibility modifier (`pub`, `pub(crate)`, etc.).
///   - The same visibility is applied to all child marker structs.
///   - Prefix with `mut` to create a mutable plugin family.
/// - `child`: A list of **child marker types** representing operations
///   within the family.
///
/// ## Syntax
///
/// ```ignore
/// declare_family! {
///     root: pub FamilyRoot,
///     child: [OperationA, OperationB, OperationC]
/// }
/// ```
///
/// Mutable families use the `mut` keyword:
///
/// ```ignore
/// declare_family! {
///     root: mut pub FamilyRoot,
///     child: [OperationA, OperationB]
/// }
/// ```
///
/// ## Generated Types
///
/// The macro generates:
///
/// - A **child marker struct** for each entry in `child`.
/// - A **family root trait** defining associated plugin model types
///   for each child operation.
///
/// The child marker structs are zero-sized types used purely as
/// identifiers for operations within the family.
///
/// The root trait declares an associated model type per child:
///
/// - Immutable families require models implementing [`PurePluginModel`].
/// - Mutable families require models implementing [`MutablePluginModel`].
///
/// Each associated type corresponds to a concrete plugin implementation
/// bound to that operation.
///
/// These associated model types are later used by [`plugin_model!`](crate::plugin_model) to bind
/// concrete implementations to specific `(Root, Child)` combinations.
///
/// ## Example
///
/// ```ignore
/// declare_family! {
///     root: pub VotingFamily,
///     child: [Phragmen, STV]
/// }
/// ```
///
/// Expands roughly to:
///
/// ```ignore
/// pub struct Phragmen;
/// pub struct STV;
///
/// pub trait VotingFamily<Input, Context, Output> {
///     type Phragmen: PurePluginModel<Input, Context, Output>;
///     type STV: PurePluginModel<Input, Context, Output>;
/// }
/// ```
///
/// Mutable family example:
///
/// ```ignore
/// declare_family! {
///     root: mut pub StorageFamily,
///     child: [Insert, Remove]
/// }
/// ```
///
/// Expands roughly to:
///
/// ```ignore
/// pub struct Insert;
/// pub struct Remove;
///
/// pub trait StorageFamily<Input, Context, Output> {
///     type Insert: MutablePluginModel<Input, Context, Output>;
///     type Remove: MutablePluginModel<Input, Context, Output>;
/// }
/// ```
#[macro_export]
macro_rules! declare_family {
    // Immutable Family
    (
        $(#[$meta:meta])*
        root: $vis:vis $Root:ident,
        child: [
            $(
                $(#[$child_meta:meta])*
                $Child:ident
            ),+ $(,)?
        ]
    ) => {
        $(
            $(#[$child_meta])*
            $vis struct $Child;
        )+

        $(#[$meta])*
        $vis trait $Root<Input, Context, Output>{
            $(
                $(#[$child_meta])*
                type $Child: $crate::plugins::PurePluginModel<Input, Context, Output>;
            )+
        }

    };

    // Mutable Family
    (
        $(#[$meta:meta])*
        root: mut $vis:vis $Root:ident,
        child: [
            $(
                $(#[$child_meta:meta])*
                $Child:ident
            ),+ $(,)?
        ]
    ) => {
        $(
            $(#[$child_meta])*
            $vis struct $Child;
        )+

        $(#[$meta])*
        $vis trait $Root<Input, Context, Output> {
            $(
                $(#[$child_meta])*
                type $Child: $crate::plugins::MutablePluginModel<Input, Context, Output>;
            )+
        }

    };
}

// ===============================================================================
// ```````````````````````````````` DEFINE FAMILY ````````````````````````````````
// ===============================================================================

/// Declares a concrete **plugin family implementation** for a given family root.
///
/// This macro generates:
/// 1. A **family marker struct** representing a concrete implementation of a
///    plugin family.
/// 2. An implementation of the **family root trait** mapping each declared
///    child operation to a concrete plugin model.
///
/// The generated family struct is purely a **type-level marker** and contains
/// no runtime data. It is used to bind concrete plugin models to a specific
/// `(FamilyType, Child)` combination through associated types.
///
/// When `borrow` are specified, the generated family marker stores them
/// in `PhantomData` fields so the type system correctly tracks them
/// without affecting runtime behavior.
///
/// ## Syntax
///
/// ### Family With Context
///
/// ```ignore
/// define_family! {
///     root: FamilyRoot,             // Required: family root trait
///
///     family: pub MyFamily,         // Required: visibility and concrete family marker struct
///     borrow: ['a],                 // Optional: lifetime parameters for family marker
///
///     input: Input,                 // Required: input type parameter
///     output: Output,               // Optional: output type (defaults to input if omitted)
///
///     context: MyContext,           // Required: context type
///     marker: [T],                  // Optional: generic parameters for the context
///
///     bounds: [T: Clone],           // Optional: trait bounds for the generated impl
///
///     child: [                      // Required: child -> model mapping
///         OperationA => ModelA,
///         OperationB => ModelB,
///     ]
/// }
/// ```
///
/// ### Family Without Context
///
/// ```ignore
/// define_family! {
///     root: FamilyRoot,           // Required: family root trait
///
///     family: pub MyFamily,       // Required: visibility and concrete family marker struct
///     borrow: ['a],               // Optional: lifetime parameters for family marker
///
///     input: Input,               // Required: input type parameter
///     output: Output,             // Optional: output type (defaults to input if omitted)
///
///     bounds: [T: Clone],         // Optional: trait bounds for the generated impl
///
///     child: [                    // Required: child -> model mapping
///         OperationA => ModelA,
///         OperationB => ModelB,
///     ]
/// }
/// ```
///
/// In the second form, the context parameter of the root trait defaults to `()`.
///
/// ## Lifetimes and Generics
///
/// - `borrow` apply to the **family marker type**
///   - used to model execution-time borrowing
///   - stored via `PhantomData`
///
/// ```ignore
/// borrow: ['a]
/// ```
///
/// - `marker` apply only when a `context` is specified
///   - used to parameterize the **context type**
///   - introduced on the generated `impl`, not the family struct
///
/// ```ignore
/// context: MyContext,
/// marker: [T]
/// ```
///
/// When no `context` is provided:
///
/// - the context defaults to `()`
/// - `marker` is not used
///
/// ## Example
///
/// ```ignore
///
/// // ----- Crate A ------
///
/// declare_family! {
///     root: pub VotingFamily,
///     child: [Phragmen, STV]
/// }
///
/// // ----- Crate B ------
///
/// plugin_model! {
///     name: PhragmenModel,
///     ...
/// }
///
/// plugin_model! {
///     name: STVModel,
///     ...
/// }
///
/// define_family! {
///     root: VotingFamily,
///
///     family: pub RuntimeVoting,
///     input: AccountId,
///     output: Balance,
///     context: RuntimeContext,
///
///     child: [
///         Phragmen => PhragmenModel,
///         STV => STVModel,
///     ]
/// }
/// ```
///
/// This binds the `Phragmen` and `Approval` operations of the
/// `VotingFamily` root to concrete plugin models for the
/// `RuntimeVoting` family implementation.
#[macro_export]
macro_rules! define_family {

    // Helper Rule: `@output_ty`
    (@output_ty $Input:tt) => { $Input };
    (@output_ty $Input:tt, $Output:tt) => { $Output };


    // With Context, Single Input, Single Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: $Input:ident,
        $(output: $Output:ident ,)?
        context: $Context:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($($marker_gen,)*)?
            $Input
            $(, $Output)?
        > $Root<
            $Input,
            $Context,
            $crate::define_family!(@output_ty $Input $(, $Output)?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+
        }
    };

    // With Context, Single Input, Tuple Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        context: $Context:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($($marker_gen,)*)?
            $Input
            $(, $($Output),+)?
        > $Root<
            $Input,
            $Context,
            $crate::define_family!(@output_ty $Input $(, ($($Output),+))?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+

        }
    };

    // With Context, Tuple Input, Single Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: ($($Input:ident),+),
        $(output: $Output:ident ,)?
        context: $Context:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($($marker_gen,)*)?
            $($Input),+
            $(, $Output)?
        > $Root<
            ($($Input),+),
            $Context,
            $crate::define_family!(@output_ty ($($Input),+) $(, $Output)?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+

        }
    };

    // With Context, Tuple Input, Tuple Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        context: $Context:ty,
        $(marker: [$($marker_gen:ident),* $(,)?],)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($($marker_gen,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $Root<
            ($($Input),+),
            $Context,
            $crate::define_family!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+

        }
    };

    // No Context, Single Input, Single Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: $Input:ident,
        $(output: $Output:ident ,)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $Input
            $(, $Output)?
        > $Root<
            $Input,
            (),
            $crate::define_family!(@output_ty $Input $(, $Output)?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+
        }
    };

    // No Context, Single Input, Tuple Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: $Input:ident,
        $(output: ($($Output:ident),+) ,)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $Input
            $(, $($Output),+)?
        > $Root<
            $Input,
            (),
            $crate::define_family!(@output_ty $Input $(, ($($Output),+))?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+
        }
    };

    // No Context, Tuple Input, Single Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: ($($Input:ident),+),
        $(output: $Output:ident ,)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($Input),+
            $(, $Output)?
        > $Root<
            ($($Input),+),
            (),
            $crate::define_family!(@output_ty ($($Input),+) $(, $Output)?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+
        }
    };

    // No Context, Tuple Input, Tuple Output
    (
        root: $Root:ident,
        $(#[$meta:meta])*
        family: $vis:vis $Name:ident,
        $(borrow: [$($borrow_lt:lifetime),* $(,)?],)?
        input: ($($Input:ident),+),
        $(output: ($($Output:ident),+) ,)?
        $(bounds: [$($bounds:tt)*],)?
        child: [
            $($child:ident => $model:ty,)+
        $(,)? ] $(,)?
    ) => {

        $crate::__phantom_struct!(
            $(#[$meta])*
            #[allow(unused)]
            $vis
            $Name
            [$($($borrow_lt),*)?]
            []
        );

        impl<
            $($($borrow_lt,)*)?
            $($Input),+
            $(, $($Output),+)?
        > $Root<
            ($($Input),+),
            (),
            $crate::define_family!(@output_ty ($($Input),+) $(, ($($Output),+))?)
        >
        for $Name$(<
            $($borrow_lt,)*
        >)?
        $(where $($bounds)*)?
        {
            $(
                type $child = $model;
            )+
        }
    };

}

// ===============================================================================
// ```````````````````````````````` HELPER MACROS ````````````````````````````````
// ===============================================================================

/// Generates a zero-sized or PhantomData-backed marker struct.
///
/// This is an internal helper used by `plugin_context`, `define_family`,
/// and other macros that need to produce marker structs which may carry
/// lifetime or type parameters purely at the type level without any
/// runtime storage.
///
/// ## Syntax
///
/// ```ignore
/// __phantom_struct!(
///     #[attributes]   // optional
///     VISIBILITY      // pub, pub(crate), or empty
///     NAME            // struct identifier
///     [LIFETIMES]     // e.g. ['a, 'b] or []
///     [GENERICS]      // e.g. [T, U]   or []
/// )
/// ```
///
/// ## Variants
///
/// | Lifetimes | Generics | Generated struct                               |
/// |-----------|----------|------------------------------------------------|
/// | `[]`      | `[]`     | `struct Foo;`                                  |
/// | `['a]`    | `[]`     | `struct Foo<'a>(PhantomData<(&'a (),)>)`       |
/// | `[]`      | `[T]`    | `struct Foo<T>(PhantomData<(T,)>)`             |
/// | `['a]`    | `[T]`    | `struct Foo<'a, T>(PhantomData<(T, &'a ())>)`  |
///
#[macro_export]
macro_rules! __phantom_struct {

    // Arm 1: No lifetimes, no generics -> plain unit struct.
    (
        $(#[$meta:meta])*
        $vis:vis
        $Name:ident
        []
        []
    ) => {
        $(#[$meta])*
        $vis struct $Name;
    };

    // Arm 2: Lifetimes only -> struct with a PhantomData reference tuple
    // that is covariant over each declared lifetime independently.
    (
        $(#[$meta:meta])*
        $vis:vis
        $Name:ident
        [$($lt:lifetime),+ $(,)?]
        []
    ) => {
        $(#[$meta])*
        $vis struct $Name<$($lt),*>(
            core::marker::PhantomData<($(&$lt (),)*)>
        );
    };

    // Arm 3: Generics only -> struct with a PhantomData tuple field that
    // tracks each type parameter independently.
    (
        $(#[$meta:meta])*
        $vis:vis
        $Name:ident
        []
        [$($gen:ident),+ $(,)?]
    ) => {
        $(#[$meta])*
        $vis struct $Name<$($gen),*>(
            core::marker::PhantomData<($($gen,)*)>
        );
    };

    // Arm 4: Both lifetimes and generics -> single PhantomData tuple field
    // combining both, so the struct has one field instead of two and the
    // variance of each parameter remains independent.
    (
        $(#[$meta:meta])*
        $vis:vis
        $Name:ident
        [$($lt:lifetime),+ $(,)?]
        [$($gen:ident),+ $(,)?]
    ) => {
        $(#[$meta])*
        $vis struct $Name<$($lt),*, $($gen),*>(
            core::marker::PhantomData<($($gen,)* $(&$lt (),)*)>
        );
    };
}

// ===============================================================================
// `````````````````````````````````` MOCK TEST ``````````````````````````````````
// ===============================================================================

#[cfg(test)]
#[allow(unused)]
mod tests {

    // -------------------------------------------------------------------------------
    // ``````````````````````````````````` IMPORTS ```````````````````````````````````
    // -------------------------------------------------------------------------------

    // --- Local crate imports ---
    use super::*;

    // --- Core / Std ---
    use core::marker::PhantomData;
    use std::mem::take;

    // --- Substrate primitives ---
    use sp_arithmetic::traits::AtLeast8BitUnsigned;

    // -------------------------------------------------------------------------------
    // ``````````````````````````````````` STRUCTS ```````````````````````````````````
    // -------------------------------------------------------------------------------

    //--- Mock structs ---

    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct BasicConfig {
        value: u8,
    }

    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct GenericConfig<T> {
        value: T,
    }

    // -------------------------------------------------------------------------------
    // ``````````````````````````````` PLUGIN CONTEXT ````````````````````````````````
    // -------------------------------------------------------------------------------

    //--- Basic context form ---

    plugin_context! {
        name: pub BasicConfigProvider,
        context: BasicConfig,
        value: BasicConfig {value: 10}
    }

    #[test]
    fn plugin_context_basic_form_returns_expected_context() {
        let ctx = <BasicConfigProvider as ModelContext>::context();
        assert_eq!(ctx, BasicConfig { value: 10 });
    }

    //--- Generic marker form ---

    plugin_context! {
        name: GenericConfigProvider,
        context: GenericConfig<T>,
        marker: [T],
        bounds: [T: AtLeast8BitUnsigned + Default],
        value: GenericConfig {value: T::default()}
    }

    #[test]
    fn plugin_context_marker_form_returns_expected_context() {
        let ctx = <GenericConfigProvider<u8> as ModelContext>::context();
        assert_eq!(ctx, GenericConfig { value: 0u8 });
    }

    // -------------------------------------------------------------------------------
    // ```````````````````````````````` PLUGIN MODEL `````````````````````````````````
    // -------------------------------------------------------------------------------

    //--- Variant 1: No Context, Single Input, Single Output ---

    plugin_model! {
        name: pub PureNoCtxSingleSingle,
        input: Input,
        output: Output,
        bounds: [Input: Into<u8>, Output: From<u8>],
        compute: |input, _ctx| {
            let x = input.into();
            Output::from(x + 1)
        }
    }

    plugin_test! {
        model: PureNoCtxSingleSingle,
        input: u8,
        output: u8,
        cases: {
            (pure_no_ctx_single_single_case1, 10, 11),
            (pure_no_ctx_single_single_case2, 0, 1),
        }
    }

    plugin_test! {
        model: PureNoCtxSingleSingle,
        input: u8,
        cases: {
            (pure_no_ctx_single_single_case3, 99, 100),
            (pure_no_ctx_single_single_case4, 24, 25),
        }
    }

    //--- Variant 2: No Context, Single Input, Tuple Output ---

    plugin_model! {
        name: pub PureNoCtxSingleTuple,
        input: Input,
        output: (OutA, OutB),
        bounds: [Input: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        compute: |input, _ctx| {
            let x = input.into();
            (OutA::from(x), OutB::from(x + 1))
        }
    }

    plugin_test! {
        model: PureNoCtxSingleTuple,
        input: u8,
        output: (u8, u8),
        cases: {
            (pure_no_ctx_single_tuple_case1, 10, (10, 11)),
            (pure_no_ctx_single_tuple_case2, 0, (0, 1)),
        }
    }

    //--- Variant 3: No Context, Tuple Input, Single Output ---

    plugin_model! {
        name: pub PureNoCtxTupleSingle,
        input: (InpA, InpB),
        output: Output,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, Output: From<u8>],
        compute: |input, _ctx| {
            let (a, b) = input;
            Output::from(a.into() + b.into())
        }
    }

    plugin_test! {
        model: PureNoCtxTupleSingle,
        input: (u8, u8),
        output: u8,
        cases: {
            (pure_no_ctx_tuple_single_case1, (10, 11), 21),
            (pure_no_ctx_tuple_single_case2, (0, 5), 5),
        }
    }

    //--- Variant 4: No Context, Tuple Input, Tuple Output ---

    plugin_model! {
        name: pub PureNoCtxTupleTuple,
        input: (InpA, InpB),
        output: (OutA, OutB),
        bounds: [InpA: Into<u8>, InpB: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        compute: |input, _ctx| {
            let (a, b) = input;
            (OutA::from(a.into() * 10), OutB::from(b.into() * 10))
        }
    }

    plugin_test! {
        model: PureNoCtxTupleTuple,
        input: (u8, u8),
        output: (u8, u8),
        cases: {
            (pure_no_ctx_tuple_tuple_case1, (5, 10), (50, 100)),
            (pure_no_ctx_tuple_tuple_case2, (1, 0), (10, 0)),
        }
    }

    // --- Variant 5: Context, Single Input, Single Output ---

    plugin_model! {
        name: pub PureCtxSingleSingle,
        input: Input,
        output: Output,
        context: BasicConfig,
        bounds: [Input: Into<u8>, Output: From<u8>],
        compute: |input, ctx| {
            let v = ctx.value;
            Output::from(input.into() + v)
        }
    }

    plugin_test! {
        model: PureCtxSingleSingle,
        input: u8,
        output: u8,
        context: BasicConfig,
        value: BasicConfig{ value: 10 },
        cases: {
            (pure_ctx_single_single_case1, 10, 20),
            (pure_ctx_single_single_case2, 1, 11),
        }
    }

    plugin_test! {
        model: PureCtxSingleSingle,
        input: u8,
        context: BasicConfig,
        value: BasicConfig{ value: 10 },
        cases: {
            (pure_ctx_single_single_case3, 90, 100),
            (pure_ctx_single_single_case4, 0, 10),
        }
    }

    //--- Variant 6: Context, Single Input, Tuple Output ---

    plugin_model! {
        name: pub PureCtxSingleTuple,
        input: Input,
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [Input: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        compute: |input, ctx| {
            let x = input.into();
            let v = ctx.value;
            (OutA::from(x), OutB::from(x + v))
        }
    }

    plugin_test! {
        model: PureCtxSingleTuple,
        input: u8,
        output: (u8, u8),
        context: BasicConfig,
        value: BasicConfig{ value: 1 },
        cases: {
            (pure_ctx_single_tuple_case1, 5, (5, 6)),
            (pure_ctx_single_tuple_case2, 1, (1, 2)),
        }
    }

    //--- Variant 7: Context, Tuple Input, Single Output ---

    plugin_model! {
        name: pub PureCtxTupleSingle,
        input: (InpA, InpB),
        output: Output,
        context: BasicConfig,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, Output: From<u8>],
        compute: |input, ctx| {
            let (a, b) = input;
            let v = ctx.value;
            Output::from(a.into() + b.into() + v)
        }
    }

    plugin_test! {
        model: PureCtxTupleSingle,
        input: (u8, u8),
        output: u8,
        context: BasicConfig,
        value: BasicConfig { value: 10},
        cases: {
            (pure_ctx_tuple_single_case1, (10, 10), 30),
            (pure_ctx_tuple_single_case2, (5, 30), 45),
        }
    }

    //--- Variant 8: Context, Tuple Input, Tuple Output ---

    plugin_model! {
        name: pub PureCtxTupleTuple,
        input: (InpA, InpB),
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        compute: |input, ctx| {
            let (a, b) = input;
            let v = ctx.value;
            (OutA::from(a.into() + v), OutB::from(b.into() + v))
        }
    }

    plugin_test! {
        model: PureCtxTupleTuple,
        input: (u8, u8),
        output: (u8, u8),
        context: BasicConfig,
        value: BasicConfig { value: 5 },
        cases: {
            (pure_ctx_tuple_tuple_case1, (5, 10), (10, 15)),
            (pure_ctx_tuple_tuple_case2, (1, 0), (6, 5)),
        }
    }

    //--- Variant 9: No Context, Single Input, Single Output (MUTABLE) ---

    plugin_model! {
        name: pub MutNoCtxSingleSingle,
        input: mut Input,
        output: Output,
        bounds: [Input: From<Vec<u8>> + Into<Vec<u8>> + Default, Output: From<usize>],
        compute: |input, _ctx| {
            let mut v: Vec<u8> = take(input).into();
            v.push(1);
            let len = v.len();
            *input = Input::from(v);
            Output::from(len)
        }
    }

    plugin_test! {
        model: MutNoCtxSingleSingle,
        input: mut Vec<u8>,
        output: usize,
        cases: {
            (mut_no_ctx_single_single_case1, vec![1, 2], 3usize, vec![1, 2, 1]),
            (mut_no_ctx_single_single_case2, vec![5, 4, 3, 2], 5usize, vec![5, 4, 3, 2, 1]),
        }
    }

    //--- Variant 10: No Context, Single Input, Tuple Output (MUTABLE) ---

    plugin_model! {
        name: pub MutNoCtxSingleTuple,
        input: mut Input,
        output: (OutA, OutB),
        bounds: [Input: From<Vec<u8>> + Into<Vec<u8>> + Default, OutA: From<usize>, OutB: From<u8>],
        compute: |input, _ctx| {
            let mut v:  Vec<u8> = take(input).into();
            v.push(1);
            v.push(0);
            let len = v.len();
            let last = *v.last().unwrap();
            *input = Input::from(v);
            (OutA::from(len), OutB::from(last))
        }
    }

    plugin_test! {
        model: MutNoCtxSingleTuple,
        input: mut Vec<u8>,
        output: (usize, u8),
        cases: {
            (mut_no_ctx_single_tuple_case1, vec![1, 0], (4usize, 0), vec![1, 0, 1, 0]),
            (mut_no_ctx_single_tuple_case2, vec![5, 4, 3, 2], (6usize, 0)),
        }
    }

    //--- Variant 11: No Context, Tuple Input, Single Output (MUTABLE) ---

    plugin_model! {
        name: pub MutNoCtxTupleSingle,
        input: mut (InpA, InpB),
        output: Output,
        bounds: [InpA: From<u8> + Into<u8> + Copy, InpB: From<u8> + Into<u8> + Copy, Output: From<u8>],
        compute: |input, _ctx| {
            input.0 = InpA::from(input.0.into() + 1);
            input.1 = InpB::from(input.1.into() + 1);
            Output::from(input.0.into() + input.1.into())
        }
    }

    plugin_test! {
        model: MutNoCtxTupleSingle,
        input: mut (u8, u8),
        output: u8,
        cases: {
            (mut_no_ctx_tuple_single_case1, (11, 12), 25, (12, 13)),
            (mut_no_ctx_tuple_single_case2, (0, 0), 2, (1, 1)),

        }
    }

    //--- Variant 12: No Context, Tuple Input, Tuple Output (MUTABLE) ---

    plugin_model! {
        name: pub MutNoCtxTupleTuple,
        input: mut (A, B),
        output: (OutA, OutB),
        bounds: [A: From<u8> + Into<u8> + Copy, B: From<u8> + Into<u8> + Copy, OutA: From<u8>, OutB: From<u8>],
        compute: |input, _ctx| {
            input.0 = A::from(input.0.into() * 10);
            input.1 = B::from(input.1.into() * 0);
            (OutA::from(input.0.into()), OutB::from(input.1.into()))
        }
    }

    plugin_test! {
        model: MutNoCtxTupleTuple,
        input: mut (u8, u8),
        output: (u8, u8),
        cases: {
            (mut_no_ctx_tuple_tuple_case1, (10, 10), (100, 0), (100, 0)),
            (mut_no_ctx_tuple_tuple_case2, (20, 35), (200, 0)),
        }
    }

    plugin_test! {
        model: MutNoCtxTupleTuple,
        input: mut (u8, u8),
        cases: {
            (mut_no_ctx_tuple_tuple_case3, (1, 10), (10, 0), (10, 0)),
            (mut_no_ctx_tuple_tuple_case4, (0, 0), (0, 0)),
        }
    }

    //--- Variant 13: Context, Single Input, Single Output (MUTABLE) ---

    plugin_model! {
        name: pub MutCtxSingleSingle,
        input: mut Input,
        output: Output,
        context: BasicConfig,
        bounds: [Input: From<Vec<u8>> + Into<Vec<u8>> + Default, Output: From<usize>],
        compute: |input, ctx| {
            let mut v: Vec<u8> = take(input).into();
            v.push(ctx.value);
            let len = v.len();
            *input = Input::from(v);
            Output::from(len)
        }
    }

    plugin_test! {
        model: MutCtxSingleSingle,
        input: mut Vec<u8>,
        output: usize,
        context: BasicConfig,
        value: BasicConfig { value: 0 },
        cases: {
            (mut_ctx_single_single_case1, vec![1], 2usize, vec![1, 0]),
            (mut_ctx_single_single_case2, vec![1, 5, 3], 4usize, vec![1, 5, 3, 0]),

        }
    }

    //--- Variant 14: Context, Single Input, Tuple Output (MUTABLE) --

    plugin_model! {
        name: pub MutCtxSingleTuple,
        input: mut Input,
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [Input: From<Vec<u8>> + Into<Vec<u8>> + Default, OutA: From<usize>, OutB: From<u8>],
        compute: |input, ctx| {
            let mut v: Vec<u8> = take(input).into();
            v.push(ctx.value);
            let len = v.len();
            let last = *v.last().unwrap();
            *input = Input::from(v);
            (OutA::from(len), OutB::from(last))
        }
    }

    plugin_test! {
        model: MutCtxSingleTuple,
        input: mut Vec<u8>,
        output: (usize, u8),
        context: BasicConfig,
        value: BasicConfig{value: 4},
        cases: {
            (mut_ctx_single_tuple_case1, vec![2, 3], (3usize, 4), vec![2, 3, 4]),
            (mut_ctx_single_tuple_case2, vec![20, 16, 12, 8], (5usize, 4), vec![20, 16, 12, 8, 4]),
        }
    }

    //--- Variant 15: Context, Tuple Input, Single Output (MUTABLE) ---

    plugin_model! {
        name: pub MutCtxTupleSingle,
        input: mut (InpA, InpB),
        output: Output,
        context: BasicConfig,
        bounds: [InpA: From<u8> + Into<u8> + Copy, InpB: From<u8> + Into<u8> + Copy, Output: From<u8>],
        compute: |input, ctx| {
            input.0 = InpA::from(input.0.into() + ctx.value);
            input.1 = InpB::from(input.1.into() + ctx.value);
            Output::from(input.0.into() + input.1.into())
        }
    }

    plugin_test! {
        model: MutCtxTupleSingle,
        input: mut (u8, u8),
        output: u8,
        context: BasicConfig,
        value: BasicConfig{value: 2},
        cases: {
            (mut_ctx_tuple_single_case1, (1, 2), 7, (3, 4)),
            (mut_ctx_tuple_single_case2, (8, 8), 20, (10, 10)),
        }
    }

    //--- Variant 16: Context, Tuple Input, Tuple Output (MUTABLE) ---

    plugin_model! {
        name: pub MutCtxTupleTuple,
        input: mut (A, B),
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [A: From<u8> + Into<u8> + Copy, B: From<u8> + Into<u8> + Copy, OutA: From<u8>, OutB: From<u8>],
        compute: |input, ctx| {
            input.0 = A::from(input.0.into() + ctx.value);
            input.1 = B::from(input.1.into() + ctx.value + 1);
            (OutA::from(input.0.into()), OutB::from(input.1.into()))
        }
    }

    plugin_test! {
        model: MutCtxTupleTuple,
        input: mut (u8, u8),
        output: (u8, u8),
        context: BasicConfig,
        value: BasicConfig{value: 2},
        cases: {
            (mut_ctx_tuple_tuple_case1, (5, 6), (7, 9), (7, 9)),
            (mut_ctx_tuple_tuple_case2, (0, 0), (2, 3), (2, 3)),
        }
    }

    plugin_test! {
        model: MutCtxTupleTuple,
        input: mut (u8, u8),
        context: BasicConfig,
        value: BasicConfig{value: 2},
        cases: {
            (mut_ctx_tuple_tuple_case3, (8, 7), (10, 10), (10, 10)),
            (mut_ctx_tuple_tuple_case4, (1, 1), (3, 4), (3, 4)),
        }
    }

    // -------------------------------------------------------------------------------
    // ````````````````````````````````` PLUGIN TYPES ````````````````````````````````
    // -------------------------------------------------------------------------------

    plugin_context! {
        name: UnitContextProvider,
        context: (),
        value: ()
    }

    //--- Variant 1: Immutable concrete model arm ---

    trait ImmutablePluginTrait {
        plugin_types! {
            input: u8,
            output: u8,
            model: Model,
            context: Context,
        }
    }

    struct ImmutableHost;

    impl ImmutablePluginTrait for ImmutableHost {
        type Model = PureNoCtxSingleSingle;
        type Context = UnitContextProvider;
    }

    fn run_immutable_plugin<T: ImmutablePluginTrait>(input: u8) -> u8 {
        let model = T::Model::default();
        let ctx = <T::Context as ModelContext>::context();

        <T::Model as PurePluginModel<u8, <T::Context as ModelContext>::Context, u8>>::compute(
            &model, input, &ctx,
        )
    }

    #[test]
    fn plugin_types_immutable_model_arm_works() {
        assert_eq!(run_immutable_plugin::<ImmutableHost>(10), 11);
        assert_eq!(run_immutable_plugin::<ImmutableHost>(0), 1);
    }

    //--- Variant 2: Mutable concrete model arm ---

    trait MutablePluginTrait {
        plugin_types! {
            input: mut Vec<u8>,
            output: usize,
            model: Model,
            context: Context,
        }
    }

    struct MutableHost;

    impl MutablePluginTrait for MutableHost {
        type Model = MutNoCtxSingleSingle;
        type Context = UnitContextProvider;
    }

    fn run_mutable_plugin<T: MutablePluginTrait>(mut input: Vec<u8>) -> (Vec<u8>, usize) {
        let model = T::Model::default();
        let ctx = <T::Context as ModelContext>::context();

        let out = <T::Model as MutablePluginModel<
            Vec<u8>,
            <T::Context as ModelContext>::Context,
            usize,
        >>::compute_mut(&model, &mut input, &ctx);

        (input, out)
    }

    #[test]
    fn plugin_types_mutable_model_arm_works() {
        let (input, out) = run_mutable_plugin::<MutableHost>(vec![1, 2]);
        assert_eq!(input, vec![1, 2, 1]);
        assert_eq!(out, 3);

        let (input, out) = run_mutable_plugin::<MutableHost>(vec![5, 4, 3, 2]);
        assert_eq!(input, vec![5, 4, 3, 2, 1]);
        assert_eq!(out, 5);
    }

    //--- Variant 3: Family arm ---

    trait SimpleFamilyRoot<Input, Context, Output> {
        type Op: PurePluginModel<Input, Context, Output> + Default;
    }

    struct SimpleFamily;

    impl SimpleFamilyRoot<u8, (), u8> for SimpleFamily {
        type Op = PureNoCtxSingleSingle;
    }

    trait FamilyPluginTrait {
        plugin_types! {
            input: u8,
            output: u8,
            root: SimpleFamilyRoot,
            family: Family,
            context: Context,
        }
    }

    struct FamilyHost;

    impl FamilyPluginTrait for FamilyHost {
        type Family = SimpleFamily;
        type Context = UnitContextProvider;
    }

    fn run_family_plugin<T: FamilyPluginTrait>(input: u8) -> u8 {
        let model = <T::Family as SimpleFamilyRoot<
            u8,
            <T::Context as ModelContext>::Context,
            u8,
        >>::Op::default();

        let ctx = <T::Context as ModelContext>::context();

        <<T::Family as SimpleFamilyRoot<
            u8,
            <T::Context as ModelContext>::Context,
            u8
        >>::Op as PurePluginModel<
            u8,
            <T::Context as ModelContext>::Context,
            u8
        >>::compute(&model, input, &ctx)
    }

    #[test]
    fn plugin_types_family_arm_works() {
        assert_eq!(run_family_plugin::<FamilyHost>(10), 11);
        assert_eq!(run_family_plugin::<FamilyHost>(0), 1);
    }

    //--- Varaint 4: Family arm with `provides` ---

    trait ProvidedFamilyRoot<Input, Context, Output> {
        type Op: PurePluginModel<Input, Context, Output> + Default;
    }

    struct ProvidedFamily;

    impl ProvidedFamilyRoot<u8, BasicConfig, u8> for ProvidedFamily {
        type Op = PureCtxSingleSingle;
    }

    trait ProvidedFamilyPluginTrait {
        plugin_types! {
            input: u8,
            output: u8,
            root: ProvidedFamilyRoot,
            family: Family,
            context: Context,
            provides: [Send + Sync + 'static],
        }
    }

    struct ThreadSafeConfigProvider;

    impl ModelContext for ThreadSafeConfigProvider {
        type Context = BasicConfig;

        fn context() -> Self::Context {
            BasicConfig { value: 10 }
        }
    }

    struct ProvidedFamilyHost;

    impl ProvidedFamilyPluginTrait for ProvidedFamilyHost {
        type Family = ProvidedFamily;
        type Context = ThreadSafeConfigProvider;
    }

    fn run_family_with_provides<T: ProvidedFamilyPluginTrait>(input: u8) -> u8 {
        let model = <T::Family as ProvidedFamilyRoot<
            u8,
            <T::Context as ModelContext>::Context,
            u8,
        >>::Op::default();

        let ctx = <T::Context as ModelContext>::context();

        <<T::Family as ProvidedFamilyRoot<
            u8,
            <T::Context as ModelContext>::Context,
            u8
        >>::Op as PurePluginModel<
            u8,
            <T::Context as ModelContext>::Context,
            u8
        >>::compute(&model, input, &ctx)
    }

    #[test]
    fn plugin_types_family_arm_with_provides_works() {
        assert_eq!(run_family_with_provides::<ProvidedFamilyHost>(10), 20);
        assert_eq!(run_family_with_provides::<ProvidedFamilyHost>(1), 11);
    }

    //--- Variant 5: Family arm with `borrow`

    #[derive(Default)]
    struct BorrowIdentityModel;

    impl<'a> PurePluginModel<&'a [u8], (), &'a [u8]> for BorrowIdentityModel {
        fn compute(&self, input: &'a [u8], _context: &()) -> &'a [u8] {
            input
        }
    }

    trait BorrowFamilyRoot<Input, Context, Output> {
        type Op: PurePluginModel<Input, Context, Output> + Default;
    }

    struct BorrowFamily<'a>(PhantomData<&'a ()>);

    impl<'a> BorrowFamilyRoot<&'a [u8], (), &'a [u8]> for BorrowFamily<'a> {
        type Op = BorrowIdentityModel;
    }

    trait BorrowedFamilyPluginTrait {
        plugin_types! {
            input: &'a [u8],
            output: &'a [u8],
            borrow: ['a],
            root: BorrowFamilyRoot,
            family: Family,
            context: Context,
        }
    }

    struct BorrowedFamilyHost;

    impl BorrowedFamilyPluginTrait for BorrowedFamilyHost {
        type Family<'a> = BorrowFamily<'a>;
        type Context = UnitContextProvider;
    }

    fn run_borrowed_family_plugin<'a, T: BorrowedFamilyPluginTrait>(input: &'a [u8]) -> &'a [u8] {
        let model = <T::Family<'a> as BorrowFamilyRoot<
            &'a [u8],
            <T::Context as ModelContext>::Context,
            &'a [u8],
        >>::Op::default();

        let ctx = <T::Context as ModelContext>::context();

        <<T::Family<'a> as BorrowFamilyRoot<
            &'a [u8],
            <T::Context as ModelContext>::Context,
            &'a [u8]
        >>::Op as PurePluginModel<
            &'a [u8],
            <T::Context as ModelContext>::Context,
            &'a [u8]
        >>::compute(&model, input, &ctx)
    }

    #[test]
    fn plugin_types_family_arm_with_borrow_works() {
        let data = [1u8, 2, 3];
        assert_eq!(
            run_borrowed_family_plugin::<BorrowedFamilyHost>(&data),
            &data
        );
    }

    // -------------------------------------------------------------------------------
    // ```````````````````````````````` PLUGIN OUTPUT ````````````````````````````````
    // -------------------------------------------------------------------------------

    // Variant 1: Immutable concrete model arm

    struct OutputPureModelRunner;

    impl OutputPureModelRunner {
        plugin_output! {
            pub fn run_pure_model,
            input: u8,
            output: u8,
            model: PureCtxSingleSingle,
            context: BasicConfigProvider,
        }
    }

    #[test]
    fn plugin_output_immutable_model_arm_works() {
        assert_eq!(OutputPureModelRunner::run_pure_model(10), 20);
        assert_eq!(OutputPureModelRunner::run_pure_model(0), 10);
    }

    // Variant 2: concrete model arm

    struct OutputMutableModelRunner;

    impl OutputMutableModelRunner {
        plugin_output! {
            pub fn run_mutable_model,
            input: mut Vec<u8>,
            output: usize,
            model: MutNoCtxSingleSingle,
            context: UnitContextProvider,
        }
    }

    #[test]
    fn plugin_output_mutable_model_arm_works() {
        let mut input = vec![1, 2];
        let out = OutputMutableModelRunner::run_mutable_model(&mut input);

        assert_eq!(out, 3);
        assert_eq!(input, vec![1, 2, 1]);

        let mut input = vec![5, 4, 3, 2];
        let out = OutputMutableModelRunner::run_mutable_model(&mut input);

        assert_eq!(out, 5);
        assert_eq!(input, vec![5, 4, 3, 2, 1]);
    }

    // Variant 3: Immutable family-selected model arm

    declare_family! {
        root: pub OutputFamilyRoot,
        child: [Run]
    }

    define_family! {
        root: OutputFamilyRoot,
        family: OutputFamily,
        input: Input,
        output: Output,
        context: (),
        bounds: [
            Input: Into<u8>,
            Output: From<u8>,
        ],
        child: [
            Run => PureNoCtxSingleSingle,
        ],
    }

    struct OutputFamilyRunner;

    impl OutputFamilyRunner {
        plugin_output! {
            pub fn run_family_model,
            input: u8,
            output: u8,
            root: OutputFamilyRoot,
            family: OutputFamily,
            child: Run,
            context: UnitContextProvider,
        }
    }

    #[test]
    fn plugin_output_immutable_family_arm_works() {
        assert_eq!(OutputFamilyRunner::run_family_model(10), 11);
        assert_eq!(OutputFamilyRunner::run_family_model(0), 1);
    }

    // Variant 4: Mutable family-selected model arm

    declare_family! {
        root: mut pub OutputMutFamilyRoot,
        child: [RunMut]
    }

    define_family! {
        root: OutputMutFamilyRoot,
        family: OutputMutFamily,
        input: Input,
        output: Output,
        context: (),
        bounds: [
            Input: From<Vec<u8>> + Into<Vec<u8>> + Default,
            Output: From<usize>,
        ],
        child: [
            RunMut => MutNoCtxSingleSingle,
        ],
    }

    struct OutputMutFamilyRunner;

    impl OutputMutFamilyRunner {
        plugin_output! {
            pub fn run_mut_family_model,
            input: mut Vec<u8>,
            output: usize,
            root: OutputMutFamilyRoot,
            family: OutputMutFamily,
            child: RunMut,
            context: UnitContextProvider,
        }
    }

    #[test]
    fn plugin_output_mutable_family_arm_works() {
        let mut input = vec![1, 2];
        let out = OutputMutFamilyRunner::run_mut_family_model(&mut input);

        assert_eq!(out, 3);
        assert_eq!(input, vec![1, 2, 1]);

        let mut input = vec![9];
        let out = OutputMutFamilyRunner::run_mut_family_model(&mut input);

        assert_eq!(out, 2);
        assert_eq!(input, vec![9, 1]);
    }

    // -------------------------------------------------------------------------------
    // ``````````````````````````````` DECLARE FAMILY ````````````````````````````````
    // -------------------------------------------------------------------------------

    //--- Variant 1: Immutable Family ---

    declare_family! {
        root: pub ImmutableDeclaredRoot,
        child: [DeclaredRun, DeclaredEcho]
    }

    struct ImmutableDeclaredFamily;

    impl ImmutableDeclaredRoot<u8, (), u8> for ImmutableDeclaredFamily {
        type DeclaredRun = PureNoCtxSingleSingle;
        type DeclaredEcho = PureNoCtxSingleSingle;
    }

    fn run_declared_immutable_family(input: u8) -> u8 {
        let model =
            <ImmutableDeclaredFamily as ImmutableDeclaredRoot<u8, (), u8>>::DeclaredRun::default();
        let context = ();

        <<ImmutableDeclaredFamily as ImmutableDeclaredRoot<u8, (), u8>>::DeclaredRun
            as PurePluginModel<u8, (), u8>>::compute(&model, input, &context)
    }

    #[test]
    fn declare_family_immutable_arm_creates_root_and_children() {
        assert_eq!(run_declared_immutable_family(10), 11);
        assert_eq!(run_declared_immutable_family(0), 1);
    }

    #[test]
    fn declare_family_immutable_arm_child_markers_exist() {
        let _run = DeclaredRun;
        let _echo = DeclaredEcho;
    }

    //--- Variant 2: Mutable Family ---

    declare_family! {
        root: mut pub MutableDeclaredRoot,
        child: [DeclaredRunMut, DeclaredNormalize]
    }

    struct MutableDeclaredFamily;

    impl MutableDeclaredRoot<Vec<u8>, (), usize> for MutableDeclaredFamily {
        type DeclaredRunMut = MutNoCtxSingleSingle;
        type DeclaredNormalize = MutNoCtxSingleSingle;
    }

    fn run_declared_mutable_family(mut input: Vec<u8>) -> (Vec<u8>, usize) {
        let model =
            <MutableDeclaredFamily as MutableDeclaredRoot<Vec<u8>, (), usize>>::DeclaredRunMut::default();
        let context = ();

        let out =
            <<MutableDeclaredFamily as MutableDeclaredRoot<Vec<u8>, (), usize>>::DeclaredRunMut
                as MutablePluginModel<Vec<u8>, (), usize>>::compute_mut(
                    &model,
                    &mut input,
                    &context,
                );

        (input, out)
    }

    #[test]
    fn declare_family_mutable_arm_creates_root_and_children() {
        let (input, out) = run_declared_mutable_family(vec![1, 2]);
        assert_eq!(input, vec![1, 2, 1]);
        assert_eq!(out, 3);
    }

    #[test]
    fn declare_family_mutable_arm_child_markers_exist() {
        let _run = DeclaredRunMut;
        let _normalize = DeclaredNormalize;
    }

    // -------------------------------------------------------------------------------
    // ```````````````````````````````` DEFINE FAMILY ````````````````````````````````
    // -------------------------------------------------------------------------------

    //--- With Context, Single Input, Single Output ---

    declare_family! {
        root: pub DefFamCtxSingleSingleRoot,
        child: [DefFamCtxSingleSingleChild]
    }

    define_family! {
        root: DefFamCtxSingleSingleRoot,
        family: DefFamCtxSingleSingleFamily,
        input: Input,
        output: Output,
        context: BasicConfig,
        bounds: [Input: Into<u8>, Output: From<u8>],
        child: [
            DefFamCtxSingleSingleChild => PureCtxSingleSingle,
        ],
    }

    fn run_define_family_ctx_single_single(input: u8) -> u8 {
        let model = <DefFamCtxSingleSingleFamily as DefFamCtxSingleSingleRoot<
            u8,
            BasicConfig,
            u8,
        >>::DefFamCtxSingleSingleChild::default();

        let ctx = BasicConfig { value: 10 };

        <<DefFamCtxSingleSingleFamily as DefFamCtxSingleSingleRoot<u8, BasicConfig, u8>>
            ::DefFamCtxSingleSingleChild as PurePluginModel<u8, BasicConfig, u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_single_input_single_output_works() {
        assert_eq!(run_define_family_ctx_single_single(10), 20);
        assert_eq!(run_define_family_ctx_single_single(1), 11);
    }

    //--- With Context, Single Input, Tuple Output ---

    declare_family! {
        root: pub DefFamCtxSingleTupleRoot,
        child: [DefFamCtxSingleTupleChild]
    }

    define_family! {
        root: DefFamCtxSingleTupleRoot,
        family: DefFamCtxSingleTupleFamily,
        input: Input,
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [Input: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        child: [
            DefFamCtxSingleTupleChild => PureCtxSingleTuple,
        ],
    }

    fn run_define_family_ctx_single_tuple(input: u8) -> (u8, u8) {
        let model = <DefFamCtxSingleTupleFamily as DefFamCtxSingleTupleRoot<
            u8,
            BasicConfig,
            (u8, u8),
        >>::DefFamCtxSingleTupleChild::default();

        let ctx = BasicConfig { value: 1 };

        <<DefFamCtxSingleTupleFamily as DefFamCtxSingleTupleRoot<u8, BasicConfig, (u8, u8)>>
            ::DefFamCtxSingleTupleChild as PurePluginModel<u8, BasicConfig, (u8, u8)>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_single_input_tuple_output_works() {
        assert_eq!(run_define_family_ctx_single_tuple(5), (5, 6));
        assert_eq!(run_define_family_ctx_single_tuple(1), (1, 2));
    }

    // With Context, Tuple Input, Single Output

    declare_family! {
        root: pub DefFamCtxTupleSingleRoot,
        child: [DefFamCtxTupleSingleChild]
    }

    define_family! {
        root: DefFamCtxTupleSingleRoot,
        family: DefFamCtxTupleSingleFamily,
        input: (InpA, InpB),
        output: Output,
        context: BasicConfig,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, Output: From<u8>],
        child: [
            DefFamCtxTupleSingleChild => PureCtxTupleSingle,
        ],
    }

    fn run_define_family_ctx_tuple_single(input: (u8, u8)) -> u8 {
        let model = <DefFamCtxTupleSingleFamily as DefFamCtxTupleSingleRoot<
            (u8, u8),
            BasicConfig,
            u8,
        >>::DefFamCtxTupleSingleChild::default();

        let ctx = BasicConfig { value: 10 };

        <<DefFamCtxTupleSingleFamily as DefFamCtxTupleSingleRoot<(u8, u8), BasicConfig, u8>>
            ::DefFamCtxTupleSingleChild as PurePluginModel<(u8, u8), BasicConfig, u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_tuple_input_single_output_works() {
        assert_eq!(run_define_family_ctx_tuple_single((10, 10)), 30);
        assert_eq!(run_define_family_ctx_tuple_single((5, 30)), 45);
    }

    //--- With Context, Tuple Input, Tuple Output ---

    declare_family! {
        root: pub DefFamCtxTupleTupleRoot,
        child: [DefFamCtxTupleTupleChild]
    }

    define_family! {
        root: DefFamCtxTupleTupleRoot,
        family: DefFamCtxTupleTupleFamily,
        input: (InpA, InpB),
        output: (OutA, OutB),
        context: BasicConfig,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        child: [
            DefFamCtxTupleTupleChild => PureCtxTupleTuple,
        ],
    }

    fn run_define_family_ctx_tuple_tuple(input: (u8, u8)) -> (u8, u8) {
        let model = <DefFamCtxTupleTupleFamily as DefFamCtxTupleTupleRoot<
            (u8, u8),
            BasicConfig,
            (u8, u8),
        >>::DefFamCtxTupleTupleChild::default();

        let ctx = BasicConfig { value: 5 };

        <<DefFamCtxTupleTupleFamily as DefFamCtxTupleTupleRoot<(u8, u8), BasicConfig, (u8, u8)>>
            ::DefFamCtxTupleTupleChild as PurePluginModel<(u8, u8), BasicConfig, (u8, u8)>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_tuple_input_tuple_output_works() {
        assert_eq!(run_define_family_ctx_tuple_tuple((5, 10)), (10, 15));
        assert_eq!(run_define_family_ctx_tuple_tuple((1, 0)), (6, 5));
    }

    //--- No Context, Single Input, Single Output ---

    declare_family! {
        root: pub DefFamNoCtxSingleSingleRoot,
        child: [DefFamNoCtxSingleSingleChild]
    }

    define_family! {
        root: DefFamNoCtxSingleSingleRoot,
        family: DefFamNoCtxSingleSingleFamily,
        input: Input,
        output: Output,
        bounds: [Input: Into<u8>, Output: From<u8>],
        child: [
            DefFamNoCtxSingleSingleChild => PureNoCtxSingleSingle,
        ],
    }

    fn run_define_family_no_ctx_single_single(input: u8) -> u8 {
        let model =
            <DefFamNoCtxSingleSingleFamily as DefFamNoCtxSingleSingleRoot<u8, (), u8>>
                ::DefFamNoCtxSingleSingleChild::default();

        let ctx = ();

        <<DefFamNoCtxSingleSingleFamily as DefFamNoCtxSingleSingleRoot<u8, (), u8>>
            ::DefFamNoCtxSingleSingleChild as PurePluginModel<u8, (), u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_no_context_single_input_single_output_works() {
        assert_eq!(run_define_family_no_ctx_single_single(10), 11);
        assert_eq!(run_define_family_no_ctx_single_single(0), 1);
    }

    //--- No Context, Single Input, Tuple Output ---

    declare_family! {
        root: pub DefFamNoCtxSingleTupleRoot,
        child: [DefFamNoCtxSingleTupleChild]
    }

    define_family! {
        root: DefFamNoCtxSingleTupleRoot,
        family: DefFamNoCtxSingleTupleFamily,
        input: Input,
        output: (OutA, OutB),
        bounds: [Input: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        child: [
            DefFamNoCtxSingleTupleChild => PureNoCtxSingleTuple,
        ],
    }

    fn run_define_family_no_ctx_single_tuple(input: u8) -> (u8, u8) {
        let model = <DefFamNoCtxSingleTupleFamily as DefFamNoCtxSingleTupleRoot<
            u8,
            (),
            (u8, u8),
        >>::DefFamNoCtxSingleTupleChild::default();

        let ctx = ();

        <<DefFamNoCtxSingleTupleFamily as DefFamNoCtxSingleTupleRoot<u8, (), (u8, u8)>>
            ::DefFamNoCtxSingleTupleChild as PurePluginModel<u8, (), (u8, u8)>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_no_context_single_input_tuple_output_works() {
        assert_eq!(run_define_family_no_ctx_single_tuple(10), (10, 11));
        assert_eq!(run_define_family_no_ctx_single_tuple(0), (0, 1));
    }

    //--- No Context, Tuple Input, Single Output ---

    declare_family! {
        root: pub DefFamNoCtxTupleSingleRoot,
        child: [DefFamNoCtxTupleSingleChild]
    }

    define_family! {
        root: DefFamNoCtxTupleSingleRoot,
        family: DefFamNoCtxTupleSingleFamily,
        input: (InpA, InpB),
        output: Output,
        bounds: [InpA: Into<u8>, InpB: Into<u8>, Output: From<u8>],
        child: [
            DefFamNoCtxTupleSingleChild => PureNoCtxTupleSingle,
        ],
    }

    fn run_define_family_no_ctx_tuple_single(input: (u8, u8)) -> u8 {
        let model = <DefFamNoCtxTupleSingleFamily as DefFamNoCtxTupleSingleRoot<
            (u8, u8),
            (),
            u8,
        >>::DefFamNoCtxTupleSingleChild::default();

        let ctx = ();

        <<DefFamNoCtxTupleSingleFamily as DefFamNoCtxTupleSingleRoot<(u8, u8), (), u8>>
            ::DefFamNoCtxTupleSingleChild as PurePluginModel<(u8, u8), (), u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_no_context_tuple_input_single_output_works() {
        assert_eq!(run_define_family_no_ctx_tuple_single((10, 11)), 21);
        assert_eq!(run_define_family_no_ctx_tuple_single((0, 5)), 5);
    }

    //--- No Context, Tuple Input, Tuple Output ---

    declare_family! {
        root: pub DefFamNoCtxTupleTupleRoot,
        child: [DefFamNoCtxTupleTupleChild]
    }

    define_family! {
        root: DefFamNoCtxTupleTupleRoot,
        family: DefFamNoCtxTupleTupleFamily,
        input: (InpA, InpB),
        output: (OutA, OutB),
        bounds: [InpA: Into<u8>, InpB: Into<u8>, OutA: From<u8>, OutB: From<u8>],
        child: [
            DefFamNoCtxTupleTupleChild => PureNoCtxTupleTuple,
        ],
    }

    fn run_define_family_no_ctx_tuple_tuple(input: (u8, u8)) -> (u8, u8) {
        let model = <DefFamNoCtxTupleTupleFamily as DefFamNoCtxTupleTupleRoot<
            (u8, u8),
            (),
            (u8, u8),
        >>::DefFamNoCtxTupleTupleChild::default();

        let ctx = ();

        <<DefFamNoCtxTupleTupleFamily as DefFamNoCtxTupleTupleRoot<(u8, u8), (), (u8, u8)>>
            ::DefFamNoCtxTupleTupleChild as PurePluginModel<(u8, u8), (), (u8, u8)>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_no_context_tuple_input_tuple_output_works() {
        assert_eq!(run_define_family_no_ctx_tuple_tuple((5, 10)), (50, 100));
        assert_eq!(run_define_family_no_ctx_tuple_tuple((1, 0)), (10, 0));
    }

    //--- With Context + marker ---

    plugin_model! {
        name: pub PureGenericCtxSingleSingle,
        input: Input,
        output: Output,
        others: [T],
        context: GenericConfig<T>,
        bounds: [Input: Into<u8>, Output: From<u8>, T: Into<u8> + Clone],
        compute: |input, ctx| {
            Output::from(input.into() + ctx.value.clone().into())
        },
    }

    declare_family! {
        root: pub DefFamCtxMarkerRoot,
        child: [DefFamCtxMarkerChild]
    }

    define_family! {
        root: DefFamCtxMarkerRoot,
        family: DefFamCtxMarkerFamily,
        input: Input,
        output: Output,
        context: GenericConfig<T>,
        marker: [T],
        bounds: [Input: Into<u8>, Output: From<u8>, T: Into<u8> + Clone],
        child: [
            DefFamCtxMarkerChild => PureGenericCtxSingleSingle,
        ],
    }

    fn run_define_family_ctx_marker(input: u8) -> u8 {
        let model =
            <DefFamCtxMarkerFamily as DefFamCtxMarkerRoot<u8, GenericConfig<u8>, u8>>
                ::DefFamCtxMarkerChild::default();

        let ctx = GenericConfig { value: 7u8 };

        <<DefFamCtxMarkerFamily as DefFamCtxMarkerRoot<u8, GenericConfig<u8>, u8>>
            ::DefFamCtxMarkerChild as PurePluginModel<u8, GenericConfig<u8>, u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_marker_works() {
        assert_eq!(run_define_family_ctx_marker(10), 17);
        assert_eq!(run_define_family_ctx_marker(0), 7);
    }

    //--- With Context + borrow + marker ---

    plugin_model! {
        name: pub PureGenericBorrowCtx,
        input: Input,
        output: Output,
        others: [T],
        context: GenericConfig<T>,
        bounds: [Input: AsRef<[u8]>, Output: From<usize>, T: Clone],
        compute: |input, _ctx| {
            Output::from(input.as_ref().len())
        },
    }

    declare_family! {
        root: pub DefFamCtxBorrowMarkerRoot,
        child: [DefFamCtxBorrowMarkerChild]
    }

    define_family! {
        root: DefFamCtxBorrowMarkerRoot,
        family: DefFamCtxBorrowMarkerFamily,
        borrow: ['a],
        input: Input,
        output: Output,
        context: GenericConfig<T>,
        marker: [T],
        bounds: [Input: AsRef<[u8]> + 'a, Output: From<usize>, T: Clone],
        child: [
            DefFamCtxBorrowMarkerChild => PureGenericBorrowCtx,
        ],
    }

    fn run_define_family_ctx_borrow_marker<'a>(input: &'a [u8]) -> usize {
        let model = <DefFamCtxBorrowMarkerFamily<'a> as DefFamCtxBorrowMarkerRoot<
            &'a [u8],
            GenericConfig<u8>,
            usize,
        >>::DefFamCtxBorrowMarkerChild::default();

        let ctx = GenericConfig { value: 99u8 };

        <<DefFamCtxBorrowMarkerFamily<'a> as DefFamCtxBorrowMarkerRoot<&'a [u8], GenericConfig<u8>, usize>>
            ::DefFamCtxBorrowMarkerChild as PurePluginModel<&'a [u8], GenericConfig<u8>, usize>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_with_context_borrow_and_marker_works() {
        let data = [1u8, 2, 3, 4];
        assert_eq!(run_define_family_ctx_borrow_marker(&data), 4);

        let data = [9u8];
        assert_eq!(run_define_family_ctx_borrow_marker(&data), 1);
    }

    //--- No Context + multiple children ---

    declare_family! {
        root: pub DefFamNoCtxMultiChildRoot,
        child: [DefFamNoCtxMultiChildA, DefFamNoCtxMultiChildB]
    }

    define_family! {
        root: DefFamNoCtxMultiChildRoot,
        family: DefFamNoCtxMultiChildFamily,
        input: Input,
        output: Output,
        bounds: [Input: Into<u8>, Output: From<u8>],
        child: [
            DefFamNoCtxMultiChildA => PureNoCtxSingleSingle,
            DefFamNoCtxMultiChildB => PureNoCtxSingleSingle,
        ],
    }

    fn run_define_family_no_ctx_multi_child_a(input: u8) -> u8 {
        let model =
            <DefFamNoCtxMultiChildFamily as DefFamNoCtxMultiChildRoot<u8, (), u8>>
                ::DefFamNoCtxMultiChildA::default();

        let ctx = ();

        <<DefFamNoCtxMultiChildFamily as DefFamNoCtxMultiChildRoot<u8, (), u8>>
            ::DefFamNoCtxMultiChildA as PurePluginModel<u8, (), u8>>
            ::compute(&model, input, &ctx)
    }

    fn run_define_family_no_ctx_multi_child_b(input: u8) -> u8 {
        let model =
            <DefFamNoCtxMultiChildFamily as DefFamNoCtxMultiChildRoot<u8, (), u8>>
                ::DefFamNoCtxMultiChildB::default();

        let ctx = ();

        <<DefFamNoCtxMultiChildFamily as DefFamNoCtxMultiChildRoot<u8, (), u8>>
            ::DefFamNoCtxMultiChildB as PurePluginModel<u8, (), u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_multiple_children_work() {
        assert_eq!(run_define_family_no_ctx_multi_child_a(10), 11);
        assert_eq!(run_define_family_no_ctx_multi_child_b(0), 1);

        let _a = DefFamNoCtxMultiChildA;
        let _b = DefFamNoCtxMultiChildB;
    }

    //--- No Context + multiple children with distinct models ---

    plugin_model! {
        name: pub PureNoCtxSingleSingleDouble,
        input: Input,
        output: Output,
        bounds: [Input: Into<u8>, Output: From<u8>],
        compute: |input, _ctx| {
            let x = input.into();
            Output::from(x * 2)
        }
    }

    declare_family! {
        root: pub DefFamNoCtxMultiChildDistinctRoot,
        child: [DefFamNoCtxMultiChildInc, DefFamNoCtxMultiChildDouble]
    }

    define_family! {
        root: DefFamNoCtxMultiChildDistinctRoot,
        family: DefFamNoCtxMultiChildDistinctFamily,
        input: Input,
        output: Output,
        bounds: [Input: Into<u8>, Output: From<u8>],
        child: [
            DefFamNoCtxMultiChildInc => PureNoCtxSingleSingle,
            DefFamNoCtxMultiChildDouble => PureNoCtxSingleSingleDouble,
        ],
    }

    fn run_define_family_no_ctx_multi_child_inc(input: u8) -> u8 {
        let model = <DefFamNoCtxMultiChildDistinctFamily as DefFamNoCtxMultiChildDistinctRoot<
            u8,
            (),
            u8,
        >>::DefFamNoCtxMultiChildInc::default();

        let ctx = ();

        <<DefFamNoCtxMultiChildDistinctFamily as DefFamNoCtxMultiChildDistinctRoot<u8, (), u8>>
            ::DefFamNoCtxMultiChildInc as PurePluginModel<u8, (), u8>>
            ::compute(&model, input, &ctx)
    }

    fn run_define_family_no_ctx_multi_child_double(input: u8) -> u8 {
        let model = <DefFamNoCtxMultiChildDistinctFamily as DefFamNoCtxMultiChildDistinctRoot<
            u8,
            (),
            u8,
        >>::DefFamNoCtxMultiChildDouble::default();

        let ctx = ();

        <<DefFamNoCtxMultiChildDistinctFamily as DefFamNoCtxMultiChildDistinctRoot<u8, (), u8>>
            ::DefFamNoCtxMultiChildDouble as PurePluginModel<u8, (), u8>>
            ::compute(&model, input, &ctx)
    }

    #[test]
    fn define_family_multiple_children_with_distinct_models_work() {
        assert_eq!(run_define_family_no_ctx_multi_child_inc(10), 11);
        assert_eq!(run_define_family_no_ctx_multi_child_inc(0), 1);

        assert_eq!(run_define_family_no_ctx_multi_child_double(10), 20);
        assert_eq!(run_define_family_no_ctx_multi_child_double(3), 6);

        let _inc = DefFamNoCtxMultiChildInc;
        let _double = DefFamNoCtxMultiChildDouble;
    }
        
    // -------------------------------------------------------------------------------
    // ```````````````````````````````` HELPER MACROS ````````````````````````````````
    // -------------------------------------------------------------------------------

    #[test]
    fn phantom_struct_no_lifetime_no_generic_compiles() {
        __phantom_struct!(pub Plain [] []);
        let _x = Plain;
    }

    #[test]
    fn phantom_struct_lifetime_only_compiles() {
        __phantom_struct!(pub WithLt ['a] []);
        let _x: WithLt<'static>;
    }

    #[test]
    fn phantom_struct_generic_only_compiles() {
        __phantom_struct!(pub WithGen [] [T]);
        let _x: WithGen<u8>;
    }

    #[test]
    fn phantom_struct_lifetime_and_generic_compiles() {
        __phantom_struct!(pub WithLtGen ['a] [T]);
        let _x: WithLtGen<'static, u8>;
    }

    #[test]
    fn phantom_struct_generic_is_covariant() {
        __phantom_struct!(pub CovGen [] [T]);
        // covariance: Foo<&'static str> can be used where Foo<&'short str> is expected
        fn accepts<'a>(_: CovGen<&'a str>) {}
        let x: CovGen<&'static str> = CovGen(PhantomData);
        accepts(x);
    }

    #[test]
    fn phantom_struct_lifetime_and_generic_is_covariant() {
        __phantom_struct!(pub CovLtGen ['a] [T]);
        fn accepts<'a>(_: CovLtGen<'a, &'a str>) {}
        let x: CovLtGen<'static, &'static str> = CovLtGen(PhantomData);
        accepts(x);
    }

    #[test]
    fn phantom_struct_field_types_are_correct() {
        // Arm 2: PhantomData<(&'a (),)>
        __phantom_struct!(pub LtField ['a] []);
        let _: LtField<'static> = LtField(PhantomData::<(&'static (),)>);

        // Arm 3: PhantomData<(T,)>
        __phantom_struct!(pub GenField [] [T]);
        let _: GenField<u8> = GenField(PhantomData::<(u8,)>);

        // Arm 4: PhantomData<(T, &'a ())> — generics first, then lifetime references
        __phantom_struct!(pub LtGenField ['a] [T]);
        let _: LtGenField<'static, u8> = LtGenField(PhantomData::<(u8, &'static ())>);
    }
}