specta-typescript 0.0.15

Export your Rust types to TypeScript
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
//! Runtime-aware TypeScript type remapping.
//!
//! Semantic types are Rust types whose TypeScript runtime value should be more
//! specific than their JSON-compatible wire representation.
//!
//! This enables the following default rules:
//!  - [`bytes::Bytes`](https://docs.rs/bytes/latest/bytes/struct.Bytes.html) to become [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
//!  - [`bytes::BytesMut`](https://docs.rs/bytes/latest/bytes/struct.BytesMut.html) to become [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
//!  - [`url::Url`](https://docs.rs/url/latest/url/struct.Url.html) to become [`Url`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL)
//!  - [`chrono::DateTime`](https://docs.rs/chrono/latest/chrono/struct.DateTime.html) to become [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
//!  - [`chrono::NaiveDate`](https://docs.rs/chrono/latest/chrono/struct.NaiveDate.html) to become [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
//!  - [`jiff::Timestamp`](https://docs.rs/jiff/latest/jiff/struct.Timestamp.html) to become [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
//!  - [`jiff::civil::Date`](https://docs.rs/jiff/latest/jiff/civil/struct.Date.html) to become [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
//!
//! This could also allow you to map your own Rust types into custom JavaScript types like custom classes. Refer to [`semantic::Configuration::define`](Configuration::define) for building your own rules.
//!
//! This is intended the be implemented by frameworks like [Tauri Specta](https://github.com/specta-rs/tauri-specta), [TauRPC](https://github.com/MatsDK/TauRPC) and [rspc](https://github.com/specta-rs/rspc) as they have control of the runtime and type layer.
//!
//! <div class="warning">
//!
//! **WARNING:** The current implementation relies on the frontend and backend being versioned in-step. This works for a Tauri desktop application but may become an issue for a HTTP API unless you have something like [Skew Protection](https://vercel.com/docs/skew-protection).
//!
//! We will likely lift this as a hard restriction in the future!
//!
//! </div>
//!
//! <details>
//! <summary>Implementing into your own framework</summary>
//!
//! # Implementing into your own framework
//!
//! A framework needs to be integrated properly for this feature to work, as it requires both type-level and runtime JS to make it work properly.
//!
//! I would highly recommend reading [specta-rs/specta#203](https://github.com/specta-rs/specta/issues/203) and understanding it as it's the core work which inspired this feature.
//!
//! Documentation coming soon... For now refer to [specta-rs/tauri-specta#219](https://github.com/specta-rs/tauri-specta/pull/219) which was the original implementation into [Tauri Specta](https://github.com/specta-rs/tauri-specta).
//!
//! </details>
//!

use std::{borrow::Cow, collections::BTreeSet, fmt, sync::Arc};

use specta::{
    Type, Types,
    datatype::{
        DataType, Enum, Field, Fields, NamedDataType, NamedReferenceType, Primitive, Reference,
        Variant,
    },
};

use crate::{
    Error, define,
    primitives::{escape_typescript_string_literal, is_identifier},
};

/// A JavaScript expression that converts between a semantic
/// TypeScript runtime value and its JSON-compatible representation.
///
/// The closure receives the JavaScript identifier/expression being transformed
/// and must return a JavaScript expression using that value.
///
/// # Examples
///
/// Convert a JSON string into a TypeScript `Date`:
///
/// ```rust
/// use specta_typescript::semantic::Transform;
///
/// let transform = Transform::new(|value| format!("new Date({value})"));
/// # let _ = transform;
/// ```
///
/// Convert a `Uint8Array` into a JSON array of numbers:
///
/// ```rust
/// use specta_typescript::semantic::Transform;
///
/// let transform = Transform::new(|value| format!("[...{value}]"));
/// # let _ = transform;
/// ```
///
/// Use [`Transform::identity`] when the TypeScript runtime value already has
/// the same representation as the value crossing the wire or when JSON.stringify/JSON.parse
/// is already able to handle the transformation for you.
#[derive(Clone)]
#[non_exhaustive]
pub struct Transform(
    /// The runtime transform function
    ///
    /// This is called with the argument being a Typescript identifier.
    /// It should output some transformation on the identifier.
    /// Eg. `|i| format!("new Date({i})")` could be one valid implementation.
    Option<Arc<dyn Fn(&str) -> String + Send + Sync>>,
);

impl fmt::Debug for Transform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            Some(r) => write!(f, "{r:p}"),
            None => write!(f, "<none>"),
        }
    }
}

impl Transform {
    /// Construct a runtime transform from a JavaScript identifier mapper.
    ///
    /// The mapper should return a JavaScript expression, not a statement.
    ///
    /// ```rust
    /// use specta_typescript::semantic::Transform;
    ///
    /// let transform = Transform::new(|ident| format!("new URL({ident})"));
    /// # let _ = transform;
    /// ```
    pub fn new(runtime: impl Fn(&str) -> String + Send + Sync + 'static) -> Self {
        Self(Some(Arc::new(runtime)))
    }

    /// Construct an identity runtime transform.
    ///
    /// This is useful when a rule only changes the exported TypeScript type, or
    /// when one direction does not need runtime conversion.
    ///
    /// ```rust
    /// use specta_typescript::semantic::Transform;
    ///
    /// let transform = Transform::identity();
    /// # let _ = transform;
    /// ```
    pub fn identity() -> Self {
        Self(None)
    }

    fn apply(&self, ident: &str) -> String {
        match &self.0 {
            Some(runtime) => runtime(ident),
            None => ident.to_owned(),
        }
    }
}

#[derive(Clone)]
pub(crate) struct DataTypeFn(Arc<dyn Fn(DataType) -> DataType + Send + Sync>);

impl DataTypeFn {
    pub(crate) fn new(f: impl Fn(DataType) -> DataType + Send + Sync + 'static) -> Self {
        Self(Arc::new(f))
    }
}

impl fmt::Debug for DataTypeFn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("DataTypeFn")
            .field(&format!("{:p}", self.0))
            .finish()
    }
}

/// A rule for a specific named data type.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Rule {
    /// Matched against [`NamedDataType::name`](specta::datatype::NamedDataType::name) to determine if rule should apply
    pub name: Cow<'static, str>,
    /// Matched against [`NamedDataType::module_path`](specta::datatype::NamedDataType::module_path) to determine if rule should apply
    pub module_path: Cow<'static, str>,
    /// The type transformation function
    ///
    /// This must match up with the type produced or consumed by the runtime.
    pub(crate) data_type: DataTypeFn,
    /// The type transformation for serializing.
    /// This is JS -> Rust
    pub(crate) serialize: Option<Transform>,
    /// The type transformation for deserializing.
    /// This is Rust -> JS
    pub(crate) deserialize: Option<Transform>,
}

/// Configuration for runtime-aware TypeScript type remapping.
///
/// By default this contains a set of default rules as defined on [the module](crate::semantic). If you don't want them use [`Configuration::empty()`](Configuration::empty) instead.
///
/// You can add your own rules via [`Configuration::define(...)`](Configuration::define).
///
#[derive(Debug, Clone)]
pub struct Configuration {
    rules: Vec<Rule>,
    lossless_bigint: bool,
    lossless_floats: bool,
}

