specta-serde 0.0.13

Serde support for Specta
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
use std::collections::{HashMap, HashSet};

use specta::{
    Types,
    datatype::{
        DataType, Enum, Field, Fields, Generic, NamedFields, NamedReference, NamedReferenceType,
        Reference, Variant,
    },
};

use crate::{
    Error, Phase, PhaseRewrite, container_rename_all_rule, enum_variant_field_rename_rule,
    inflection::RenameRule,
    parser::{SerdeContainerAttrs, SerdeFieldAttrs, SerdeVariantAttrs},
    phase_field_key,
    phased::PhasedTy,
    repr::EnumRepr,
    serialized_variant_name,
};

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ApplyMode {
    Unified,
    Phases,
}

pub fn validate_for_mode(types: &Types, mode: ApplyMode) -> Result<(), Error> {
    for ndt in types.into_unsorted_iter() {
        let Some(ty) = &ndt.ty else {
            continue;
        };

        inner(
            ty,
            types,
            &mut HashSet::new(),
            ndt.name.to_string(),
            mode,
            true,
            None,
            FlattenDirections::LIVE,
        )?;
    }

    // Generic definitions are necessarily conditional: an internally tagged
    // `E<T> { V(T) }` is valid for map-like `T` and invalid for scalar `T`.
    // Walk registered roots as use sites so their concrete generic arguments
    // are available through `GenericEnv` when following the named reference.
    for root in types.roots() {
        inner(
            root,
            types,
            &mut HashSet::new(),
            "<root>".to_string(),
            mode,
            true,
            None,
            FlattenDirections::LIVE,
        )?;
    }

    Ok(())
}

pub(crate) fn validate_datatype_for_mode(
    dt: &DataType,
    types: &Types,
    mode: ApplyMode,
) -> Result<(), Error> {
    inner(
        dt,
        types,
        &mut HashSet::new(),
        "<top-level>".to_string(),
        mode,
        true,
        None,
        FlattenDirections::LIVE,
    )
}

pub(crate) fn validate_datatype_for_mode_shallow(
    dt: &DataType,
    types: &Types,
    mode: ApplyMode,
    phase: Phase,
) -> Result<(), Error> {
    inner(
        dt,
        types,
        &mut HashSet::new(),
        "<top-level>".to_string(),
        mode,
        false,
        None,
        FlattenDirections::for_phase(phase),
    )
}

fn inner(
    dt: &DataType,
    types: &Types,
    checked_references: &mut HashSet<(Reference, FlattenDirections)>,
    path: String,
    mode: ApplyMode,
    follow_named_references: bool,
    env: Option<&GenericEnv<'_>>,
    root_directions: FlattenDirections,
) -> Result<(), Error> {
    match dt {
        DataType::Nullable(ty) => inner(
            ty,
            types,
            checked_references,
            path,
            mode,
            follow_named_references,
            env,
            root_directions,
        )?,
        DataType::Map(map) => {
            inner(
                map.key_ty(),
                types,
                checked_references,
                format!("{path}.<map_key>"),
                mode,
                follow_named_references,
                env,
                root_directions,
            )?;
            inner(
                map.value_ty(),
                types,
                checked_references,
                format!("{path}.<map_value>"),
                mode,
                follow_named_references,
                env,
                root_directions,
            )?;
        }
        DataType::List(list) => {
            inner(
                &list.ty,
                types,
                checked_references,
                format!("{path}.<list_item>"),
                mode,
                follow_named_references,
                env,
                root_directions,
            )?;
        }
        DataType::Struct(strct) => {
            let container_attrs = SerdeContainerAttrs::from_attributes(&strct.attributes)?;
            let declared_deserialize_live = root_directions.deserialize
                && !container_attrs.as_ref().is_some_and(|attrs| {
                    attrs.resolved_from.is_some() || attrs.resolved_try_from.is_some()
                });
            validate_container_attributes(
                &strct.attributes,
                types,
                checked_references,
                &path,
                mode,
                env,
                root_directions,
            )?;
            if let Some(attrs) = &container_attrs {
                if attrs.variant_identifier || attrs.field_identifier {
                    return Err(Error::invalid_phased_type_usage(
                        path,
                        "`#[serde(variant_identifier)]` and `#[serde(field_identifier)]` are only valid on enums",
                    ));
                }

                if attrs.untagged {
                    return Err(Error::invalid_phased_type_usage(
                        path,
                        "`#[serde(untagged)]` is only valid on enums",
                    ));
                }

                if attrs.content.is_some() {
                    return Err(Error::invalid_phased_type_usage(
                        path,
                        "`#[serde(content = ...)]` is only valid on enums",
                    ));
                }

                if attrs.tag.is_some() && !matches!(&strct.fields, Fields::Named(_)) {
                    return Err(Error::invalid_phased_type_usage(
                        path,
                        "`#[serde(tag = ...)]` on structs requires named fields",
                    ));
                }
            }

            // serde_derive silently ignores every `skip` spelling on a
            // newtype struct's only field. The Type derive preserves that
            // field and marks it so exporters can retain the bare wire shape.
            if let Fields::Unnamed(unnamed) = &strct.fields
                && let [field] = unnamed.fields.as_slice()
                && !field
                    .attributes
                    .contains_key(crate::SERDE_NEWTYPE_SKIP_IGNORED)
                && SerdeFieldAttrs::from_attributes(&field.attributes)?
                    .is_some_and(|attrs| attrs.skip_serializing || attrs.skip_deserializing)
            {
                return Err(Error::invalid_usage_of_skip(
                    path,
                    "serde ignores `skip` on a newtype struct's only field (the wire format \
                     stays the bare inner value), so the export cannot represent it; remove \
                     the attribute -- it has no effect on the serde wire format",
                ));
            }

            match &strct.fields {
                Fields::Unit => {}
                Fields::Unnamed(unnamed) => {
                    if root_directions.serialize
                        && container_attrs
                            .as_ref()
                            .is_none_or(|attrs| attrs.resolved_into.is_none())
                    {
                        validate_unnamed_conditional_omission(unnamed, &path)?;
                    }
                    for (idx, (field, _)) in unnamed
                        .fields
                        .iter()
                        .enumerate()
                        .filter_map(|(idx, field)| field.ty.as_ref().map(|ty| (idx, (field, ty))))
                    {
                        validate_field_attributes(field, format!("{path}[{idx}]"), mode)?;
                    }
                    for (idx, (field, ty)) in unnamed
                        .fields
                        .iter()
                        .enumerate()
                        .filter_map(|(idx, field)| field.ty.as_ref().map(|ty| (idx, (field, ty))))
                    {
                        inner(
                            ty,
                            types,
                            checked_references,
                            format!("{path}[{idx}]"),
                            mode,
                            follow_named_references,
                            env,
                            root_directions.and(FlattenDirections::for_field(field)?),
                        )?;
                    }
                }
                Fields::Named(named) => {
                    validate_named_field_keys(
                        named,
                        struct_field_key_rules(&strct.attributes, &path)?,
                        &path,
                        declared_deserialize_live,
                        mode,
                    )?;
                    for (name, (field, ty)) in named
                        .fields
                        .iter()
                        .filter_map(|(name, field)| field.ty.as_ref().map(|ty| (name, (field, ty))))
                    {
                        validate_field_attributes(field, format!("{path}.{name}"), mode)?;
                        validate_flatten_field(
                            field,
                            ty,
                            types,
                            &format!("{path}.{name}"),
                            mode,
                            root_directions,
                            env,
                        )?;
                    }
                    for (name, (field, ty)) in named
                        .fields
                        .iter()
                        .filter_map(|(name, field)| field.ty.as_ref().map(|ty| (name, (field, ty))))
                    {
                        inner(
                            ty,
                            types,
                            checked_references,
                            format!("{path}.{name}"),
                            mode,
                            follow_named_references,
                            env,
                            root_directions.and(FlattenDirections::for_field(field)?),
                        )?;
                    }
                }
            }
        }
        DataType::Enum(enm) => {
            let container_attrs = SerdeContainerAttrs::from_attributes(&enm.attributes)?;
            let declared_directions = FlattenDirections {
                serialize: root_directions.serialize
                    && container_attrs
                        .as_ref()
                        .is_none_or(|attrs| attrs.resolved_into.is_none()),
                deserialize: root_directions.deserialize
                    && !container_attrs.as_ref().is_some_and(|attrs| {
                        attrs.resolved_from.is_some() || attrs.resolved_try_from.is_some()
                    }),
            };
            // Untagged enums never emit variant names, so attrs that only
            // rename variant labels are not part of the wire shape for them.
            let variant_names_emitted =
                !matches!(EnumRepr::from_attrs(&enm.attributes)?, EnumRepr::Untagged);
            validate_container_attributes(
                &enm.attributes,
                types,
                checked_references,
                &path,
                mode,
                env,
                root_directions,
            )?;
            if container_attrs.as_ref().is_some_and(|attrs| attrs.default) {
                return Err(Error::invalid_phased_type_usage(
                    path,
                    "`#[serde(default)]` is only valid on structs",
                ));
            }
            validate_identifier_enum(enm, &path, mode)?;
            validate_enum(enm, types, path.clone(), mode, declared_directions, env)?;
            if variant_names_emitted {
                validate_variant_aliases(
                    enm,
                    &container_attrs,
                    &path,
                    declared_directions.deserialize,
                    mode,
                )?;
            }

            for (variant_name, variant) in &enm.variants {
                let variant_is_rendered = variant_is_rendered(variant)?;
                validate_variant_attributes(variant, format!("{path}::{variant_name}"), mode)?;
                let variant_directions =
                    FlattenDirections::for_variant(variant)?.and(root_directions);
                if variant_names_emitted && variant_is_rendered {
                    validate_variant_key(variant_name, variant, &container_attrs, &path, mode)?;
                }
                match &variant.fields {
                    Fields::Unit => {}
                    Fields::Named(named) => {
                        if variant_is_rendered {
                            validate_named_field_keys(
                                named,
                                variant_field_key_rules(&container_attrs, variant, variant_name)?,
                                &format!("{path}::{variant_name}"),
                                declared_directions.deserialize && variant_directions.deserialize,
                                mode,
                            )?;
                        }
                        for (name, (field, ty)) in
                            named.fields.iter().filter_map(|(name, field)| {
                                field.ty.as_ref().map(|ty| (name, (field, ty)))
                            })
                        {
                            let path = format!("{path}::{variant_name}.{name}");
                            validate_field_attributes(field, path.clone(), mode)?;
                            validate_flatten_field(
                                field,
                                ty,
                                types,
                                &path,
                                mode,
                                variant_directions,
                                env,
                            )?;
                        }
                        for (name, (field, ty)) in
                            named.fields.iter().filter_map(|(name, field)| {
                                field.ty.as_ref().map(|ty| (name, (field, ty)))
                            })
                        {
                            inner(
                                ty,
                                types,
                                checked_references,
                                format!("{path}::{variant_name}.{name}"),
                                mode,
                                follow_named_references,
                                env,
                                variant_directions.and(FlattenDirections::for_field(field)?),
                            )?;
                        }
                    }
                    Fields::Unnamed(unnamed) => {
                        if declared_directions.serialize && variant_is_serialized(variant)? {
                            validate_unnamed_conditional_omission(
                                unnamed,
                                &format!("{path}::{variant_name}"),
                            )?;
                        }
                        for (idx, (field, _)) in
                            unnamed
                                .fields
                                .iter()
                                .enumerate()
                                .filter_map(|(idx, field)| {
                                    field.ty.as_ref().map(|ty| (idx, (field, ty)))
                                })
                        {
                            validate_field_attributes(
                                field,
                                format!("{path}::{variant_name}[{idx}]"),
                                mode,
                            )?;
                        }
                        for (idx, (field, ty)) in
                            unnamed
                                .fields
                                .iter()
                                .enumerate()
                                .filter_map(|(idx, field)| {
                                    field.ty.as_ref().map(|ty| (idx, (field, ty)))
                                })
                        {
                            inner(
                                ty,
                                types,
                                checked_references,
                                format!("{path}::{variant_name}[{idx}]"),
                                mode,
                                follow_named_references,
                                env,
                                variant_directions.and(FlattenDirections::for_field(field)?),
                            )?;
                        }
                    }
                }
            }
        }
        DataType::Tuple(tuple) => {
            for (idx, ty) in tuple.elements.iter().enumerate() {
                inner(
                    ty,
                    types,
                    checked_references,
                    format!("{path}[{idx}]"),
                    mode,
                    follow_named_references,
                    env,
                    root_directions,
                )?;
            }
        }
        DataType::Intersection(types_) => {
            for (idx, ty) in types_.iter().enumerate() {
                inner(
                    ty,
                    types,
                    checked_references,
                    format!("{path}.<intersection_{idx}>"),
                    mode,
                    follow_named_references,
                    env,
                    root_directions,
                )?;
            }
        }
        DataType::Reference(reference) => match reference {
            Reference::Named(reference) => {
                if let NamedReferenceType::Reference {
                    generics: reference_generics,
                    ..
                } = &reference.inner
                {
                    for (_, dt) in reference_generics {
                        inner(
                            dt,
                            types,
                            checked_references,
                            format!("{path}.<generic>"),
                            mode,
                            follow_named_references,
                            env,
                            root_directions,
                        )?;
                    }
                }

                // The memo is keyed on the reference with its generic
                // arguments *resolved through the current substitution*, so
                // the same syntactic reference reached under two different
                // outer instantiations (say `Outer<Inner>` and
                // `Outer<Vec<u8>>` both naming `FlattenGeneric<T>` in their
                // body) is re-validated for each. Identical
                // (reference, resolved arguments) pairs still dedupe, which
                // keeps cyclic graphs terminating; if resolving a key would
                // grow past `RESOLVED_KEY_NODE_BUDGET` (only possible for
                // hand-built non-regular generics - derived ones don't
                // compile), it falls back to the syntactic key, trading that
                // pathological corner's strictness for termination. The key
                // also carries the walk's flatten liveness, so a visit through
                // a phase-dead field can't mask a later live visit; liveness
                // only shrinks along a path and has four values, so the key
                // space stays finite.
                let reference_key = (resolved_reference_key(reference, env), root_directions);
                if follow_named_references && !checked_references.contains(&reference_key) {
                    checked_references.insert(reference_key);
                    if let Some(ty) = named_reference_ty(reference, types) {
                        let name = types
                            .get(reference)
                            .map(|ndt| ndt.name.to_string())
                            .unwrap_or_else(|| path.clone());
                        // A resolved definition's `Generic` placeholders are
                        // bound by *this* reference's concrete arguments,
                        // which were written in the current scope - hence a
                        // new frame with `parent: env`. Inline datatypes were
                        // written at the use site, so they keep the current
                        // scope.
                        let reference_frame = match &reference.inner {
                            NamedReferenceType::Reference { generics, .. } => Some(GenericEnv {
                                map: generics,
                                parent: env,
                            }),
                            NamedReferenceType::Inline { .. }
                            | NamedReferenceType::Recursive(_) => None,
                        };
                        inner(
                            ty,
                            types,
                            checked_references,
                            name,
                            mode,
                            follow_named_references,
                            reference_frame.as_ref().or(env),
                            root_directions,
                        )?;
                    }
                }
            }
            Reference::Opaque(reference) => {
                if let Some(phased) = reference.downcast_ref::<PhasedTy>() {
                    if mode == ApplyMode::Unified {
                        return Err(Error::invalid_phased_type_usage(
                            path,
                            "`specta_serde::Phased<Serialize, Deserialize>` requires `PhasesFormat`",
                        ));
                    }

                    // Each phased side is only ever rendered for its own
                    // phase, so flatten liveness narrows accordingly.
                    inner(
                        &phased.serialize,
                        types,
                        checked_references,
                        format!("{path}.<phased_serialize>"),
                        mode,
                        follow_named_references,
                        env,
                        root_directions.and(FlattenDirections::SERIALIZE_ONLY),
                    )?;
                    inner(
                        &phased.deserialize,
                        types,
                        checked_references,
                        format!("{path}.<phased_deserialize>"),
                        mode,
                        follow_named_references,
                        env,
                        root_directions.and(FlattenDirections::DESERIALIZE_ONLY),
                    )?;
                }
            }
        },
        DataType::Generic(_) | DataType::Primitive(_) => {}
    }

    Ok(())
}