impl Default for Configuration {
    fn default() -> Self {
        Self {
            rules: vec![
                // Uint8Array
                Rule {
                    name: "Bytes".into(),
                    module_path: "bytes".into(),
                    data_type: DataTypeFn::new(|_| define("Uint8Array").into()),
                    serialize: Some(Transform::new(|i| format!("[...{i}]"))),
                    deserialize: Some(Transform::new(|i| format!("new Uint8Array({i})"))),
                },
                Rule {
                    name: "BytesMut".into(),
                    module_path: "bytes".into(),
                    data_type: DataTypeFn::new(|_| define("Uint8Array").into()),
                    serialize: Some(Transform::new(|i| format!("[...{i}]"))),
                    deserialize: Some(Transform::new(|i| format!("new Uint8Array({i})"))),
                },
                // URL
                Rule {
                    name: "Url".into(),
                    module_path: "url".into(),
                    data_type: DataTypeFn::new(|_| define("URL").into()),
                    serialize: None,
                    deserialize: Some(Transform::new(|i| format!("new URL({i})"))),
                },
                // Date
                Rule {
                    name: "DateTime".into(),
                    module_path: "chrono".into(),
                    data_type: DataTypeFn::new(|_| define("Date").into()),
                    serialize: None,
                    deserialize: Some(Transform::new(|i| format!("new Date({i})"))),
                },
                Rule {
                    name: "NaiveDate".into(),
                    module_path: "chrono".into(),
                    data_type: DataTypeFn::new(|_| define("Date").into()),
                    serialize: Some(Transform::new(|i| {
                        format!("{i}.toISOString().slice(0, 10)")
                    })),
                    deserialize: Some(Transform::new(|i| format!("new Date({i})"))),
                },
                Rule {
                    name: "Timestamp".into(),
                    module_path: "jiff".into(),
                    data_type: DataTypeFn::new(|_| define("Date").into()),
                    serialize: Some(Transform::new(|i| format!("{i}.toISOString()"))),
                    deserialize: Some(Transform::new(|i| format!("new Date({i})"))),
                },
                Rule {
                    name: "Date".into(),
                    module_path: "jiff::civil".into(),
                    data_type: DataTypeFn::new(|_| define("Date").into()),
                    serialize: Some(Transform::new(|i| {
                        format!("{i}.toISOString().slice(0, 10)")
                    })),
                    deserialize: Some(Transform::new(|i| format!("new Date({i})"))),
                },
                // We don't implement support for `chrono::NaiveDateTime`, and many `jiff` types as lack of timezone is an issue with JS's `Date`
            ],
            lossless_bigint: false,
            lossless_floats: false,
        }
    }
}

impl Configuration {
    /// Construct a [`Configuration`] without the default rules.
    ///
    /// Prefer [`Configuration::default`] when possible; the default
    /// rules cover common ecosystem types and may grow over time.
    pub fn empty() -> Self {
        Self {
            rules: Default::default(),
            lossless_bigint: false,
            lossless_floats: false,
        }
    }

    /// Exposes the rules applies to this instance for manual manipulation.
    ///
    /// This could be used to filter the default rules if you want to exclude certain ones.
    pub fn rules_mut(&mut self) -> &mut Vec<Rule> {
        &mut self.rules
    }

    /// Define a new rule for a given type `T`.
    ///
    /// `dt` receives the original [`DataType`] for `T` and must return the
    /// TypeScript-facing [`DataType`] that should replace it. `serialize`
    /// transforms TypeScript runtime values before sending them to Rust.
    /// `deserialize` transforms values received from Rust into TypeScript
    /// runtime values.
    ///
    /// This only works for named types, such as types generated by the
    /// [`Type`] derive macro. It does not work for primitives.
    ///
    /// ```rust
    /// use specta::Type;
    /// use specta_typescript::{define, semantic::{Configuration, Transform}};
    ///
    /// #[derive(Type)]
    /// struct MyCustomUrl(String);
    ///
    /// let mut semantic_types = Configuration::empty();
    /// semantic_types.define::<MyCustomUrl>(
    ///     |_| define("URL").into(), // Runtime Specta Type
    ///     Some(Transform::new(|value| format!("{value}.toString()"))), // JS -> JSON
    ///     Some(Transform::new(|value| format!("new URL({value})"))), // JSON -> JS
    /// );
    /// ```
    pub fn define<T: Type>(
        mut self,
        dt: impl Fn(DataType) -> DataType + Send + Sync + 'static,
        serialize: Option<Transform>,
        deserialize: Option<Transform>,
    ) -> Self {
        let mut types = Types::default();
        let ndt = match T::definition(&mut types) {
            DataType::Reference(Reference::Named(r)) => types.get(&r),
            _ => None,
        };
        if let Some(ndt) = ndt {
            self.rules.push(Rule {
                name: ndt.name.clone(),
                module_path: ndt.module_path.clone(),
                data_type: DataTypeFn(Arc::new(dt)),
                serialize,
                deserialize,
            });
        }

        self
    }

    /// Enable lossless support for large integer types (`BigInt`s).
    ///
    /// This remaps `usize`, `isize`, `u64`, `i64`, `u128`, and `i128` so they
    /// are a [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) in JavaScript.
    ///
    /// This is only safe if your serialization and deserialization layer can losslessly transmit `BigInt`s to the frontend.
    ///
    /// Refer to [specta-rs/specta#203](https://github.com/specta-rs/specta/issues/203) for implementation details.
    pub fn enable_lossless_bigints(mut self) -> Self {
        if !self.lossless_bigint {
            self.lossless_bigint = true;
        }

        self
    }

    /// Enable lossless float support.
    ///
    /// By enabling this, you assert that your runtime *must* preserve `NaN`,
    /// `Infinity`, and `-Infinity` values from JavaScript to Rust and we will flatten `number | null` into `number`.
    ///
    /// Refer to [specta-rs/specta#203](https://github.com/specta-rs/specta/issues/203) for implementation details.
    pub fn enable_lossless_floats(mut self) -> Self {
        if !self.lossless_floats {
            self.lossless_floats = true;
        }

        self
    }

    /// Transform a [`Types`] collection using the configured rules.
    ///
    /// This rewrites registered named types so their exported TypeScript shapes
    /// match the values produced or consumed by the runtime transforms.
    ///
    /// Call this after any format-specific mapping that changes the type graph,
    /// and before exporting the final TypeScript definitions.
    pub fn apply_types<'a>(&self, types: &'a Types) -> Cow<'a, Types> {
        let mut types = Cow::Borrowed(types);

        if self.has_builtin_remaps() {
            types = Cow::Owned(types.into_owned().map(|mut ndt| {
                let remap_bigint = if ndt.name.ends_with("_Serialize") {
                    serialize_bigint
                } else {
                    deserialize_bigint
                };

                ndt.generics.to_mut().iter_mut().for_each(|generic| {
                    if let Some(dt) = &mut generic.default {
                        apply_builtin_remaps(
                            dt,
                            remap_bigint,
                            self.lossless_bigint,
                            self.lossless_floats,
                        );
                    }
                });
                if let Some(dt) = &mut ndt.ty {
                    apply_builtin_remaps(
                        dt,
                        remap_bigint,
                        self.lossless_bigint,
                        self.lossless_floats,
                    );
                }

                ndt
            }));
        }

        if !self.rules.is_empty() {
            let source = types.into_owned();
            let lookup = source.clone();
            types = Cow::Owned(source.map(|mut ndt| {
                if let Some(dt) = &mut ndt.ty {
                    self.apply_rules_to_dt(&lookup, dt);
                }

                if let Some(rule) = self
                    .rules
                    .iter()
                    .find(|r| r.name == ndt.name && r.module_path == ndt.module_path)
                    && let Some(dt) = ndt.ty.take()
                {
                    ndt.ty = Some((rule.data_type.0)(dt));
                }

                ndt
            }));
        }

        types
    }

    fn apply_rules_to_dt(&self, types: &Types, dt: &mut DataType) {
        if let DataType::Reference(Reference::Named(reference)) = dt
            && let Some(rule) = self.rule_for_reference(types, reference)
        {
            *dt = (rule.data_type.0)(Self::reference_source_dt(types, reference));
            return;
        }

        match dt {
            DataType::Primitive(_) | DataType::Generic(_) => {}
            DataType::List(list) => self.apply_rules_to_dt(types, &mut list.ty),
            DataType::Map(map) => {
                self.apply_rules_to_dt(types, map.key_ty_mut());
                self.apply_rules_to_dt(types, map.value_ty_mut());
            }
            DataType::Struct(s) => self.apply_rules_to_fields(types, &mut s.fields),
            DataType::Enum(e) => {
                for (_, variant) in &mut e.variants {
                    self.apply_rules_to_fields(types, &mut variant.fields);
                }
            }
            DataType::Tuple(tuple) => {
                for dt in &mut tuple.elements {
                    self.apply_rules_to_dt(types, dt);
                }
            }
            DataType::Nullable(dt) => self.apply_rules_to_dt(types, dt),
            DataType::Intersection(dts) => {
                for dt in dts {
                    self.apply_rules_to_dt(types, dt);
                }
            }
            DataType::Reference(Reference::Named(reference)) => match &mut reference.inner {
                NamedReferenceType::Recursive(_) | NamedReferenceType::Reference { .. } => {}
                NamedReferenceType::Inline { dt, .. } => self.apply_rules_to_dt(types, dt),
            },
            DataType::Reference(Reference::Opaque(_)) => {}
        }
    }

    fn apply_rules_to_fields(&self, types: &Types, fields: &mut Fields) {
        match fields {
            Fields::Unit => {}
            Fields::Unnamed(fields) => {
                for field in &mut fields.fields {
                    if let Some(dt) = &mut field.ty {
                        self.apply_rules_to_dt(types, dt);
                    }
                }
            }
            Fields::Named(fields) => {
                for (_, field) in &mut fields.fields {
                    if let Some(dt) = &mut field.ty {
                        self.apply_rules_to_dt(types, dt);
                    }
                }
            }
        }
    }

    /// Scan a [`DataType`] tree applying serialize-facing rules.
    ///
    /// This assumes [`Configuration::apply_types`] has already been applied to the [`Types`].
    /// Therefore the type updates will be shallow (up until references to the `Types`).
    ///
    /// The returned JavaScript expression is built around `js_ident` and may be
    /// deeply nested for structs, tuples, lists, nullable values, and
    /// intersections.
    ///
    /// If a registered graph contains no runtime transform, `None` is returned;
    /// its type-only changes are handled by [`Configuration::apply_types`]. For
    /// inline inputs, a type-only result is retained with an identity runtime
    /// expression because there is no registered type for `apply_types` to rewrite.
    ///
    /// This method is non-panicking. A construct that cannot be transformed is
    /// skipped: it contributes no runtime change, and enclosing structs/lists
    /// still transform their other parts. When the *root* cannot be transformed,
    /// `None` is returned. Use [`Configuration::try_apply_serialize`] to find out
    /// why a transform was skipped.
    pub fn apply_serialize(
        &self,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Option<(Option<DataType>, String)> {
        self.apply(
            serialize_bigint,
            |rule| &rule.serialize,
            types,
            dt,
            js_ident,
        )
    }

    /// Fallible counterpart to [`Configuration::apply_serialize`].
    ///
    /// Returns an [`Error`] when a non-identity transform is required beneath a
    /// construct whose wire shape cannot be discriminated without guessing (for
    /// example overlapping `#[serde(untagged)]` variants, or a recursive enum
    /// whose expansion would leave nested values untransformed). Emitting a
    /// transform there would let the exported type and the runtime value
    /// diverge, so the unsupported part is skipped and reported instead.
    pub fn try_apply_serialize(
        &self,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Result<Option<(Option<DataType>, String)>, Error> {
        self.try_apply(
            serialize_bigint,
            |rule| &rule.serialize,
            types,
            dt,
            js_ident,
        )
    }

    /// Scan a [`DataType`] tree applying deserialize-facing rules.
    ///
    /// Use this for values received from Rust before exposing them to
    /// TypeScript callers.
    ///
    /// This method is non-panicking. See [`Configuration::apply_serialize`] for
    /// the skipping semantics, and [`Configuration::try_apply_deserialize`] to
    /// find out why a transform was skipped.
    pub fn apply_deserialize(
        &self,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Option<(Option<DataType>, String)> {
        self.apply(
            deserialize_bigint,
            |rule| &rule.deserialize,
            types,
            dt,
            js_ident,
        )
    }

    /// Fallible counterpart to [`Configuration::apply_deserialize`].
    ///
    /// See [`Configuration::try_apply_serialize`] for the rejection semantics.
    pub fn try_apply_deserialize(
        &self,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Result<Option<(Option<DataType>, String)>, Error> {
        self.try_apply(
            deserialize_bigint,
            |rule| &rule.deserialize,
            types,
            dt,
            js_ident,
        )
    }

    /// Run a traversal and keep the diagnostic if a part could not be
    /// transformed. The infallible API drops the diagnostic and keeps whatever
    /// could be generated.
    fn run(
        &self,
        remap_bigint: fn() -> DataType,
        transform_for_rule: fn(&Rule) -> &Option<Transform>,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> (Option<(Option<DataType>, String)>, Option<Error>) {
        let mut walk = Walk {
            transform_for_rule,
            remap_bigint,
            types,
            stack: Vec::new(),
            error: None,
            recursion_cut: false,
            current: None,
        };
        let result = self.apply_inner(&mut walk, dt, js_ident);
        (prune_identity_root(types, dt, js_ident, result), walk.error)
    }

    fn apply(
        &self,
        remap_bigint: fn() -> DataType,
        transform_for_rule: fn(&Rule) -> &Option<Transform>,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Option<(Option<DataType>, String)> {
        self.run(remap_bigint, transform_for_rule, types, dt, js_ident)
            .0
    }

    fn try_apply(
        &self,
        remap_bigint: fn() -> DataType,
        transform_for_rule: fn(&Rule) -> &Option<Transform>,
        types: &Types,
        dt: &DataType,
        js_ident: &str,
    ) -> Result<Option<(Option<DataType>, String)>, Error> {
        let (result, error) = self.run(remap_bigint, transform_for_rule, types, dt, js_ident);
        match error {
            Some(error) => Err(error),
            None => Ok(result),
        }
    }

    fn apply_inner(
        &self,
        walk: &mut Walk<'_>,
        dt: &DataType,
        js_ident: &str,
    ) -> Option<(Option<DataType>, String)> {
        let result = match dt {
            DataType::Reference(Reference::Named(r)) => {
                if let Some(rule) = self.rule_for_reference(walk.types, r) {
                    return Some((
                        Some((rule.data_type.0)(Self::reference_source_dt(walk.types, r))),
                        (walk.transform_for_rule)(rule).as_ref().map_or_else(
                            || js_ident.to_owned(),
                            |transform| transform.apply(js_ident),
                        ),
                    ));
                }

                match &r.inner {
                    NamedReferenceType::Inline { dt, .. } => self.apply_inner(walk, dt, js_ident),
                    // An inline cycle was cut short during definition. Treat it
                    // like the cycle-stack cutoff so an enclosing enum reports
                    // it rather than emitting a silently partial transform.
                    NamedReferenceType::Recursive(_) => {
                        walk.recursion_cut = true;
                        None
                    }
                    NamedReferenceType::Reference { .. } => {
                        let ndt = walk.types.get(r)?;

                        let ty = ndt.ty.as_ref()?;
                        let key = (ndt.name.clone(), ndt.module_path.clone());
                        if walk.stack.contains(&key) {
                            walk.recursion_cut = true;
                            return None;
                        }
                        walk.stack.push(key);
                        let previous = walk.current.replace(ndt);
                        let result = self
                            .apply_inner(walk, ty, js_ident)
                            .map(|(_, runtime)| (None, runtime));
                        walk.current = previous;
                        walk.stack.pop();
                        result
                    }
                }
            }
            DataType::Struct(s) => {
                self.apply_fields(walk, &s.fields, js_ident)
                    .map(|(fields, runtime)| {
                        let ty = fields.map(|fields| {
                            let mut next = s.clone();
                            next.fields = fields;
                            DataType::Struct(next)
                        });
                        (ty, runtime)
                    })
            }
            DataType::Tuple(tuple) => {
                let mut ty = tuple.clone();
                let mut changed = false;
                let parts = tuple
                    .elements
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, element)| {
                        let ident = format!("{js_ident}[{idx}]");
                        let (next_ty, runtime) = self.apply_inner(walk, element, &ident)?;
                        if let Some(next_ty) = next_ty {
                            ty.elements[idx] = next_ty;
                            changed = true;
                        }
                        (runtime != ident).then_some((idx, runtime))
                    })
                    .collect::<Vec<_>>();

                if parts.is_empty() {
                    changed.then_some((Some(DataType::Tuple(ty)), js_ident.to_owned()))
                } else {
                    Some((
                        changed.then_some(DataType::Tuple(ty)),
                        array_transform(js_ident, tuple.elements.len(), parts),
                    ))
                }
            }
            DataType::Map(map) => {
                let item = "v";
                let (next_ty, runtime) = self.apply_inner(walk, map.value_ty(), item)?;

                let mut ty = map.clone();
                let mut changed = false;
                if let Some(next_ty) = next_ty {
                    ty.set_value_ty(next_ty);
                    changed = true;
                }

                Some((
                    changed.then_some(DataType::Map(ty)),
                    if runtime == item {
                        js_ident.to_owned()
                    } else {
                        format!(
                            "Object.fromEntries(Object.entries({js_ident}).map(([k,{item}])=>[k,{runtime}]))"
                        )
                    },
                ))
            }
            DataType::List(list) => {
                let item = "i";
                let (next_ty, runtime) = self.apply_inner(walk, &list.ty, item)?;
                let mut ty = list.clone();
                let mut changed = false;
                if let Some(next_ty) = next_ty {
                    ty.ty = Box::new(next_ty);
                    changed = true;
                }
                Some((
                    changed.then_some(DataType::List(ty)),
                    if runtime == item {
                        js_ident.to_owned()
                    } else {
                        format!("{js_ident}.map({item}=>{runtime})")
                    },
                ))
            }
            DataType::Nullable(inner) => {
                let (next_ty, runtime) = self.apply_inner(walk, inner, js_ident)?;
                Some((
                    next_ty.map(|dt| DataType::Nullable(Box::new(dt))),
                    if runtime == js_ident {
                        js_ident.to_owned()
                    } else {
                        format!("{js_ident}==null?{js_ident}:{runtime}")
                    },
                ))
            }
            DataType::Intersection(items) => {
                let mut ty = items.clone();
                let mut changed = false;
                let mut parts = items
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, item)| {
                        let (next_ty, runtime) = self.apply_inner(walk, item, js_ident)?;
                        if let Some(next_ty) = next_ty {
                            ty[idx] = next_ty;
                            changed = true;
                        }
                        Some(runtime)
                    })
                    .collect::<Vec<_>>();

                parts.dedup();

                match parts.as_slice() {
                    [] => None,
                    [runtime] => Some((
                        changed.then_some(DataType::Intersection(ty)),
                        runtime.clone(),
                    )),
                    _ => Some((
                        changed.then_some(DataType::Intersection(ty)),
                        spread_transform(
                            "",
                            parts.into_iter().map(|p| format!("...{p}")).collect(),
                        ),
                    )),
                }
            }
            DataType::Enum(e) => self.apply_enum(walk, e, js_ident),
            DataType::Primitive(_)
            | DataType::Generic(_)
            | DataType::Reference(Reference::Opaque(_)) => None,
        };

        self.apply_builtin_remaps(walk.remap_bigint, dt, js_ident, result)
    }

    /// Apply the configured rules to one variant/struct [`Fields`] layout.
    ///
    /// Returns `None` when the layout needs neither a type nor a runtime
    /// change, and otherwise the (possibly unchanged) fields plus the runtime
    /// expression. This is shared by structs and enum variant payloads so the
    /// two stay in agreement.
    fn apply_fields(
        &self,
        walk: &mut Walk<'_>,
        fields: &Fields,
        js_ident: &str,
    ) -> Option<(Option<Fields>, String)> {
        match fields {
            Fields::Unit => None,
            Fields::Named(named) => {
                let mut ty = named.clone();
                let mut changed = false;
                let mut parts = Vec::new();

                for (name, field) in &named.fields {
                    let Some(field_ty) = &field.ty else { continue };
                    let field_ident = js_property_access(js_ident, name);
                    let Some((next_ty, runtime)) = self.apply_inner(walk, field_ty, &field_ident)
                    else {
                        continue;
                    };

                    if let Some(next_ty) = next_ty
                        && let Some((_, field)) = ty.fields.iter_mut().find(|(n, _)| n == name)
                    {
                        field.ty = Some(next_ty);
                        changed = true;
                    }
                    if runtime != field_ident {
                        parts.push(format!("{}:{runtime}", js_object_key(name)));
                    }
                }

                if parts.is_empty() {
                    changed.then_some((Some(Fields::Named(ty)), js_ident.to_owned()))
                } else {
                    Some((
                        changed.then_some(Fields::Named(ty)),
                        spread_transform(js_ident, parts),
                    ))
                }
            }
            Fields::Unnamed(unnamed) => {
                let mut ty = unnamed.clone();
                let mut changed = false;
                let is_newtype = unnamed.fields.len() == 1;
                let parts = unnamed
                    .fields
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, field)| {
                        let field_ty = field.ty.as_ref()?;
                        let field_ident = if is_newtype {
                            js_ident.to_owned()
                        } else {
                            format!("{js_ident}[{idx}]")
                        };
                        let (next_ty, runtime) = self.apply_inner(walk, field_ty, &field_ident)?;

                        if let Some(next_ty) = next_ty {
                            ty.fields[idx].ty = Some(next_ty);
                            changed = true;
                        }

                        (runtime != field_ident).then_some((idx, runtime))
                    })
                    .collect::<Vec<_>>();

                if parts.is_empty() {
                    changed.then_some((Some(Fields::Unnamed(ty)), js_ident.to_owned()))
                } else {
                    Some((
                        changed.then_some(Fields::Unnamed(ty)),
                        if is_newtype {
                            parts.into_iter().next()?.1
                        } else {
                            array_transform(js_ident, unnamed.fields.len(), parts)
                        },
                    ))
                }
            }
        }
    }

    /// Generate a representation-aware runtime transform for an enum.
    ///
    /// The wire shape is the one produced by the format layer (for example
    /// `specta_serde`): external variants are keyed by their serialized name,
    /// internally/adjacently tagged variants carry a discriminator field, and
    /// untagged variants carry no discriminant (and are told apart by their
    /// payload shape). Each active variant's payload is transformed with the
    /// same [`Fields`] traversal structs use, then the branches are dispatched
    /// on the wire shape so the runtime value matches the remapped type.
    ///
    /// When a non-identity transform would be required beneath a shape that
    /// cannot be discriminated without guessing (or beneath a recursive enum,
    /// whose single-expression expansion would be partial), this records an
    /// explicit [`Error`] and returns `None` rather than emitting an expression
    /// that would let the exported type and the runtime value diverge.
    fn apply_enum(
        &self,
        walk: &mut Walk<'_>,
        e: &Enum,
        js_ident: &str,
    ) -> Option<(Option<DataType>, String)> {
        let active = e
            .variants
            .iter()
            .filter(|(_, variant)| !variant.skip)
            .collect::<Vec<_>>();
        if active.is_empty() {
            return None;
        }

        let cut_before = walk.recursion_cut;

        let mut branches = Vec::with_capacity(active.len());
        let mut changed = false;
        let mut needs_transform = false;
        for (_, variant) in &active {
            let (fields, runtime) = match self.apply_fields(walk, &variant.fields, js_ident) {
                Some((fields, runtime)) => (fields, runtime),
                None => (None, js_ident.to_owned()),
            };
            changed |= fields.is_some();
            needs_transform |= runtime != js_ident;
            branches.push((fields, runtime));
        }

        // A recursive expansion is cut short by the cycle stack, so the single
        // expression we could emit would leave the nested values untransformed
        // while the exported type claims otherwise. Report it instead.
        if walk.recursion_cut && !cut_before {
            self.record_unsupported(walk, RECURSIVE_ENUM_REASON);
            return None;
        }

        let build = |branches: &mut Vec<(Option<Fields>, String)>| -> Option<DataType> {
            if !changed {
                return None;
            }
            let mut next = e.clone();
            for ((_, variant), (fields, _)) in next
                .variants
                .iter_mut()
                .filter(|(_, variant)| !variant.skip)
                .zip(branches.iter_mut())
            {
                if let Some(fields) = fields.take() {
                    variant.fields = fields;
                }
            }
            Some(DataType::Enum(next))
        };

        if !needs_transform {
            return build(&mut branches).map(|ty| (Some(ty), js_ident.to_owned()));
        }

        // A lone variant is never ambiguous: every value belongs to it.
        if branches.len() == 1 {
            let next_ty = build(&mut branches);
            let (_, runtime) = branches.pop()?;
            return Some((next_ty, runtime));
        }

        let needs = branches
            .iter()
            .map(|(_, runtime)| runtime != js_ident)
            .collect::<Vec<_>>();
        let Some(guards) = enum_branch_guards(walk.types, &active, &needs, js_ident) else {
            self.record_unsupported(walk, UNSUPPORTED_ENUM_REASON);
            return None;
        };

        let has_else = guards
            .iter()
            .any(|guard| matches!(guard, BranchGuard::Else));
        let mut parts = Vec::new();
        let mut fallback = None;
        for ((_, runtime), guard) in branches.iter().zip(&guards) {
            match guard {
                BranchGuard::Match(condition) => {
                    if has_else || runtime != js_ident {
                        parts.push(format!("{condition}?{runtime}"));
                    }
                }
                BranchGuard::Else => fallback = Some(runtime.clone()),
            }
        }

        let default = fallback.unwrap_or_else(|| js_ident.to_owned());
        let runtime = if parts.is_empty() {
            default
        } else {
            format!("{}:{}", parts.join(":"), default)
        };

        let next_ty = build(&mut branches);
        Some((next_ty, runtime))
    }

    /// Record the first unsupported construct, attaching the named type
    /// currently being expanded so the diagnostic can name it.
    fn record_unsupported(&self, walk: &mut Walk<'_>, reason: &'static str) {
        if walk.error.is_some() {
            return;
        }

        let mut error = Error::unsupported_semantic_transform(reason);
        if let Some(ndt) = walk.current {
            error = error.with_named_datatype(ndt);
        }
        walk.error = Some(error);
    }

    fn rule_for_reference<'a>(
        &'a self,
        types: &'a Types,
        reference: &specta::datatype::NamedReference,
    ) -> Option<&'a Rule> {
        let ndt = types.get(reference)?;
        self.rules
            .iter()
            .find(|rule| rule.name == ndt.name && rule.module_path == ndt.module_path)
    }

    fn reference_source_dt(
        types: &Types,
        reference: &specta::datatype::NamedReference,
    ) -> DataType {
        match &reference.inner {
            NamedReferenceType::Inline { dt, .. } => (**dt).clone(),
            NamedReferenceType::Reference { .. } | NamedReferenceType::Recursive(_) => types
                .get(reference)
                .and_then(|ndt| ndt.ty.clone())
                .unwrap_or_else(|| DataType::Reference(Reference::Named(reference.clone()))),
        }
    }

    fn has_builtin_remaps(&self) -> bool {
        self.lossless_bigint || self.lossless_floats
    }

    fn apply_builtin_remaps(
        &self,
        remap_bigint: fn() -> DataType,
        dt: &DataType,
        js_ident: &str,
        result: Option<(Option<DataType>, String)>,
    ) -> Option<(Option<DataType>, String)> {
        if !self.has_builtin_remaps() {
            return result;
        }

        let source = result
            .as_ref()
            .and_then(|(dt, _)| dt.clone())
            .unwrap_or_else(|| dt.clone());
        let mut remapped = source.clone();
        apply_builtin_remaps(
            &mut remapped,
            remap_bigint,
            self.lossless_bigint,
            self.lossless_floats,
        );

        let runtime = result
            .as_ref()
            .map(|(_, runtime)| runtime.as_str())
            .unwrap_or(js_ident);
        let runtime = if is_lossless_bigint_primitive(&source)
            && self.lossless_bigint
            && remap_bigint() == deserialize_bigint()
        {
            format!("BigInt({runtime})")
        } else {
            runtime.to_owned()
        };

        if remapped == source {
            result
        } else {
            Some((Some(remapped), runtime))
        }
    }
}