fn validate_identifier_enum(enm: &Enum, path: &str, mode: ApplyMode) -> Result<(), Error> {
    let Some(attrs) = SerdeContainerAttrs::from_attributes(&enm.attributes)? else {
        return Ok(());
    };

    if !attrs.variant_identifier && !attrs.field_identifier {
        return Ok(());
    }

    if attrs.variant_identifier && attrs.field_identifier {
        return Err(Error::invalid_phased_type_usage(
            path,
            "`variant_identifier` and `field_identifier` cannot be used together",
        ));
    }

    if mode == ApplyMode::Unified {
        return Err(Error::invalid_phased_type_usage(
            path,
            "identifier enums require `PhasesFormat` because they widen deserialize-only input shape",
        ));
    }

    if attrs.variant_identifier
        && let Some((name, _)) = enm
            .variants
            .iter()
            .find(|(_, variant)| !matches!(&variant.fields, Fields::Unit))
    {
        return Err(Error::invalid_phased_type_usage(
            path,
            format!(
                "`variant_identifier` requires all unit variants, but variant `{name}` is not unit"
            ),
        ));
    }

    if attrs.field_identifier {
        let variants = &enm.variants;
        for (idx, (name, variant)) in variants.iter().enumerate() {
            let is_last = idx + 1 == variants.len();
            match &variant.fields {
                Fields::Unit => {}
                Fields::Unnamed(unnamed) if is_last && unnamed.fields.len() == 1 => {}
                _ => {
                    return Err(Error::invalid_phased_type_usage(
                        path,
                        format!(
                            "`field_identifier` requires unit variants and an optional final newtype fallback; invalid variant `{name}`"
                        ),
                    ));
                }
            }
        }
    }

    Ok(())
}