fn prune_identity_root(
    types: &Types,
    dt: &DataType,
    js_ident: &str,
    result: Option<(Option<DataType>, String)>,
) -> Option<(Option<DataType>, String)> {
    let registered_root = matches!(
        dt,
        DataType::Reference(Reference::Named(reference))
            if !matches!(reference.inner, NamedReferenceType::Inline { .. })
                && types.get(reference).is_some_and(|ndt| ndt.ty.is_some())
    );

    result.filter(|(next_ty, runtime)| {
        runtime != js_ident
            || next_ty
                .as_ref()
                .is_some_and(|next_ty| !registered_root || registered_generics_changed(dt, next_ty))
    })
}

fn registered_generics_changed(dt: &DataType, next_ty: &DataType) -> bool {
    let (
        DataType::Reference(Reference::Named(source)),
        DataType::Reference(Reference::Named(remapped)),
    ) = (dt, next_ty)
    else {
        return false;
    };
    let (
        NamedReferenceType::Reference {
            generics: source, ..
        },
        NamedReferenceType::Reference {
            generics: remapped, ..
        },
    ) = (&source.inner, &remapped.inner)
    else {
        return false;
    };

    source != remapped
}

fn is_lossless_bigint_primitive(dt: &DataType) -> bool {
    matches!(
        dt,
        DataType::Primitive(
            Primitive::usize
                | Primitive::isize
                | Primitive::u64
                | Primitive::i64
                | Primitive::u128
                | Primitive::i128
        )
    )
}

fn apply_builtin_remaps(
    dt: &mut DataType,
    remap_bigint: fn() -> DataType,
    lossless_bigint: bool,
    lossless_floats: bool,
) {
    if let DataType::Primitive(primitive) = dt
        && let Some(remapped) = remap_primitive(
            primitive.clone(),
            remap_bigint,
            lossless_bigint,
            lossless_floats,
        )
    {
        *dt = remapped;
        return;
    }

    match dt {
        DataType::Primitive(_) | DataType::Generic(_) => {}
        DataType::List(list) => {
            apply_builtin_remaps(&mut list.ty, remap_bigint, lossless_bigint, lossless_floats)
        }
        DataType::Map(map) => {
            // Built-in lossless remaps (`BigInt`, `Number`) must not rewrite a
            // map key, including through generic/inline wrappers: keys are
            // JSON object property names, which the serializer stringifies.
            // `primitives::map_key_datatype` renders the key shape instead.
            apply_builtin_remaps(
                map.value_ty_mut(),
                remap_bigint,
                lossless_bigint,
                lossless_floats,
            );
        }
        DataType::Struct(s) => apply_builtin_remaps_to_fields(
            &mut s.fields,
            remap_bigint,
            lossless_bigint,
            lossless_floats,
        ),
        DataType::Enum(e) => {
            for (_, variant) in &mut e.variants {
                apply_builtin_remaps_to_fields(
                    &mut variant.fields,
                    remap_bigint,
                    lossless_bigint,
                    lossless_floats,
                );
            }
        }
        DataType::Tuple(tuple) => {
            for dt in &mut tuple.elements {
                apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
            }
        }
        DataType::Nullable(dt) => {
            apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
        }
        DataType::Intersection(dts) => {
            for dt in dts {
                apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
            }
        }
        DataType::Reference(reference) => {
            let Reference::Named(reference) = reference else {
                return;
            };

            match &mut reference.inner {
                NamedReferenceType::Recursive(_) => {}
                NamedReferenceType::Inline { dt, .. } => {
                    apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
                }
                NamedReferenceType::Reference { generics, .. } => {
                    for (_, dt) in generics {
                        apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
                    }
                }
            }
        }
    }
}