fn validate_container_attributes(
    attrs: &specta::datatype::Attributes,
    types: &Types,
    checked_references: &mut HashSet<(Reference, FlattenDirections)>,
    path: &str,
    mode: ApplyMode,
    env: Option<&GenericEnv<'_>>,
    root_directions: FlattenDirections,
) -> Result<(), Error> {
    let Some(container_attrs) = SerdeContainerAttrs::from_attributes(attrs)? else {
        return Ok(());
    };

    if container_attrs.from.is_some() && container_attrs.try_from.is_some() {
        return Err(Error::invalid_conversion_usage(
            path,
            "`from` and `try_from` cannot be used together",
        ));
    }

    // A directional conversion target is only used in its own phase (`into`
    // only ever serializes, `from`/`try_from` only ever deserialize - see
    // `select_conversion_target`), so flatten liveness narrows per target,
    // composed with the caller's root liveness.
    for (suffix, target, target_directions) in [
        (
            "<serde_into>",
            container_attrs.resolved_into.as_ref(),
            FlattenDirections::SERIALIZE_ONLY,
        ),
        (
            "<serde_from>",
            container_attrs.resolved_from.as_ref(),
            FlattenDirections::DESERIALIZE_ONLY,
        ),
        (
            "<serde_try_from>",
            container_attrs.resolved_try_from.as_ref(),
            FlattenDirections::DESERIALIZE_ONLY,
        ),
    ] {
        if let Some(target) = target {
            inner(
                target,
                types,
                checked_references,
                format!("{path}.{suffix}"),
                mode,
                true,
                env,
                root_directions.and(target_directions),
            )?;
        }
    }

    if mode == ApplyMode::Unified {
        // The effective container name per direction defaults to the type's
        // own name (`path` is the type name here: the traversal resets it at
        // every named reference), so a one-sided rename equal to that name is
        // a no-op. `rename_all` / `rename_all_fields` are validated per field
        // and variant key via `validate_named_field_keys` /
        // `validate_variant_key` instead of comparing the raw rules.
        let rename_serialize = container_attrs.rename_serialize.as_deref().unwrap_or(path);
        let rename_deserialize = container_attrs
            .rename_deserialize
            .as_deref()
            .unwrap_or(path);
        if rename_serialize != rename_deserialize {
            return Err(Error::incompatible_rename(
                "container rename",
                path.to_string(),
                Some(rename_serialize.to_string()),
                Some(rename_deserialize.to_string()),
            ));
        }
    }

    Ok(())
}

fn validate_variant_attributes(
    variant: &Variant,
    path: String,
    mode: ApplyMode,
) -> Result<(), Error> {
    let Some(serde_attrs) = SerdeVariantAttrs::from_attributes(&variant.attributes)? else {
        return Ok(());
    };

    let has_type_override = has_type_override(&variant.attributes);

    if serde_attrs.has_serialize_with {
        ensure_codec_override(has_type_override, &path, "serialize_with")?;
    }
    if serde_attrs.has_deserialize_with {
        ensure_codec_override(has_type_override, &path, "deserialize_with")?;
    }
    if serde_attrs.has_with {
        ensure_codec_override(has_type_override, &path, "with")?;
    }

    // Variant renames and rename_all rules are validated by comparing the
    // *effective* per-direction names/keys in `validate_variant_key` and
    // `validate_named_field_keys` (see `inner`'s enum arm), not the raw attrs.

    if mode == ApplyMode::Unified
        && !variant.skip
        && serde_attrs.skip_serializing != serde_attrs.skip_deserializing
    {
        // Applies regardless of `untagged`: a mismatched skip means serde
        // constructs/emits this variant on one side but not the other
        // (`variant_is_skipped_for_mode` would otherwise drop it from the
        // unified type entirely, even for the side that still uses it).
        // `#[specta(skip)]` variants are exempt: they are dropped from both
        // phases before the serde skip flags matter.
        return Err(Error::incompatible_skip(
            "variant skip",
            path,
            serde_attrs.skip_serializing,
            serde_attrs.skip_deserializing,
        ));
    }

    Ok(())
}

/// The rename rules a struct's named field keys are subject to, per phase
/// (`Serialize`, then `Deserialize`), computed by the same helper the rewrite
/// path uses.
fn struct_field_key_rules(
    attrs: &specta::datatype::Attributes,
    path: &str,
) -> Result<(Option<RenameRule>, Option<RenameRule>), Error> {
    Ok((
        container_rename_all_rule(attrs, PhaseRewrite::Serialize, "struct rename_all", path)?,
        container_rename_all_rule(attrs, PhaseRewrite::Deserialize, "struct rename_all", path)?,
    ))
}

/// The rename rules an enum variant's named field keys are subject to
/// (variant `rename_all`, falling back to the container's
/// `rename_all_fields`), per phase, computed by the same helper the rewrite
/// path uses.
fn variant_field_key_rules(
    container_attrs: &Option<SerdeContainerAttrs>,
    variant: &Variant,
    variant_name: &str,
) -> Result<(Option<RenameRule>, Option<RenameRule>), Error> {
    Ok((
        enum_variant_field_rename_rule(
            container_attrs,
            variant,
            PhaseRewrite::Serialize,
            variant_name,
        )?,
        enum_variant_field_rename_rule(
            container_attrs,
            variant,
            PhaseRewrite::Deserialize,
            variant_name,
        )?,
    ))
}

/// Unified mode requires every live named field key to resolve to the same
/// effective wire key in both directions. Effective keys are computed with
/// [`phase_field_key`] — the exact function the rewrite path uses — so
/// explicit renames, rename rules applied to default names, and their no-op
/// coincidences are all judged by what actually reaches the wire, never by
/// comparing raw attribute options.
fn validate_named_field_keys(
    named: &NamedFields,
    (rule_serialize, rule_deserialize): (Option<RenameRule>, Option<RenameRule>),
    path: &str,
    declared_deserialize_live: bool,
    mode: ApplyMode,
) -> Result<(), Error> {
    if mode != ApplyMode::Unified || !declared_deserialize_live {
        return Ok(());
    }

    let mut occupied_deserialize_keys = HashMap::new();
    for (name, field) in &named.fields {
        if !field_has_live_key(field)? {
            continue;
        }

        let serde_attrs = SerdeFieldAttrs::from_attributes(&field.attributes)?;
        let serialize_key = phase_field_key(
            name.as_ref(),
            serde_attrs.as_ref(),
            rule_serialize,
            PhaseRewrite::Serialize,
        )?;
        let deserialize_key = phase_field_key(
            name.as_ref(),
            serde_attrs.as_ref(),
            rule_deserialize,
            PhaseRewrite::Deserialize,
        )?;

        if serialize_key != deserialize_key {
            return Err(Error::incompatible_rename(
                "field key",
                format!("{path}.{name}"),
                Some(serialize_key),
                Some(deserialize_key),
            ));
        }

        occupied_deserialize_keys.insert(deserialize_key, name.as_ref());
    }

    for (name, field) in &named.fields {
        if !field_has_live_key(field)? {
            continue;
        }

        let Some(attrs) = SerdeFieldAttrs::from_attributes(&field.attributes)? else {
            continue;
        };
        for alias in attrs.aliases {
            if let Some(owner) = occupied_deserialize_keys.get(alias.as_str())
                && *owner != name.as_ref()
            {
                return Err(Error::invalid_phased_type_usage(
                    format!("{path}.{name}"),
                    format!(
                        "field alias `{alias}` collides with a key already accepted by `{owner}`; unified alias lowering cannot represent serde's key precedence safely"
                    ),
                ));
            }
            occupied_deserialize_keys.insert(alias, name.as_ref());
        }
    }

    Ok(())
}

/// Unified mode requires an emitted variant label to resolve to the same
/// effective name in both directions, computed with
/// [`serialized_variant_name`] — the exact function the rewrite path uses.
/// Variant-level `#[serde(untagged)]` variants and variants that are never
/// rendered have no emitted label to compare.
fn validate_variant_key(
    variant_name: &str,
    variant: &Variant,
    container_attrs: &Option<SerdeContainerAttrs>,
    path: &str,
    mode: ApplyMode,
) -> Result<(), Error> {
    // Dead variants (see `variant_is_rendered`) are gated by the caller.
    if mode != ApplyMode::Unified {
        return Ok(());
    }

    if SerdeVariantAttrs::from_attributes(&variant.attributes)?.is_some_and(|attrs| attrs.untagged)
    {
        return Ok(());
    }

    let serialize_name = serialized_variant_name(
        variant_name,
        variant,
        container_attrs,
        PhaseRewrite::Serialize,
    )?;
    let deserialize_name = serialized_variant_name(
        variant_name,
        variant,
        container_attrs,
        PhaseRewrite::Deserialize,
    )?;

    if serialize_name != deserialize_name {
        return Err(Error::incompatible_rename(
            "variant name",
            format!("{path}::{variant_name}"),
            Some(serialize_name),
            Some(deserialize_name),
        ));
    }

    Ok(())
}

fn validate_variant_aliases(
    enm: &Enum,
    container_attrs: &Option<SerdeContainerAttrs>,
    path: &str,
    declared_deserialize_live: bool,
    mode: ApplyMode,
) -> Result<(), Error> {
    if mode != ApplyMode::Unified || !declared_deserialize_live {
        return Ok(());
    }

    let mut occupied_deserialize_names = HashMap::new();
    for (name, variant) in &enm.variants {
        let attrs = SerdeVariantAttrs::from_attributes(&variant.attributes)?;
        if !FlattenDirections::for_variant(variant)?.deserialize
            || attrs.as_ref().is_some_and(|attrs| attrs.untagged)
        {
            continue;
        }

        occupied_deserialize_names.insert(
            serialized_variant_name(name, variant, container_attrs, PhaseRewrite::Deserialize)?,
            name.as_ref(),
        );
    }

    for (name, variant) in &enm.variants {
        let attrs = SerdeVariantAttrs::from_attributes(&variant.attributes)?;
        if !FlattenDirections::for_variant(variant)?.deserialize
            || attrs.as_ref().is_some_and(|attrs| attrs.untagged)
        {
            continue;
        }

        for alias in attrs.into_iter().flat_map(|attrs| attrs.aliases) {
            if let Some(owner) = occupied_deserialize_names.get(alias.as_str())
                && *owner != name.as_ref()
            {
                return Err(Error::invalid_phased_type_usage(
                    format!("{path}::{name}"),
                    format!(
                        "variant alias `{alias}` collides with a name already accepted by `{owner}`; unified alias lowering cannot represent serde's name precedence safely"
                    ),
                ));
            }
            occupied_deserialize_names.insert(alias, name.as_ref());
        }
    }

    Ok(())
}