fn apply_builtin_remaps_to_fields(
    fields: &mut Fields,
    remap_bigint: fn() -> DataType,
    lossless_bigint: bool,
    lossless_floats: bool,
) {
    match fields {
        Fields::Unit => {}
        Fields::Unnamed(fields) => {
            for field in &mut fields.fields {
                if let Some(dt) = &mut field.ty {
                    apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
                }
            }
        }
        Fields::Named(fields) => {
            for (_, field) in &mut fields.fields {
                if let Some(dt) = &mut field.ty {
                    apply_builtin_remaps(dt, remap_bigint, lossless_bigint, lossless_floats);
                }
            }
        }
    }
}

fn remap_primitive(
    primitive: Primitive,
    remap_bigint: fn() -> DataType,
    lossless_bigint: bool,
    lossless_floats: bool,
) -> Option<DataType> {
    if lossless_bigint
        && matches!(
            primitive,
            Primitive::usize
                | Primitive::isize
                | Primitive::u64
                | Primitive::i64
                | Primitive::u128
                | Primitive::i128
        )
    {
        return Some(remap_bigint());
    }

    if lossless_floats && matches!(primitive, Primitive::f16 | Primitive::f32 | Primitive::f64) {
        return Some(Reference::opaque(crate::opaque::Number).into());
    }

    None
}

fn serialize_bigint() -> DataType {
    crate::define("bigint | number").into()
}

fn deserialize_bigint() -> DataType {
    Reference::opaque(crate::opaque::BigInt).into()
}

fn spread_transform(js_ident: &str, mut parts: Vec<String>) -> String {
    if !js_ident.is_empty() {
        parts.insert(0, format!("...{js_ident}"));
    }
    format!("({{{}}})", parts.join(","))
}

fn array_transform(js_ident: &str, len: usize, parts: Vec<(usize, String)>) -> String {
    let mut items = (0..len)
        .map(|idx| format!("{js_ident}[{idx}]"))
        .collect::<Vec<_>>();

    for (idx, runtime) in parts {
        items[idx] = runtime;
    }

    format!("([{}])", items.join(","))
}

/// Render a property read of `name` on `base`.
///
/// `__proto__` needs an own-property-safe read: both `base.__proto__` and
/// `base["__proto__"]` resolve to the inherited `Object.prototype` accessor
/// when no own property exists, so a transform body would run against the
/// prototype object (`new URL(Object.prototype)`). The descriptor form yields
/// the own value when present and `undefined` otherwise, and stays a valid base
/// expression for nested reads (`?.[]` short-circuits the whole chain).
///
/// Regression coverage for https://github.com/specta-rs/specta/issues/611.
fn js_property_access(base: &str, name: &str) -> String {
    if name == "__proto__" {
        format!(
            "Object.getOwnPropertyDescriptor({base},{})?.value",
            js_string_literal(name)
        )
    } else if is_identifier(name) {
        format!("{base}.{name}")
    } else {
        format!("{base}[\"{}\"]", escape_typescript_string_literal(name))
    }
}

/// Render a wire key as an object-literal property key for a transform body.
///
/// `__proto__` must be a *computed* key. In an object literal, `__proto__:`
/// triggers the legacy prototype setter instead of defining an own property, so
/// `{...value, __proto__: new URL(value.__proto__)}` would silently keep the
/// wire value. Quoting the key does not help (`"__proto__":` is the same
/// special case); only `["__proto__"]:` defines an own property.
///
/// Regression coverage for https://github.com/specta-rs/specta/issues/611.
fn js_object_key(name: &str) -> Cow<'_, str> {
    if name == "__proto__" {
        Cow::Owned(format!("[{}]", js_string_literal(name)))
    } else if is_identifier(name) {
        Cow::Borrowed(name)
    } else {
        Cow::Owned(format!("\"{}\"", escape_typescript_string_literal(name)))
    }
}

fn js_string_literal(value: &str) -> String {
    format!("\"{}\"", escape_typescript_string_literal(value))
}

/// Mutable state shared across one `apply_serialize` / `apply_deserialize` call.
struct Walk<'a> {
    transform_for_rule: fn(&Rule) -> &Option<Transform>,
    remap_bigint: fn() -> DataType,
    types: &'a Types,
    /// Named types currently being expanded, used to cut recursive cycles.
    stack: Vec<(Cow<'static, str>, Cow<'static, str>)>,
    /// The first unsupported construct encountered, if any.
    error: Option<Error>,
    /// Set when a recursive reference was cut short by [`Walk::stack`].
    recursion_cut: bool,
    /// The named type currently being expanded, used to name errors.
    current: Option<&'a NamedDataType>,
}

const UNSUPPORTED_ENUM_REASON: &str = "the enum's wire shape cannot be discriminated without guessing (for example overlapping `#[serde(untagged)]` variants); use a tagged representation or apply the rule to a non-ambiguous position";
const RECURSIVE_ENUM_REASON: &str = "the enum is recursive, so a single expression cannot transform nested values without the exported type claiming more than the runtime value provides";

/// How a single enum branch is selected at runtime.
enum BranchGuard {
    /// A JavaScript expression that is truthy exactly for this variant.
    Match(String),
    /// The variant is the unguarded fallback: either a remapped untagged
    /// variant whose runtime shape may differ from its wire shape, or (for
    /// tagged enums) nothing.
    Else,
}