/// Whether the variant is rendered in at least one phase. `#[specta(skip)]`
/// variants (also set by `#[serde(skip)]` at derive time) and variants
/// serde-skipped in both directions are dropped by
/// `filter_enum_variants_for_phase` before either phase's output is produced,
/// so nothing about them — names, field keys, skip asymmetry — can create a
/// phase difference.
fn variant_is_rendered(variant: &Variant) -> Result<bool, Error> {
    if variant.skip {
        return Ok(false);
    }

    Ok(!SerdeVariantAttrs::from_attributes(&variant.attributes)?
        .is_some_and(|attrs| attrs.skip_serializing && attrs.skip_deserializing))
}

fn variant_is_serialized(variant: &Variant) -> Result<bool, Error> {
    if variant.skip {
        return Ok(false);
    }

    Ok(!SerdeVariantAttrs::from_attributes(&variant.attributes)?
        .is_some_and(|attrs| attrs.skip_serializing))
}

/// Whether a named field's *key* is part of the local wire shape: excludes
/// fields with an erased type (never rendered), flattened fields (their keys
/// come from the flattened type), and fields skipped in both directions.
fn field_has_live_key(field: &Field) -> Result<bool, Error> {
    if field.ty.is_none() {
        return Ok(false);
    }

    let Some(attrs) = SerdeFieldAttrs::from_attributes(&field.attributes)? else {
        return Ok(true);
    };

    Ok(!(attrs.flatten || attrs.skip_serializing && attrs.skip_deserializing))
}

fn validate_field_attributes(field: &Field, path: String, mode: ApplyMode) -> Result<(), Error> {
    let Some(serde_attrs) = SerdeFieldAttrs::from_attributes(&field.attributes)? else {
        return Ok(());
    };

    // Field renames are validated by comparing the *effective* per-direction
    // keys in `validate_named_field_keys` (unnamed fields have no wire key,
    // so renames are meaningless for them).

    if mode == ApplyMode::Unified
        && !field
            .attributes
            .contains_key(crate::SERDE_NEWTYPE_SKIP_IGNORED)
        && serde_attrs.skip_serializing != serde_attrs.skip_deserializing
    {
        // Plain `#[serde(skip)]` sets both flags and never reaches here.
        // A one-sided skip means serde requires/emits the field on one side
        // but not the other, which a unified shape can't represent: it would
        // either wrongly drop a field one side needs, or wrongly keep a field
        // the other side never sees.
        return Err(Error::incompatible_skip(
            "field skip",
            path,
            serde_attrs.skip_serializing,
            serde_attrs.skip_deserializing,
        ));
    }

    let has_type_override = has_type_override(&field.attributes);

    if serde_attrs.has_serialize_with {
        ensure_codec_override(has_type_override, &path, "serialize_with")?;
    }
    if serde_attrs.has_deserialize_with {
        ensure_codec_override(has_type_override, &path, "deserialize_with")?;
    }
    if serde_attrs.has_with {
        ensure_codec_override(has_type_override, &path, "with")?;
    }

    Ok(())
}

fn validate_unnamed_conditional_omission(
    unnamed: &specta::datatype::UnnamedFields,
    path: &str,
) -> Result<(), Error> {
    let mut conditional_omission = None;
    for (idx, field) in unnamed.fields.iter().enumerate() {
        if unnamed_field_is_absent_from_serialization(field)? {
            continue;
        }

        if let Some(omitted_idx) = conditional_omission {
            return Err(Error::invalid_phased_type_usage(
                format!("{path}[{omitted_idx}]"),
                "`skip_serializing_if` on an unnamed field is only representable on the final live field because omitting an earlier element shifts every following value",
            ));
        }

        let has_skip_serializing_if = SerdeFieldAttrs::from_attributes(&field.attributes)?
            .is_some_and(|attrs| attrs.skip_serializing_if.is_some());
        if has_skip_serializing_if {
            conditional_omission = Some(idx);
        }
    }

    Ok(())
}

fn unnamed_field_is_absent_from_serialization(field: &Field) -> Result<bool, Error> {
    if field.ty.is_none() {
        return Ok(true);
    }

    Ok(SerdeFieldAttrs::from_attributes(&field.attributes)?
        .is_some_and(|attrs| attrs.skip_serializing))
}

/// Validates a `#[serde(flatten)]`-ed field's own type, independent of the
/// caller's usual traversal. Serde only lets a flattened field serialize as a
/// struct or map (or an `Option`/reference resolving to one); anything else
/// is a runtime `"can only flatten structs and maps"` error from serde_json,
/// so we surface it at export time instead.
fn validate_flatten_field(
    field: &Field,
    ty: &DataType,
    types: &Types,
    path: &str,
    mode: ApplyMode,
    container: FlattenDirections,
    env: Option<&GenericEnv<'_>>,
) -> Result<(), Error> {
    let Some(serde_attrs) = SerdeFieldAttrs::from_attributes(&field.attributes)? else {
        return Ok(());
    };

    if !serde_attrs.flatten {
        return Ok(());
    }

    // A skipped direction never flattens, so its shape can't cause a flatten
    // error however non-flattenable it is (verified against serde_json 1.x).
    // Full `#[serde(skip)]` erases the field's type entirely and never
    // reaches this check; one-sided skips keep the type but kill their
    // direction. Under `PhasesFormat` the skipped phase drops the field
    // before flatten lowering; unified mode drops the field for a skip in
    // *either* direction (see `should_skip_field_for_mode` in `lib.rs`), so
    // any skip means nothing flattens there. The enclosing container's
    // liveness (an enum variant's own one-sided skips - see
    // `filter_enum_variants_for_phase`) ANDs in: a phase the variant doesn't
    // exist in can't flatten any of its fields.
    let directions = FlattenDirections {
        serialize: container.serialize && !serde_attrs.skip_serializing,
        deserialize: container.deserialize && !serde_attrs.skip_deserializing,
    };

    let live = match mode {
        ApplyMode::Unified => directions.serialize && directions.deserialize,
        ApplyMode::Phases => directions.serialize || directions.deserialize,
    };
    if !live {
        return Ok(());
    }

    validate_flatten_target(ty, types, path, &mut HashSet::new(), env, directions)
}

/// Which directions of a flattened field actually hit the wire. A direction
/// killed by a one-sided serde skip doesn't need a flattenable shape, which
/// matters wherever the two directions' shapes can diverge: explicit
/// [`PhasedTy`] overrides and container conversion targets.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct FlattenDirections {
    serialize: bool,
    deserialize: bool,
}

impl FlattenDirections {
    /// Both directions hit the wire (a plain struct field's container).
    const LIVE: Self = Self {
        serialize: true,
        deserialize: true,
    };

    /// The single direction a per-phase rendering serves:
    /// `PhasesFormat::map_type` selects and rewrites one phase of an inline
    /// datatype, so only that phase's flatten shapes are reachable there.
    fn for_phase(phase: Phase) -> Self {
        match phase {
            Phase::Serialize => Self::SERIALIZE_ONLY,
            Phase::Deserialize => Self::DESERIALIZE_ONLY,
        }
    }

    /// Both liveness constraints apply (e.g. a variant's own skips *and* the
    /// walk's root phase).
    fn and(self, other: Self) -> Self {
        Self {
            serialize: self.serialize && other.serialize,
            deserialize: self.deserialize && other.deserialize,
        }
    }

    /// Only the serialize direction is reachable (an `into` conversion wire,
    /// or a `Phased` override's serialize side).
    const SERIALIZE_ONLY: Self = Self {
        serialize: true,
        deserialize: false,
    };

    /// Only the deserialize direction is reachable (a `from`/`try_from`
    /// conversion wire, or a `Phased` override's deserialize side).
    const DESERIALIZE_ONLY: Self = Self {
        serialize: false,
        deserialize: true,
    };

    /// The directions in which a field exists on the wire: a one-sided serde
    /// skip removes it from that phase before the rewrite ever visits its
    /// type (`should_skip_field_for_mode` in `lib.rs`), so nothing reached
    /// *through* the field can flatten there. (Fully skipped fields carry no
    /// type and are never walked at all.)
    fn for_field(field: &Field) -> Result<Self, Error> {
        if field
            .attributes
            .contains_key(crate::SERDE_NEWTYPE_SKIP_IGNORED)
        {
            return Ok(Self::LIVE);
        }
        let attrs = SerdeFieldAttrs::from_attributes(&field.attributes)?;
        Ok(Self {
            serialize: !attrs.as_ref().is_some_and(|attrs| attrs.skip_serializing),
            deserialize: !attrs.as_ref().is_some_and(|attrs| attrs.skip_deserializing),
        })
    }