/// Work out how to select each active variant from its wire value.
///
/// Prefers a shared discriminator field (internally/adjacently tagged enums),
/// then the external representation's per-variant key or string literal, then
/// structural discrimination of untagged variants. Returns `None` when no
/// unambiguous discrimination exists.
fn enum_branch_guards(
    types: &Types,
    active: &[&(Cow<'static, str>, Variant)],
    needs: &[bool],
    js_ident: &str,
) -> Option<Vec<BranchGuard>> {
    tag_branch_guards(active, js_ident)
        .or_else(|| external_branch_guards(active, js_ident))
        .or_else(|| untagged_branch_guards(types, active, needs, js_ident))
}

/// Guards for a tagged enum: a field present in every variant with distinct
/// string-literal values (`#[serde(tag = ...)]`, optionally with `content`).
fn tag_branch_guards(
    active: &[&(Cow<'static, str>, Variant)],
    js_ident: &str,
) -> Option<Vec<BranchGuard>> {
    let candidates = active
        .iter()
        .map(|(_, variant)| variant_tag_candidates(variant))
        .collect::<Vec<_>>();
    let (key, literals) = common_tag_key(&candidates)?;

    Some(
        literals
            .iter()
            .map(|literal| {
                BranchGuard::Match(format!(
                    "{js_ident}!=null&&{}==={}",
                    js_property_access(js_ident, &key),
                    js_string_literal(literal),
                ))
            })
            .collect(),
    )
}

/// Guards for the external representation: a non-unit variant is keyed by its
/// serialized name, while a unit (or collapsed) variant is the bare string.
/// The discriminators must be distinct so overlapping shapes are rejected
/// rather than mis-dispatched.
///
/// The keyed guard uses an own-property check rather than `value.name !==
/// undefined`: the latter is also true for members inherited from
/// `Object.prototype` (a variant named `constructor`, `valueOf`, `toString`,
/// … would otherwise be selected for any object). The `=== literal` guard for
/// unit variants compares against a specific string, so it is unaffected.
fn external_branch_guards(
    active: &[&(Cow<'static, str>, Variant)],
    js_ident: &str,
) -> Option<Vec<BranchGuard>> {
    let mut discriminators = BTreeSet::new();
    let mut guards = Vec::with_capacity(active.len());

    for (_, variant) in active {
        let (discriminator, guard) = match &variant.fields {
            Fields::Named(named) if named.fields.len() == 1 => {
                let name = named.fields[0].0.to_string();
                let guard = format!(
                    "{js_ident}!=null&&Object.prototype.hasOwnProperty.call({js_ident},{})",
                    js_string_literal(&name),
                );
                (name, guard)
            }
            Fields::Unnamed(unnamed) if unnamed.fields.len() == 1 => {
                let literal = string_literal_of(unnamed.fields[0].ty.as_ref()?)?;
                let guard = format!("{js_ident}==={}", js_string_literal(&literal));
                (literal, guard)
            }
            _ => return None,
        };

        if !discriminators.insert(discriminator) {
            return None;
        }
        guards.push(BranchGuard::Match(guard));
    }

    Some(guards)
}

/// Guards for untagged enums, which have no discriminant and must be told
/// apart by their payload shape.
///
/// Every variant must occupy a distinct wire shape (`null`, `boolean`,
/// `number`, `string`, `Array.isArray`, object). At most one variant may have a
/// non-identity runtime transform, because a remapped payload can change its
/// runtime shape; that variant becomes the unguarded fallback. Overlapping
/// shapes are rejected rather than guessed.
fn untagged_branch_guards(
    types: &Types,
    active: &[&(Cow<'static, str>, Variant)],
    needs: &[bool],
    js_ident: &str,
) -> Option<Vec<BranchGuard>> {
    let mut shapes = Vec::with_capacity(active.len());
    let mut fallback = None;

    for (idx, (_, variant)) in active.iter().enumerate() {
        let shape = untagged_payload_shape(types, variant)?;
        if needs[idx] && fallback.replace(idx).is_some() {
            return None;
        }
        shapes.push(shape);
    }

    let mut seen = BTreeSet::new();
    for shape in &shapes {
        if !seen.insert(*shape) {
            return None;
        }
    }

    Some(
        shapes
            .iter()
            .enumerate()
            .map(|(idx, shape)| {
                if Some(idx) == fallback {
                    BranchGuard::Else
                } else {
                    BranchGuard::Match(shape_guard(*shape, js_ident))
                }
            })
            .collect(),
    )
}

/// The wire shape of an untagged variant's payload.
fn untagged_payload_shape(types: &Types, variant: &Variant) -> Option<UntaggedShape> {
    match &variant.fields {
        Fields::Unit => Some(UntaggedShape::Null),
        Fields::Named(_) => Some(UntaggedShape::Object),
        Fields::Unnamed(unnamed) => {
            let mut live = unnamed.fields.iter().filter_map(|field| field.ty.as_ref());
            let first = live.next()?;
            if live.next().is_some() {
                return Some(UntaggedShape::Array);
            }
            wire_shape(types, first, WIRE_SHAPE_DEPTH)
        }
    }
}

/// The JavaScript value shape a wire datatype occupies, resolving newtype
/// wrappers and named references to the underlying wire type. Returns `None`
/// for shapes that cannot be classified (for example nullable payloads).
fn wire_shape(types: &Types, ty: &DataType, depth: usize) -> Option<UntaggedShape> {
    if depth == 0 {
        return None;
    }

    match ty {
        DataType::Primitive(primitive) => Some(primitive_shape(primitive)),
        DataType::Tuple(tuple) if tuple.elements.is_empty() => Some(UntaggedShape::Null),
        DataType::List(_) => Some(UntaggedShape::Array),
        DataType::Map(_) => Some(UntaggedShape::Object),
        DataType::Struct(s) => match &s.fields {
            Fields::Named(_) => Some(UntaggedShape::Object),
            Fields::Unit => Some(UntaggedShape::Null),
            Fields::Unnamed(unnamed) => {
                let mut live = unnamed.fields.iter().filter_map(|field| field.ty.as_ref());
                let first = live.next()?;
                if live.next().is_some() {
                    Some(UntaggedShape::Array)
                } else {
                    wire_shape(types, first, depth - 1)
                }
            }
        },
        DataType::Reference(Reference::Named(r)) => {
            let source = Configuration::reference_source_dt(types, r);
            wire_shape(types, &source, depth - 1)
        }
        _ => None,
    }
}

const WIRE_SHAPE_DEPTH: usize = 32;

fn primitive_shape(primitive: &Primitive) -> UntaggedShape {
    match primitive {
        Primitive::bool => UntaggedShape::Boolean,
        Primitive::str | Primitive::char => UntaggedShape::String,
        Primitive::i8
        | Primitive::i16
        | Primitive::i32
        | Primitive::i64
        | Primitive::i128
        | Primitive::isize
        | Primitive::u8
        | Primitive::u16
        | Primitive::u32
        | Primitive::u64
        | Primitive::u128
        | Primitive::usize
        | Primitive::f16
        | Primitive::f32
        | Primitive::f64
        | Primitive::f128 => UntaggedShape::Number,
    }
}

/// A JavaScript value shape used to tell untagged variants apart.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum UntaggedShape {
    Null,
    Boolean,
    Number,
    String,
    Array,
    Object,
}

fn shape_guard(shape: UntaggedShape, js_ident: &str) -> String {
    match shape {
        UntaggedShape::Null => format!("{js_ident}===null"),
        UntaggedShape::Boolean => format!("typeof {js_ident}===\"boolean\""),
        UntaggedShape::Number => format!("typeof {js_ident}===\"number\""),
        UntaggedShape::String => format!("typeof {js_ident}===\"string\""),
        UntaggedShape::Array => format!("Array.isArray({js_ident})"),
        UntaggedShape::Object => {
            format!("{js_ident}!=null&&typeof {js_ident}===\"object\"&&!Array.isArray({js_ident})")
        }
    }
}

/// The literal value of a synthetic single-variant string enum, if `ty` is one.
fn string_literal_of(ty: &DataType) -> Option<String> {
    let DataType::Enum(e) = ty else {
        return None;
    };

    let mut variants = e.variants.iter();
    let (name, variant) = variants.next()?;
    if variants.next().is_some() || !matches!(variant.fields, Fields::Unit) {
        return None;
    }

    Some(name.to_string())
}

/// Every `(key, literal)` discriminator candidate directly on a variant's wire
/// shape.
fn variant_tag_candidates(variant: &Variant) -> Vec<(String, String)> {
    let mut candidates = Vec::new();
    match &variant.fields {
        Fields::Named(named) => collect_tag_candidates(&named.fields, &mut candidates),
        Fields::Unnamed(unnamed) => {
            for field in &unnamed.fields {
                if let Some(ty) = &field.ty {
                    collect_datatype_tag_candidates(ty, &mut candidates);
                }
            }
        }
        Fields::Unit => {}
    }
    candidates
}

fn collect_tag_candidates(
    fields: &[(Cow<'static, str>, Field)],
    candidates: &mut Vec<(String, String)>,
) {
    for (name, field) in fields {
        if field.optional {
            continue;
        }
        let Some(ty) = &field.ty else { continue };
        if let Some(literal) = string_literal_of(ty) {
            candidates.push((name.to_string(), literal));
        }
    }
}

/// Descend the intersection/struct wrappers an internally tagged tuple variant
/// is rewritten into so its tag field is visible.
fn collect_datatype_tag_candidates(ty: &DataType, candidates: &mut Vec<(String, String)>) {
    match ty {
        DataType::Intersection(parts) => {
            for part in parts {
                collect_datatype_tag_candidates(part, candidates);
            }
        }
        DataType::Struct(s) => {
            if let Fields::Named(named) = &s.fields {
                collect_tag_candidates(&named.fields, candidates);
            }
        }
        _ => {}
    }
}

/// The key shared by every variant with distinct literal values, if any, plus
/// the literal for each variant in order.
fn common_tag_key(candidates: &[Vec<(String, String)>]) -> Option<(String, Vec<String>)> {
    let first = candidates.first()?;

    'keys: for (key, _) in first {
        let mut literals = Vec::with_capacity(candidates.len());
        let mut seen = BTreeSet::new();
        for variant in candidates {
            let matching = variant
                .iter()
                .filter(|(candidate, _)| candidate == key)
                .collect::<Vec<_>>();
            let [(_, literal)] = matching.as_slice() else {
                continue 'keys;
            };
            if !seen.insert(literal.clone()) {
                continue 'keys;
            }
            literals.push(literal.clone());
        }

        return Some((key.clone(), literals));
    }

    None
}