    /// The directions in which an enum variant exists on the wire: a fully
    /// skipped variant exists in neither, and a one-sided variant skip drops
    /// it from that phase (`filter_enum_variants_for_phase` in `lib.rs`).
    fn for_variant(variant: &Variant) -> Result<Self, Error> {
        if variant.skip {
            return Ok(Self {
                serialize: false,
                deserialize: false,
            });
        }

        let attrs = SerdeVariantAttrs::from_attributes(&variant.attributes)?;
        Ok(Self {
            serialize: !attrs.as_ref().is_some_and(|attrs| attrs.skip_serializing),
            deserialize: !attrs.as_ref().is_some_and(|attrs| attrs.skip_deserializing),
        })
    }
}

/// Lexically scoped substitution environment for resolving
/// [`DataType::Generic`] placeholders while checking a flatten target.
///
/// Each time the main [`inner`] walk or [`validate_flatten_target`] follows a
/// named reference that carries concrete generic arguments, it pushes a new
/// frame mapping the definition's parameters to those arguments (so a flatten
/// field declared *inside* a generic definition is checked against each
/// instantiation it's reached through). The arguments themselves were
/// written one scope up, so when a placeholder resolves to an argument, the
/// argument is validated against this frame's `parent`. Substitution is lazy
/// (no datatype is ever rewritten), which keeps the `seen` keys syntactic and
/// guarantees termination even for non-regular recursive generics like
/// `struct N<T>(Box<N<Option<T>>>)`.
struct GenericEnv<'a> {
    map: &'a [(Generic, DataType)],
    parent: Option<&'a GenericEnv<'a>>,
}

/// Upper bound on the number of datatype nodes materialized while resolving a
/// reference's generic arguments for a memo key. Regular type graphs resolve
/// in a handful of nodes; only a hand-built non-regular generic (e.g.
/// `N<T>` whose body names `N<Option<T>>` - the derive can't produce one,
/// rustc fails to monomorphize `Type` for it) grows without bound, and the
/// budget converts that into a fallback to the syntactic key so the walk
/// still terminates.
const RESOLVED_KEY_NODE_BUDGET: usize = 256;

/// Builds the `checked_references` memo key for a named reference: the
/// reference with its generic arguments resolved through `env`, so the same
/// syntactic reference reached under different substitutions gets distinct
/// keys (and identical substitutions still dedupe). Falls back to the
/// syntactic reference when there is nothing to resolve or the node budget is
/// exhausted.
///
/// Termination: every key is either a syntactic reference occurring in the
/// finite type graph, or a reference whose resolved arguments are trees of at
/// most [`RESOLVED_KEY_NODE_BUDGET`] nodes built from clones of graph nodes -
/// a finite set either way, so a walk deduped on these keys terminates.
fn resolved_reference_key(reference: &NamedReference, env: Option<&GenericEnv<'_>>) -> Reference {
    let mut resolved = reference.clone();

    if env.is_some() {
        let mut budget = RESOLVED_KEY_NODE_BUDGET;
        let ok = match &mut resolved.inner {
            NamedReferenceType::Reference { generics, .. } => generics
                .iter_mut()
                .all(|(_, dt)| resolve_generics_for_key(dt, env, &mut budget)),
            // An `#[specta(inline)]` reference carries its instantiation
            // inside the inline datatype itself (as unresolved `Generic`
            // placeholders when it sits in another generic definition), so
            // the key must resolve that content too or two instantiations of
            // the same inline reference would collide.
            NamedReferenceType::Inline { dt, .. } => resolve_generics_for_key(dt, env, &mut budget),
            NamedReferenceType::Recursive(_) => true,
        };

        if !ok {
            return Reference::Named(reference.clone());
        }
    }

    Reference::Named(resolved)
}

/// Resolves [`DataType::Generic`] placeholders in `dt` (in place) through the
/// substitution environment, for memo-key purposes. Returns `false` when the
/// node budget runs out, in which case the caller discards the partial result
/// and falls back to the syntactic key. Datatypes stored inside attributes
/// (resolved conversion targets) are not rewritten; a key collision there
/// would only re-skip a validation, never mis-validate.
fn resolve_generics_for_key(
    dt: &mut DataType,
    env: Option<&GenericEnv<'_>>,
    budget: &mut usize,
) -> bool {
    if *budget == 0 {
        return false;
    }
    *budget -= 1;

    match dt {
        DataType::Generic(generic) => {
            let Some((arg, parent)) = env.and_then(|env| {
                env.map
                    .iter()
                    .find(|(param, _)| param == generic)
                    .map(|(_, arg)| (arg, env.parent))
            }) else {
                return true;
            };

            // The argument was written one scope up, so it resolves against
            // the parent environment.
            let mut resolved = arg.clone();
            if !resolve_generics_for_key(&mut resolved, parent, budget) {
                return false;
            }
            *dt = resolved;
            true
        }
        DataType::Nullable(inner) => resolve_generics_for_key(inner, env, budget),
        DataType::List(list) => resolve_generics_for_key(&mut list.ty, env, budget),
        DataType::Map(map) => {
            resolve_generics_for_key(map.key_ty_mut(), env, budget)
                && resolve_generics_for_key(map.value_ty_mut(), env, budget)
        }
        DataType::Tuple(tuple) => tuple
            .elements
            .iter_mut()
            .all(|element| resolve_generics_for_key(element, env, budget)),
        DataType::Intersection(parts) => parts
            .iter_mut()
            .all(|part| resolve_generics_for_key(part, env, budget)),
        DataType::Struct(strct) => resolve_fields_generics_for_key(&mut strct.fields, env, budget),
        DataType::Enum(enm) => enm
            .variants
            .iter_mut()
            .all(|(_, variant)| resolve_fields_generics_for_key(&mut variant.fields, env, budget)),
        DataType::Reference(Reference::Named(reference)) => match &mut reference.inner {
            NamedReferenceType::Reference { generics, .. } => generics
                .iter_mut()
                .all(|(_, dt)| resolve_generics_for_key(dt, env, budget)),
            NamedReferenceType::Inline { dt, .. } => resolve_generics_for_key(dt, env, budget),
            NamedReferenceType::Recursive(_) => true,
        },
        // A `Phased` override wraps datatypes that may themselves contain
        // `Generic` placeholders (e.g. `Phased<T, Inner>` used as a generic
        // argument), so its contents must be resolved for the key too; a
        // rebuilt opaque with equal contents still compares/hashes equal.
        DataType::Reference(Reference::Opaque(reference)) => {
            match reference.downcast_ref::<PhasedTy>() {
                Some(phased) => {
                    let mut serialize = phased.serialize.clone();
                    let mut deserialize = phased.deserialize.clone();
                    if !resolve_generics_for_key(&mut serialize, env, budget)
                        || !resolve_generics_for_key(&mut deserialize, env, budget)
                    {
                        return false;
                    }
                    *dt = DataType::Reference(Reference::opaque(PhasedTy {
                        serialize,
                        deserialize,
                    }));
                    true
                }
                // Other opaque references are exporter-specific leaves.
                None => true,
            }
        }
        DataType::Primitive(_) => true,
    }
}

fn resolve_fields_generics_for_key(
    fields: &mut Fields,
    env: Option<&GenericEnv<'_>>,
    budget: &mut usize,
) -> bool {
    match fields {
        Fields::Unit => true,
        Fields::Unnamed(unnamed) => unnamed
            .fields
            .iter_mut()
            .filter_map(|field| field.ty.as_mut())
            .all(|ty| resolve_generics_for_key(ty, env, budget)),
        Fields::Named(named) => named
            .fields
            .iter_mut()
            .filter_map(|(_, field)| field.ty.as_mut())
            .all(|ty| resolve_generics_for_key(ty, env, budget)),
    }
}

/// `seen` tracks named references currently being resolved on this flatten
/// check so a cyclic type graph terminates instead of recursing forever. This
/// mirrors the `seen`/`checked_references` discipline used elsewhere in this
/// module, but is scoped to a single field's flatten check rather than shared
/// with the outer traversal, since a reference already validated elsewhere
/// still needs its shape checked here.
fn validate_flatten_target(
    ty: &DataType,
    types: &Types,
    path: &str,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    directions: FlattenDirections,
) -> Result<(), Error> {
    match ty {
        // Maps merge directly.
        DataType::Map(_) => Ok(()),
        // Enums are accepted for all four serde representations: external,
        // internal, and adjacent tagging always produce map-shaped output
        // when flattened (verified against serde_json 1.x - even an
        // externally tagged unit variant flattens as `"Variant": null`). An
        // untagged enum only flattens when the active variant's payload is
        // map-shaped, but rejecting it statically would break the common
        // pattern of flattening `serde_json::Value` (an untagged enum) for
        // extra fields, so it is deliberately accepted. Container
        // conversions still substitute the wire type, though, so any
        // declared conversion targets are checked instead.
        DataType::Enum(enm) => {
            let attrs = SerdeContainerAttrs::from_attributes(&enm.attributes)?;
            if let Some((serialize_wire, deserialize_wire)) =
                conversion_wire_targets(attrs.as_ref())
            {
                validate_conversion_wires(
                    serialize_wire,
                    deserialize_wire,
                    types,
                    path,
                    seen,
                    env,
                    directions,
                )?;
            }

            Ok(())
        }
        // An intersection is a structural merge of object-like types (see
        // `DataType::Intersection`'s docs), so it is map-shaped by
        // construction. It can't come out of a derive - specta-serde only
        // produces it *after* validation, when lowering flatten itself - but
        // a manually built one must not be false-positived on.
        DataType::Intersection(_) => Ok(()),
        // A flattened `Option<T>` (including nested `Option<Option<T>>`)
        // contributes nothing when absent and validates as `T` when present.
        DataType::Nullable(inner) => {
            validate_flatten_target(inner, types, path, seen, env, directions)
        }
        // `()` and other zero-element tuples serialize as nothing, so
        // flattening one is a harmless no-op (verified against serde_json).
        DataType::Tuple(tuple) if tuple.elements.is_empty() => Ok(()),
        DataType::Struct(strct) => {
            let attrs = SerdeContainerAttrs::from_attributes(&strct.attributes)?;

            // `#[serde(into/from/try_from)]` substitute the serde *wire* type
            // for the declared shape, per direction, so the wire types are
            // what serde actually flattens - and only for directions that are
            // actually live. A live direction without a conversion keeps the
            // declared shape, so the declared-shape checks below run for
            // exactly the remaining uncovered live directions. (Unified mode
            // separately rejects one-sided conversions during the rewrite -
            // see `select_conversion_target` in lib.rs - which this mirrors.)
            let directions = match conversion_wire_targets(attrs.as_ref()) {
                Some((serialize_wire, deserialize_wire)) => {
                    let remaining = validate_conversion_wires(
                        serialize_wire,
                        deserialize_wire,
                        types,
                        path,
                        seen,
                        env,
                        directions,
                    )?;

                    if !remaining.serialize && !remaining.deserialize {
                        return Ok(());
                    }

                    remaining
                }
                None => directions,
            };

            // `#[serde(transparent)]` structs delegate (de)serialization
            // straight to their single non-skipped field, so serde sees that
            // field's shape - not a struct/tuple wrapper - when flattened.
            if attrs.is_some_and(|attrs| attrs.transparent) {
                let inner_fields = match &strct.fields {
                    Fields::Unit => return Ok(()),
                    Fields::Unnamed(unnamed) => unnamed
                        .fields
                        .iter()
                        .filter_map(|f| f.ty.as_ref())
                        .collect::<Vec<_>>(),
                    Fields::Named(named) => named
                        .fields
                        .iter()
                        .filter_map(|(_, f)| f.ty.as_ref())
                        .collect::<Vec<_>>(),
                };

                return match inner_fields.as_slice() {
                    // Not exactly one live field: leave it be rather than
                    // false-positive on a shape we can't confidently resolve.
                    [only] => validate_flatten_target(only, types, path, seen, env, directions),
                    _ => Ok(()),
                };
            }

            match &strct.fields {
                Fields::Unit | Fields::Named(_) => Ok(()),
                Fields::Unnamed(unnamed) => match unnamed.fields.as_slice() {
                    // A tuple struct with exactly one declared field is a
                    // newtype: serde delegates (de)serialization straight to
                    // the inner value (no `#[serde(transparent)]` needed), so
                    // the flatten target is the inner value's shape.
                    [single] => match &single.ty {
                        Some(ty) => validate_flatten_target(ty, types, path, seen, env, directions),
                        // The field is skipped, so its type is unknowable
                        // here (serde still delegates to it when
                        // serializing); don't false-positive.
                        None => Ok(()),
                    },
                    // Zero or multiple declared fields serialize as a
                    // sequence (`[]` / `[a, b]`), even when skips leave one
                    // live field - verified against serde_json 1.x.
                    _ => Err(Error::invalid_flatten_target(
                        path.to_string(),
                        "tuple structs serialize as a sequence; serde can only flatten structs and maps",
                    )),
                },
            }
        }
        DataType::Reference(Reference::Named(reference)) => {
            let key = Reference::Named(reference.clone());
            if !seen.insert(key.clone()) {
                return Ok(());
            }

            let result = match &reference.inner {
                // Inline datatypes were written at the use site, so they
                // resolve generic placeholders against the current scope.
                NamedReferenceType::Inline { dt, .. } => {
                    validate_flatten_target(dt, types, path, seen, env, directions)
                }
                // The resolved definition's placeholders resolve against this
                // reference's concrete arguments, which themselves were
                // written in the current scope - hence `parent: env`.
                NamedReferenceType::Reference { generics, .. } => types
                    .get(reference)
                    .and_then(|ndt| ndt.ty.as_ref())
                    .map_or(Ok(()), |ty| {
                        validate_flatten_target(
                            ty,
                            types,
                            path,
                            seen,
                            Some(&GenericEnv {
                                map: generics,
                                parent: env,
                            }),
                            directions,
                        )
                    }),
                // A recursive-inline marker is a cycle we've already checked.
                NamedReferenceType::Recursive(_) => Ok(()),
            };
            seen.remove(&key);

            result
        }
        // A generic placeholder is knowable whenever the enclosing definition
        // was reached through a reference carrying concrete arguments:
        // substitute and validate the argument in the scope it was written
        // in. A bare placeholder (validating the generic definition itself)
        // stays accepted - serde allows it whenever the instantiation is
        // struct/map-shaped, so rejecting it would be a false positive.
        DataType::Generic(generic) => {
            match env.and_then(|env| env.map.iter().find(|(param, _)| param == generic)) {
                Some((_, arg)) => validate_flatten_target(
                    arg,
                    types,
                    path,
                    seen,
                    env.and_then(|env| env.parent),
                    directions,
                ),
                None => Ok(()),
            }
        }
        DataType::Reference(Reference::Opaque(reference)) => {
            // An explicit `specta_serde::Phased<Serialize, Deserialize>`
            // override is stored as an opaque reference wrapping both phase
            // shapes; each *live* phase must be a valid flatten target. A
            // phase killed by a one-sided skip never flattens (`PhasesFormat`
            // drops the field from that phase before flatten lowering), so
            // its shape is unreachable and must not be validated. Each side
            // is also only ever exported/rewritten *for its own phase*, so it
            // is validated in that direction alone: e.g. the serialize
            // shape's deserialize-facing details (a raw shape behind an
            // `into`-only conversion, a `from` wire) are unreachable.
            match reference.downcast_ref::<PhasedTy>() {
                Some(phased) => {
                    if directions.serialize {
                        validate_flatten_target(
                            &phased.serialize,
                            types,
                            path,
                            seen,
                            env,
                            FlattenDirections::SERIALIZE_ONLY,
                        )?;
                    }
                    if directions.deserialize {
                        validate_flatten_target(
                            &phased.deserialize,
                            types,
                            path,
                            seen,
                            env,
                            FlattenDirections::DESERIALIZE_ONLY,
                        )?;
                    }

                    Ok(())
                }
                // Any other opaque reference is exporter-specific; its shape
                // is unknowable here, so don't false-positive on it.
                None => Ok(()),
            }
        }
        DataType::List(_) | DataType::Tuple(_) | DataType::Primitive(_) => {
            Err(Error::invalid_flatten_target(
                path.to_string(),
                "serde can only flatten structs and maps, but this field's type serializes as a sequence, tuple, or scalar value",
            ))
        }
    }
}

/// Validates the reachable conversion wire types, each in its own direction
/// only: an `into` wire is only ever serialized and a `from`/`try_from` wire
/// only ever deserialized (`select_conversion_target` in `lib.rs` picks by
/// phase), so e.g. a serialize wire's own deserialize-only conversions are
/// unreachable. A wire for a direction that isn't live is skipped entirely.
///
/// Returns the live directions left *uncovered* by a wire - the ones the
/// caller's declared shape still serves.
fn validate_conversion_wires(
    serialize_wire: Option<&DataType>,
    deserialize_wire: Option<&DataType>,
    types: &Types,
    path: &str,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    directions: FlattenDirections,
) -> Result<FlattenDirections, Error> {
    if directions.serialize
        && let Some(wire) = serialize_wire
    {
        validate_flatten_target(
            wire,
            types,
            path,
            seen,
            env,
            FlattenDirections::SERIALIZE_ONLY,
        )?;
    }
    if directions.deserialize
        && let Some(wire) = deserialize_wire
    {
        validate_flatten_target(
            wire,
            types,
            path,
            seen,
            env,
            FlattenDirections::DESERIALIZE_ONLY,
        )?;
    }

    Ok(FlattenDirections {
        serialize: directions.serialize && serialize_wire.is_none(),
        deserialize: directions.deserialize && deserialize_wire.is_none(),
    })
}

/// The serde wire types substituted by container conversions, as
/// `(serialize, deserialize)` targets: `#[serde(into = ...)]` replaces the
/// serialize shape and `#[serde(from/try_from = ...)]` the deserialize shape.
/// Returns `None` when the container declares no conversions. Mirrors the
/// per-direction selection in `select_conversion_target` (`lib.rs`).
fn conversion_wire_targets(
    attrs: Option<&SerdeContainerAttrs>,
) -> Option<(Option<&DataType>, Option<&DataType>)> {
    let attrs = attrs?;
    let serialize = attrs.resolved_into.as_ref();
    let deserialize = attrs
        .resolved_from
        .as_ref()
        .or(attrs.resolved_try_from.as_ref());

    (serialize.is_some() || deserialize.is_some()).then_some((serialize, deserialize))
}

fn ensure_codec_override(
    has_type_override: bool,
    path: &str,
    attr: &'static str,
) -> Result<(), Error> {
    if has_type_override {
        return Ok(());
    }

    Err(Error::unsupported_serde_custom_codec(
        path.to_string(),
        attr,
    ))
}

fn has_type_override(attributes: &specta::datatype::Attributes) -> bool {
    attributes
        .get_named_as::<bool>("specta:type_override")
        .copied()
        .unwrap_or(false)
}

fn validate_enum(
    enm: &Enum,
    types: &Types,
    path: String,
    mode: ApplyMode,
    declared_directions: FlattenDirections,
    env: Option<&GenericEnv<'_>>,
) -> Result<(), Error> {
    let valid_variants = enm
        .variants
        .iter()
        .filter(|(_, variant)| !variant.skip)
        .count();
    if valid_variants == 0 && !&enm.variants.is_empty() {
        return Err(Error::invalid_usage_of_skip(
            path,
            "all variants are skipped, resulting in an invalid non-empty enum",
        ));
    }

    let repr = EnumRepr::from_attrs(&enm.attributes)?;

    validate_untagged_variants(enm, &path)?;
    validate_other_variant(enm, &path, &repr, mode, declared_directions.deserialize)?;
    validate_adjacent_collapsed_newtype_variants(enm, &path, &repr, mode)?;

    if matches!(repr, EnumRepr::Internal { .. }) {
        validate_internally_tag_enum(
            enm,
            types,
            path,
            &mut HashSet::new(),
            env,
            declared_directions,
        )?;
    }

    Ok(())
}

/// An adjacently tagged newtype variant whose sole non-`Option` field is
/// `#[serde(skip)]`ped is direction-asymmetric even though the skip itself is
/// symmetric: serde's serializer omits the `content` key entirely, but its
/// deserializer still requires `content: null`. No unified shape can
/// represent both directions, so unified mode rejects it (matching the
/// policy for one-sided renames/skips). A skipped `Option` sole field is
/// exempt: serde's `missing_field` helper deserializes a missing `content`
/// key as `None`, so the unified `content?: null` shape is exact for both
/// directions.
fn validate_adjacent_collapsed_newtype_variants(
    enm: &Enum,
    path: &str,
    repr: &EnumRepr,
    mode: ApplyMode,
) -> Result<(), Error> {
    if mode != ApplyMode::Unified || !matches!(repr, EnumRepr::Adjacent { .. }) {
        return Ok(());
    }

    for (name, variant) in &enm.variants {
        if variant.skip {
            continue;
        }
        // `#[serde(untagged)]` variants bypass the tag/content representation
        // entirely (they serialize as their bare payload), so there is no
        // `content` key to be asymmetric about.
        if SerdeVariantAttrs::from_attributes(&variant.attributes)?.is_some_and(|attrs| {
            attrs.untagged || (attrs.skip_serializing && attrs.skip_deserializing)
        }) {
            continue;
        }

        let Fields::Unnamed(unnamed) = &variant.fields else {
            continue;
        };
        let [field] = unnamed.fields.as_slice() else {
            continue;
        };

        // Only serde-level symmetric skips exhibit the asymmetry (one-sided
        // skips are already rejected by the field-level check; specta-only
        // skips are invisible to serde).
        if !SerdeFieldAttrs::from_attributes(&field.attributes)?
            .is_some_and(|attrs| attrs.skip_serializing && attrs.skip_deserializing)
        {
            continue;
        }

        if field.attributes.contains_key(crate::NULLABLE_FIELD) {
            continue;
        }

        return Err(Error::invalid_phased_type_usage(
            format!("{path}.{name}"),
            "`#[serde(skip)]` on an adjacently tagged newtype variant's non-`Option` field \
             requires `PhasesFormat` because serde's serializer omits the `content` key while \
             its deserializer requires `content: null`",
        ));
    }

    Ok(())
}

fn validate_untagged_variants(enm: &Enum, path: &str) -> Result<(), Error> {
    let mut seen_untagged = false;

    for (name, variant) in &enm.variants {
        let is_untagged = SerdeVariantAttrs::from_attributes(&variant.attributes)?
            .is_some_and(|attrs| attrs.untagged);

        if is_untagged {
            seen_untagged = true;
            continue;
        }

        if seen_untagged && !variant.skip {
            return Err(Error::invalid_phased_type_usage(
                path,
                format!(
                    "`#[serde(untagged)]` variants must be ordered last, but variant `{name}` appears after an untagged variant"
                ),
            ));
        }
    }

    Ok(())
}

fn validate_other_variant(
    enm: &Enum,
    path: &str,
    repr: &EnumRepr,
    mode: ApplyMode,
    declared_deserializes: bool,
) -> Result<(), Error> {
    let mut other_variants = Vec::new();
    for (name, variant) in &enm.variants {
        if variant_is_rendered(variant)?
            && SerdeVariantAttrs::from_attributes(&variant.attributes)?
                .is_some_and(|attrs| attrs.other)
        {
            other_variants.push((name, variant));
        }
    }

    if other_variants.is_empty() {
        return Ok(());
    }

    if !matches!(
        repr,
        EnumRepr::External | EnumRepr::Internal { .. } | EnumRepr::Adjacent { .. }
    ) {
        return Err(Error::invalid_phased_type_usage(
            path,
            "`#[serde(other)]` requires a tagged enum representation",
        ));
    }

    if other_variants.len() > 1 {
        return Err(Error::invalid_phased_type_usage(
            path,
            "`#[serde(other)]` can only be used on a single enum variant",
        ));
    }

    let (name, variant) = other_variants[0];
    if !matches!(&variant.fields, Fields::Unit) {
        return Err(Error::invalid_phased_type_usage(
            path,
            format!("`#[serde(other)]` variant `{name}` must be a unit variant"),
        ));
    }

    if mode == ApplyMode::Unified
        && declared_deserializes
        && !externally_tagged_unit_enum(enm, repr)?
    {
        return Err(Error::invalid_phased_type_usage(
            path,
            "`#[serde(other)]` requires `PhasesFormat` because a shared tag type cannot exclude known variants soundly",
        ));
    }

    Ok(())
}

fn externally_tagged_unit_enum(enm: &Enum, repr: &EnumRepr) -> Result<bool, Error> {
    if !matches!(repr, EnumRepr::External) {
        return Ok(false);
    }

    for (_, variant) in &enm.variants {
        let attrs = SerdeVariantAttrs::from_attributes(&variant.attributes)?;
        if attrs
            .as_ref()
            .is_some_and(|attrs| attrs.skip_serializing && attrs.skip_deserializing)
        {
            continue;
        }

        if !matches!(variant.fields, Fields::Unit)
            || attrs.as_ref().is_some_and(|attrs| attrs.untagged)
        {
            return Ok(false);
        }
    }

    Ok(true)
}

fn validate_internally_tag_enum(
    enm: &Enum,
    types: &Types,
    path: String,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    directions: FlattenDirections,
) -> Result<(), Error> {
    for (variant_name, variant) in &enm.variants {
        validate_internally_tag_variant(
            enm,
            variant_name,
            variant,
            types,
            &path,
            seen,
            env,
            directions.and(FlattenDirections::for_variant(variant)?),
        )?;
    }

    Ok(())
}

fn validate_internally_tag_variant(
    enm: &Enum,
    variant_name: &str,
    variant: &Variant,
    types: &Types,
    path: &str,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    directions: FlattenDirections,
) -> Result<(), Error> {
    if !directions.serialize && !directions.deserialize {
        return Ok(());
    }

    let _ = enm;
    if SerdeVariantAttrs::from_attributes(&variant.attributes)?.is_some_and(|attrs| attrs.untagged)
    {
        return Ok(());
    }

    match &variant.fields {
        Fields::Unit => Ok(()),
        Fields::Named(named) => {
            for (_, field) in &named.fields {
                let field_directions = directions.and(FlattenDirections::for_field(field)?);
                if field.ty.is_none()
                    && (field_directions.serialize || field_directions.deserialize)
                {
                    return Err(Error::invalid_internally_tagged_enum(
                        path,
                        variant_name,
                        "a payload hidden only from Specta cannot be merged with an internal tag",
                    ));
                }
            }
            Ok(())
        }
        Fields::Unnamed(unnamed) => {
            let mut fields = unnamed
                .fields
                .iter()
                .filter_map(|field| field.ty.as_ref().map(|ty| (field, ty)));
            let Some((_, first_field)) = fields.next() else {
                return Ok(());
            };

            if fields.next().is_some() {
                return Err(Error::invalid_internally_tagged_enum(
                    path,
                    variant_name,
                    "tuple variant must have at most one non-skipped field",
                ));
            }

            validate_internally_tag_enum_datatype(
                first_field,
                types,
                path,
                variant_name,
                seen,
                env,
                true,
                directions,
            )
        }
    }
}

// `seen` tracks named references on the current recursion path so cyclic
// (self- or mutually-recursive) untagged payloads terminate; a reference
// already on the path is being validated further up the stack. Mirrors
// `internal_tag_payload_compatibility` in `lib.rs`.
fn validate_internally_tag_enum_datatype(
    ty: &DataType,
    types: &Types,
    path: &str,
    variant_name: &str,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    reject_contextual_generic_argument: bool,
    directions: FlattenDirections,
) -> Result<(), Error> {
    if !directions.serialize && !directions.deserialize {
        return Ok(());
    }

    let attrs = match ty {
        DataType::Struct(strct) => SerdeContainerAttrs::from_attributes(&strct.attributes)?,
        DataType::Enum(enm) => SerdeContainerAttrs::from_attributes(&enm.attributes)?,
        _ => None,
    };
    let directions = match conversion_wire_targets(attrs.as_ref()) {
        Some((serialize_wire, deserialize_wire)) => {
            let remaining = validate_internally_tag_conversion_wires(
                serialize_wire,
                deserialize_wire,
                types,
                path,
                variant_name,
                seen,
                env,
                reject_contextual_generic_argument,
                directions,
            )?;
            if !remaining.serialize && !remaining.deserialize {
                return Ok(());
            }
            remaining
        }
        None => directions,
    };

    match ty {
        DataType::Map(_) => Ok(()),
        DataType::Struct(strct)
            if SerdeContainerAttrs::from_attributes(&strct.attributes)?
                .is_some_and(|attrs| attrs.transparent) =>
        {
            let mut live_fields = Vec::new();
            match &strct.fields {
                Fields::Unit => {}
                Fields::Unnamed(unnamed) => {
                    for field in &unnamed.fields {
                        let field_directions = directions.and(FlattenDirections::for_field(field)?);
                        if !field_directions.serialize && !field_directions.deserialize {
                            continue;
                        }
                        let Some(ty) = &field.ty else {
                            return Err(Error::invalid_internally_tagged_enum(
                                path,
                                variant_name,
                                "a payload hidden only from Specta cannot be merged with an internal tag",
                            ));
                        };
                        live_fields.push((ty, field_directions));
                    }
                }
                Fields::Named(named) => {
                    for (_, field) in &named.fields {
                        let field_directions = directions.and(FlattenDirections::for_field(field)?);
                        if !field_directions.serialize && !field_directions.deserialize {
                            continue;
                        }
                        let Some(ty) = &field.ty else {
                            return Err(Error::invalid_internally_tagged_enum(
                                path,
                                variant_name,
                                "a payload hidden only from Specta cannot be merged with an internal tag",
                            ));
                        };
                        live_fields.push((ty, field_directions));
                    }
                }
            }

            match live_fields.as_slice() {
                [(ty, field_directions)] => validate_internally_tag_enum_datatype(
                    ty,
                    types,
                    path,
                    variant_name,
                    seen,
                    env,
                    reject_contextual_generic_argument,
                    *field_directions,
                ),
                [] => Ok(()),
                _ => Err(Error::invalid_internally_tagged_enum(
                    path,
                    variant_name,
                    "payload cannot be merged with an internal tag",
                )),
            }
        }
        DataType::Struct(strct) => match &strct.fields {
            Fields::Unit => Ok(()),
            Fields::Named(named) => {
                for (_, field) in &named.fields {
                    let field_directions = directions.and(FlattenDirections::for_field(field)?);
                    if field.ty.is_none()
                        && (field_directions.serialize || field_directions.deserialize)
                    {
                        return Err(Error::invalid_internally_tagged_enum(
                            path,
                            variant_name,
                            "a payload hidden only from Specta cannot be merged with an internal tag",
                        ));
                    }
                }
                Ok(())
            }
            Fields::Unnamed(unnamed) if unnamed.fields.len() == 1 => {
                let field = &unnamed.fields[0];
                let field_directions = directions.and(FlattenDirections::for_field(field)?);
                match field.ty.as_ref() {
                    Some(ty) => validate_internally_tag_enum_datatype(
                        ty,
                        types,
                        path,
                        variant_name,
                        seen,
                        env,
                        reject_contextual_generic_argument,
                        field_directions,
                    ),
                    None if !field_directions.serialize && !field_directions.deserialize => Ok(()),
                    None => Err(Error::invalid_internally_tagged_enum(
                        path,
                        variant_name,
                        "a payload hidden only from Specta cannot be merged with an internal tag",
                    )),
                }
            }
            Fields::Unnamed(_) => Err(Error::invalid_internally_tagged_enum(
                path,
                variant_name,
                "payload cannot be merged with an internal tag",
            )),
        },
        DataType::Reference(Reference::Named(reference)) => {
            let key = resolved_reference_key(reference, env);
            if !seen.insert(key.clone()) {
                return Ok(());
            }

            let path = inner_reference_path(path, reference, types);
            let result = match &reference.inner {
                NamedReferenceType::Inline { dt, .. } => {
                    let reject_contextual_generic_argument =
                        reject_contextual_generic_argument && datatype_changes_under_env(dt, env);
                    validate_internally_tag_enum_datatype(
                        dt,
                        types,
                        &path,
                        variant_name,
                        seen,
                        env,
                        reject_contextual_generic_argument,
                        directions,
                    )
                }
                NamedReferenceType::Reference { generics, .. } => {
                    let reject_contextual_generic_argument = reject_contextual_generic_argument
                        && generics
                            .iter()
                            .any(|(_, argument)| datatype_changes_under_env(argument, env));
                    types
                        .get(reference)
                        .and_then(|ndt| ndt.ty.as_ref())
                        .map_or(Ok(()), |ty| {
                            validate_internally_tag_enum_datatype(
                                ty,
                                types,
                                &path,
                                variant_name,
                                seen,
                                Some(&GenericEnv {
                                    map: generics,
                                    parent: env,
                                }),
                                reject_contextual_generic_argument,
                                directions,
                            )
                        })
                }
                NamedReferenceType::Recursive(_) => Ok(()),
            };
            seen.remove(&key);

            result
        }
        DataType::Enum(enm) => match EnumRepr::from_attrs(&enm.attributes)? {
            EnumRepr::Untagged => {
                validate_internally_tag_enum(enm, types, path.to_string(), seen, env, directions)
            }
            EnumRepr::External | EnumRepr::Internal { .. } | EnumRepr::Adjacent { .. } => Ok(()),
        },
        DataType::Tuple(tuple) if tuple.elements.is_empty() => Ok(()),
        DataType::Generic(generic) => {
            match env.and_then(|env| env.map.iter().find(|(param, _)| param == generic)) {
                Some((_, argument)) => {
                    let requires_contextual_rewrite = (directions.serialize
                        && crate::internal_tag_payload_requires_contextual_rewrite_for_mode(
                            argument,
                            types,
                            PhaseRewrite::Serialize,
                        )?)
                        || (directions.deserialize
                            && crate::internal_tag_payload_requires_contextual_rewrite_for_mode(
                                argument,
                                types,
                                PhaseRewrite::Deserialize,
                            )?);
                    if reject_contextual_generic_argument && requires_contextual_rewrite {
                        return Err(Error::invalid_internally_tagged_enum(
                            path,
                            variant_name,
                            "a concrete generic payload requires context-sensitive enum encoding; use a non-generic wrapper variant",
                        ));
                    }

                    validate_internally_tag_enum_datatype(
                        argument,
                        types,
                        path,
                        variant_name,
                        seen,
                        env.and_then(|env| env.parent),
                        reject_contextual_generic_argument,
                        directions,
                    )
                }
                None => Ok(()),
            }
        }
        DataType::Reference(Reference::Opaque(_))
        | DataType::Intersection(_)
        | DataType::Tuple(_)
        | DataType::Primitive(_)
        | DataType::List(_)
        | DataType::Nullable(_) => Err(Error::invalid_internally_tagged_enum(
            path,
            variant_name,
            "payload cannot be merged with an internal tag",
        )),
    }
}

#[allow(clippy::too_many_arguments)]
fn validate_internally_tag_conversion_wires(
    serialize_wire: Option<&DataType>,
    deserialize_wire: Option<&DataType>,
    types: &Types,
    path: &str,
    variant_name: &str,
    seen: &mut HashSet<Reference>,
    env: Option<&GenericEnv<'_>>,
    reject_contextual_generic_argument: bool,
    directions: FlattenDirections,
) -> Result<FlattenDirections, Error> {
    if directions.serialize
        && let Some(wire) = serialize_wire
    {
        validate_internally_tag_enum_datatype(
            wire,
            types,
            path,
            variant_name,
            seen,
            env,
            reject_contextual_generic_argument,
            FlattenDirections::SERIALIZE_ONLY,
        )?;
    }
    if directions.deserialize
        && let Some(wire) = deserialize_wire
    {
        validate_internally_tag_enum_datatype(
            wire,
            types,
            path,
            variant_name,
            seen,
            env,
            reject_contextual_generic_argument,
            FlattenDirections::DESERIALIZE_ONLY,
        )?;
    }

    Ok(FlattenDirections {
        serialize: directions.serialize && serialize_wire.is_none(),
        deserialize: directions.deserialize && deserialize_wire.is_none(),
    })
}

fn datatype_changes_under_env(ty: &DataType, env: Option<&GenericEnv<'_>>) -> bool {
    let mut resolved = ty.clone();
    let mut budget = RESOLVED_KEY_NODE_BUDGET;
    resolve_generics_for_key(&mut resolved, env, &mut budget) && resolved != *ty
}

/// Extends `path` with the name of the type behind a named reference, so an
/// error surfaced while validating what that reference resolves to (for
/// example an untagged enum nested inside an internally tagged variant's
/// payload) names the actual inner type instead of repeating the outer
/// container's path, which would misattribute the error (e.g. blaming a
/// variant that doesn't exist on the outer enum).
fn inner_reference_path(path: &str, reference: &NamedReference, types: &Types) -> String {
    match types.get(reference) {
        Some(ndt) => format!("{path} -> {}", ndt.name),
        None => path.to_string(),
    }
}

fn named_reference_ty<'a>(reference: &'a NamedReference, types: &'a Types) -> Option<&'a DataType> {
    match &reference.inner {
        NamedReferenceType::Inline { dt, .. } => Some(dt),
        NamedReferenceType::Reference { .. } => types.get(reference)?.ty.as_ref(),
        NamedReferenceType::Recursive(_) => None,
    }
}