rkyv-js-codegen 0.2.0

JavaScript/TypeScript code generator for rkyv types
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
//! Rust source extraction: parses files with `syn` and adds every type
//! marked with a recognized derive (default `rkyv::Archive`) to the [`CodeGenerator`].
//!
//! ## Marker detection
//!
//! A derive path marks a type for extraction iff:
//!
//! - it is the exact multi-segment marker path (`rkyv::Archive`, leading `::` allowed), or
//! - it is a single-segment ident resolving to a marker path through the file's `use` imports (including renames), or
//! - it is a bare ident and a glob import (`use rkyv::*`) brings a marker path into scope, or
//! - it matches a path registered via [`add_marker_path`](CodeGenerator::add_marker_path).
//!
//! ## Use-item analysis
//!
//! `use` trees are flattened into a local-name → fully-qualified-path map:
//!
//! - `use std::collections::BTreeMap` maps `BTreeMap` to `std::collections::BTreeMap`
//! - `use rkyv::Archive as Rkyv` maps `Rkyv` to `rkyv::Archive`
//! - `type HashMap<K, V> = std::collections::HashMap<K, V, S>` maps `HashMap` to `std::collections::HashMap` 
//!   (the alias *path* only; extra RHS arguments surface as trailing type arguments at the use site)
//!
//! ## Remote proxies
//!
//! A type with `#[rkyv(remote = T)]` is a serialization proxy: it emits no top-level export.
//! Instead, the proxy itself is auto-registered as a with-wrapper whose template is the proxy's own codec expression,
//! so fields annotated `#[rkyv(with = ProxyDef)]` resolve to it (rkyv 0.8 semantics).

use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use quote::ToTokens;
use syn::spanned::Spanned;
use syn::{
    Attribute, Fields, GenericArgument, PathArguments, Type, TypeArray, TypePath, TypeTuple,
    UseTree,
};
use walkdir::WalkDir;

use crate::error::{Diagnostic, DiagnosticKind, Error, SourceLocation};
use crate::expr::{CodecExpr, codec};
use crate::generator::{CodeGenerator, EnumVariant, OnUnknown, TypeKind};
use crate::registry::WithWrapper;

/// Per-file context built from `use` items and type aliases.
struct SourceContext {
    /// Maps local name → fully-qualified path.
    imports: HashMap<String, String>,
    /// Glob import prefixes (`use rkyv::*` → `"rkyv"`).
    globs: Vec<String>,
    /// The file being parsed, if known.
    file: Option<PathBuf>,
}

impl SourceContext {
    fn location(&self, span: proc_macro2::Span) -> SourceLocation {
        let start = span.start();
        SourceLocation {
            file: self.file.clone(),
            line: start.line,
            column: start.column + 1,
        }
    }
}

/// Recursively flatten a `UseTree` into import entries and glob prefixes.
fn collect_imports(
    tree: &UseTree,
    prefix: &[String],
    imports: &mut HashMap<String, String>,
    globs: &mut Vec<String>,
) {
    match tree {
        UseTree::Path(p) => {
            let mut new_prefix = prefix.to_vec();
            new_prefix.push(p.ident.to_string());
            collect_imports(&p.tree, &new_prefix, imports, globs);
        }
        UseTree::Name(n) => {
            let name = n.ident.to_string();
            let full_path = make_full_path(prefix, &name);
            imports.insert(name, full_path);
        }
        UseTree::Rename(r) => {
            let canonical = r.ident.to_string();
            let alias = r.rename.to_string();
            let full_path = make_full_path(prefix, &canonical);
            imports.insert(alias, full_path);
        }
        UseTree::Glob(_) => {
            if !prefix.is_empty() {
                globs.push(prefix.join("::"));
            }
        }
        UseTree::Group(g) => {
            for item in &g.items {
                collect_imports(item, prefix, imports, globs);
            }
        }
    }
}

fn make_full_path(prefix: &[String], name: &str) -> String {
    if prefix.is_empty() {
        name.to_string()
    } else {
        format!("{}::{}", prefix.join("::"), name)
    }
}

fn path_segments(path: &syn::Path) -> Vec<String> {
    path.segments.iter().map(|s| s.ident.to_string()).collect()
}

/// Build a `SourceContext` from all `use` items and type aliases in a file.
fn build_source_context(file: &syn::File, source_file: Option<PathBuf>) -> SourceContext {
    let mut imports = HashMap::new();
    let mut globs = Vec::new();

    for item in &file.items {
        match item {
            syn::Item::Use(item_use) => {
                collect_imports(&item_use.tree, &[], &mut imports, &mut globs);
            }
            // `type Foo<..> = some::path::Bar<..>` maps `Foo` to
            // `some::path::Bar`. Only the path is resolved; generic
            // parameters on either side are handled at the use site.
            syn::Item::Type(item_type) => {
                if let Type::Path(TypePath { path, .. }) = &*item_type.ty
                    && path.segments.len() > 1
                {
                    imports.insert(
                        item_type.ident.to_string(),
                        path_segments(path).join("::"),
                    );
                }
            }
            _ => {}
        }
    }

    SourceContext {
        imports,
        globs,
        file: source_file,
    }
}

/// Type-level `#[rkyv(...)]` attributes the extractor understands.
#[derive(Default)]
struct RkyvTypeAttrs {
    /// `#[rkyv(remote = T)]`
    remote: Option<syn::Type>,
    /// `#[rkyv(archived = Name)]`
    archived: Option<String>,
}

/// Consume the rest of an unrecognized nested meta so parsing can continue.
fn skip_nested_meta_value(meta: &syn::meta::ParseNestedMeta) -> syn::Result<()> {
    if meta.input.peek(syn::Token![=]) {
        let _eq: syn::Token![=] = meta.input.parse()?;
        while !meta.input.is_empty() && !meta.input.peek(syn::Token![,]) {
            let _tt: proc_macro2::TokenTree = meta.input.parse()?;
        }
    } else if meta.input.peek(syn::token::Paren) {
        let content;
        syn::parenthesized!(content in meta.input);
        let _rest: proc_macro2::TokenStream = content.parse()?;
    }
    Ok(())
}

fn parse_rkyv_type_attrs(attrs: &[Attribute]) -> RkyvTypeAttrs {
    let mut parsed = RkyvTypeAttrs::default();
    for attr in attrs {
        if !attr.path().is_ident("rkyv") {
            continue;
        }
        let _ = attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("remote") {
                parsed.remote = Some(meta.value()?.parse()?);
            } else if meta.path.is_ident("archived") {
                let path: syn::Path = meta.value()?.parse()?;
                if let Some(last) = path.segments.last() {
                    parsed.archived = Some(last.ident.to_string());
                }
            } else {
                skip_nested_meta_value(&meta)?;
            }
            Ok(())
        });
    }
    parsed
}

/// The `W` of a field-level `#[rkyv(with = W)]`, if present.
fn parse_rkyv_field_with(attrs: &[Attribute]) -> Option<syn::Type> {
    let mut with: Option<syn::Type> = None;
    for attr in attrs {
        if !attr.path().is_ident("rkyv") {
            continue;
        }
        let _ = attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("with") {
                if with.is_none() {
                    with = Some(meta.value()?.parse()?);
                } else {
                    skip_nested_meta_value(&meta)?;
                }
            } else {
                skip_nested_meta_value(&meta)?;
            }
            Ok(())
        });
    }
    with
}

/// Check whether one of the derive paths marks the type for extraction.
fn has_marker_derive(attrs: &[Attribute], ctx: &SourceContext, codegen: &CodeGenerator) -> bool {
    let markers = &codegen.marker_paths;
    for attr in attrs {
        if !attr.path().is_ident("derive") {
            continue;
        }
        let Ok(nested) = attr.parse_args_with(
            syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated,
        ) else {
            continue;
        };
        for path in nested {
            let segments = path_segments(&path);
            if segments.len() == 1 {
                let ident = &segments[0];
                // A user marker registered as a bare name.
                if markers.contains(ident) {
                    return true;
                }
                // Resolve through imports (incl. renames).
                if ctx.imports.get(ident).is_some_and(|fq| markers.contains(fq)) {
                    return true;
                }
                // Resolve through glob imports.
                if ctx
                    .globs
                    .iter()
                    .any(|glob| markers.contains(&format!("{glob}::{ident}")))
                {
                    return true;
                }
            } else {
                let joined = segments.join("::");
                if markers.contains(&joined) {
                    return true;
                }
            }
        }
    }
    false
}

fn type_to_string(ty: &Type) -> String {
    ty.to_token_stream().to_string()
}

/// Convert a syn type to a codec expression.
///
/// Errors carry only the [`DiagnosticKind`]; the calling field attaches
/// `referenced_by` and location provenance.
fn type_to_expr(
    ty: &Type,
    codegen: &CodeGenerator,
    ctx: &SourceContext,
) -> Result<CodecExpr, DiagnosticKind> {
    match ty {
        Type::Path(TypePath { qself: None, path }) => {
            let segment = path.segments.last().expect("type paths are non-empty");
            let raw_ident = segment.ident.to_string();

            // Multi-segment paths are already fully qualified; single-segment
            // idents resolve through the file's imports.
            let full_path = if path.segments.len() > 1 {
                path_segments(path).join("::")
            } else {
                ctx.imports
                    .get(&raw_ident)
                    .cloned()
                    .unwrap_or_else(|| raw_ident.clone())
            };

            match full_path.as_str() {
                "u8" => Ok(codec::u8()),
                "i8" => Ok(codec::i8()),
                "u16" => Ok(codec::u16()),
                "i16" => Ok(codec::i16()),
                "u32" => Ok(codec::u32()),
                "i32" => Ok(codec::i32()),
                "u64" => Ok(codec::u64()),
                "i64" => Ok(codec::i64()),
                "f32" => Ok(codec::f32()),
                "f64" => Ok(codec::f64()),
                "bool" => Ok(codec::bool_()),
                "char" => Ok(codec::char_()),
                "String" | "std::string::String" => Ok(codec::string()),
                "Vec" | "std::vec::Vec" => {
                    let inner = single_generic_arg(segment, ty)?;
                    Ok(codec::vec(type_to_expr(inner, codegen, ctx)?))
                }
                "Option" | "std::option::Option" => {
                    let inner = single_generic_arg(segment, ty)?;
                    Ok(codec::option(type_to_expr(inner, codegen, ctx)?))
                }
                "Box" | "std::boxed::Box" => {
                    let inner = single_generic_arg(segment, ty)?;
                    Ok(codec::boxed(type_to_expr(inner, codegen, ctx)?))
                }
                _ => {
                    if let Some(external) = codegen.registry.get_type(&full_path) {
                        let raw_args = collect_type_args(segment);
                        // Trailing arguments (hashers, allocators) are discarded,
                        // so never try to resolve them to codecs.
                        let keep = if external.allows_trailing() && raw_args.len() > external.arity()
                        {
                            external.arity()
                        } else {
                            raw_args.len()
                        };
                        let args = raw_args[..keep]
                            .iter()
                            .map(|arg| type_to_expr(arg, codegen, ctx))
                            .collect::<Result<Vec<_>, _>>()?;
                        external.instantiate(args).map_err(|kind| match kind {
                            DiagnosticKind::GenericArity {
                                expected, found, ..
                            } => DiagnosticKind::GenericArity {
                                rust_path: full_path.clone(),
                                expected,
                                found,
                            },
                            other => other,
                        })
                    } else if path.segments.len() == 1 && full_path == raw_ident {
                        // A bare local ident: a reference to another generated type,
                        // validated at generate time.
                        Ok(codec::named(raw_ident))
                    } else {
                        Err(DiagnosticKind::UnknownType {
                            suggestion: codegen.registry.suggest_type(&full_path),
                            rust_path: full_path,
                        })
                    }
                }
            }
        }
        Type::Array(TypeArray { elem, len, .. }) => {
            let elem_expr = type_to_expr(elem, codegen, ctx)?;
            if let syn::Expr::Lit(syn::ExprLit {
                lit: syn::Lit::Int(lit_int),
                ..
            }) = len
                && let Ok(len_val) = lit_int.base10_parse::<u64>()
            {
                Ok(codec::array(elem_expr, len_val))
            } else {
                Err(DiagnosticKind::UnsupportedFieldType {
                    rust_type: type_to_string(ty),
                })
            }
        }
        Type::Tuple(TypeTuple { elems, .. }) => {
            let elem_exprs = elems
                .iter()
                .map(|elem| type_to_expr(elem, codegen, ctx))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(codec::tuple(elem_exprs))
        }
        Type::Reference(reference) => {
            if let Type::Path(TypePath { path, .. }) = &*reference.elem
                && path.is_ident("str")
            {
                return Ok(codec::string());
            }
            type_to_expr(&reference.elem, codegen, ctx)
        }
        Type::Paren(paren) => type_to_expr(&paren.elem, codegen, ctx),
        Type::Group(group) => type_to_expr(&group.elem, codegen, ctx),
        other => Err(DiagnosticKind::UnsupportedFieldType {
            rust_type: type_to_string(other),
        }),
    }
}

fn single_generic_arg<'a>(
    segment: &'a syn::PathSegment,
    whole: &Type,
) -> Result<&'a Type, DiagnosticKind> {
    if let PathArguments::AngleBracketed(args) = &segment.arguments
        && let Some(GenericArgument::Type(ty)) = args.args.first()
    {
        return Ok(ty);
    }
    Err(DiagnosticKind::UnsupportedFieldType {
        rust_type: type_to_string(whole),
    })
}

/// Collect the type arguments of a path segment.
///
/// - `[T; N]` array arguments are unwrapped to `T` (SmallVec/TinyVec-style parameters).
/// - Lifetimes and const generics are skipped.
fn collect_type_args(segment: &syn::PathSegment) -> Vec<&Type> {
    let PathArguments::AngleBracketed(args) = &segment.arguments else {
        return vec![];
    };

    let mut type_args = Vec::new();
    for arg in &args.args {
        if let GenericArgument::Type(ty) = arg {
            if let Type::Array(TypeArray { elem, .. }) = ty {
                type_args.push(elem.as_ref());
            } else {
                type_args.push(ty);
            }
        }
    }
    type_args
}

/// Resolve the `W` of `#[rkyv(with = W)]` to a registry lookup key.
fn resolve_wrapper_path(ty: &syn::Type, ctx: &SourceContext) -> Option<String> {
    let Type::Path(TypePath { qself: None, path }) = ty else {
        return None;
    };
    let segments = path_segments(path);
    if segments.len() == 1 {
        Some(
            ctx.imports
                .get(&segments[0])
                .cloned()
                .unwrap_or_else(|| segments[0].clone()),
        )
    } else {
        Some(segments.join("::"))
    }
}

/// Resolve a field to its codec expression.
///
/// `Ok(None)` means the field is omitted (a `Skip` wrapper).
fn field_expr(
    field: &syn::Field,
    context: &str,
    codegen: &CodeGenerator,
    ctx: &SourceContext,
) -> Result<Option<CodecExpr>, Diagnostic> {
    if let Some(with_type) = parse_rkyv_field_with(&field.attrs) {
        let location = ctx.location(with_type.span());
        let resolved = resolve_wrapper_path(&with_type, ctx);
        let wrapper = resolved
            .as_deref()
            .and_then(|path| lookup_wrapper(codegen, ctx, path));
        let Some(wrapper) = wrapper else {
            return Err(Diagnostic::new(DiagnosticKind::UnknownWithWrapper {
                wrapper_path: resolved.unwrap_or_else(|| type_to_string(&with_type)),
            })
            .referenced_by(context)
            .at(Some(location)));
        };
        let underlying = if wrapper.needs_underlying() {
            let expr = type_to_expr(&field.ty, codegen, ctx).map_err(|kind| {
                Diagnostic::new(kind)
                    .referenced_by(context)
                    .at(Some(ctx.location(field.ty.span())))
            })?;
            Some(expr)
        } else {
            None
        };
        return Ok(wrapper.apply(underlying));
    }

    type_to_expr(&field.ty, codegen, ctx)
        .map(Some)
        .map_err(|kind| {
            Diagnostic::new(kind)
                .referenced_by(context)
                .at(Some(ctx.location(field.ty.span())))
        })
}

/// Look up a with-wrapper by resolved path, trying glob prefixes for bare idents
/// (`use rkyv::with::*` + `with = AsBox`).
fn lookup_wrapper(
    codegen: &CodeGenerator,
    ctx: &SourceContext,
    path: &str,
) -> Option<WithWrapper> {
    if let Some(wrapper) = codegen.registry.get_wrapper(path) {
        return Some(wrapper.clone());
    }
    if !path.contains("::") {
        for glob in &ctx.globs {
            if let Some(wrapper) = codegen.registry.get_wrapper(&format!("{glob}::{path}")) {
                return Some(wrapper.clone());
            }
        }
    }
    None
}

/// A struct's extracted shape: named fields form a record codec;
/// unnamed (tuple-struct) fields are positional.
enum StructShape {
    Record(Vec<(String, CodecExpr)>),
    Tuple(Vec<CodecExpr>),
}

fn extract_struct_shape(
    type_name: &str,
    fields: &Fields,
    codegen: &CodeGenerator,
    ctx: &SourceContext,
) -> Result<StructShape, Vec<Diagnostic>> {
    let mut diagnostics = Vec::new();

    let shape = match fields {
        Fields::Named(named) => {
            let mut out = Vec::new();
            for field in &named.named {
                let field_name = field
                    .ident
                    .as_ref()
                    .expect("named fields have idents")
                    .to_string();
                let context = format!("{type_name}.{field_name}");
                match field_expr(field, &context, codegen, ctx) {
                    Ok(Some(expr)) => out.push((field_name, expr)),
                    Ok(None) => {}
                    Err(diagnostic) => diagnostics.push(diagnostic),
                }
            }
            StructShape::Record(out)
        }
        Fields::Unnamed(unnamed) => {
            let mut out = Vec::new();
            for (index, field) in unnamed.unnamed.iter().enumerate() {
                let context = format!("{type_name}.{index}");
                match field_expr(field, &context, codegen, ctx) {
                    Ok(Some(expr)) => out.push(expr),
                    Ok(None) => {}
                    Err(diagnostic) => diagnostics.push(diagnostic),
                }
            }
            StructShape::Tuple(out)
        }
        Fields::Unit => StructShape::Record(Vec::new()),
    };

    if diagnostics.is_empty() {
        Ok(shape)
    } else {
        Err(diagnostics)
    }
}

/// The codec expression for a tuple struct: archived exactly like a tuple of its fields,
/// so `struct Pair(A, B)` aliases `r.tuple(A, B)`. A single-field (newtype) struct is transparent — the inner codec — and
/// a zero-field one degenerates to `r.unit`, both matching rkyv's layout.
fn tuple_struct_expr(mut exprs: Vec<CodecExpr>) -> CodecExpr {
    match exprs.len() {
        0 => CodecExpr::runtime("unit"),
        1 => exprs.pop().expect("len checked"),
        _ => CodecExpr::call(CodecExpr::runtime("tuple"), exprs),
    }
}

fn extract_enum_variants(
    type_name: &str,
    variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
    codegen: &CodeGenerator,
    ctx: &SourceContext,
) -> Result<Vec<EnumVariant>, Vec<Diagnostic>> {
    let mut out = Vec::new();
    let mut diagnostics = Vec::new();

    for variant in variants {
        let variant_name = variant.ident.to_string();
        match &variant.fields {
            Fields::Unit => out.push(EnumVariant::Unit(variant_name)),
            Fields::Unnamed(unnamed) => {
                let mut exprs = Vec::new();
                for (index, field) in unnamed.unnamed.iter().enumerate() {
                    let context = format!("{type_name}::{variant_name}.{index}");
                    match field_expr(field, &context, codegen, ctx) {
                        Ok(Some(expr)) => exprs.push(expr),
                        Ok(None) => {}
                        Err(diagnostic) => diagnostics.push(diagnostic),
                    }
                }
                let variant = if unnamed.unnamed.len() == 1 {
                    // A newtype variant decodes as the bare inner value;
                    // a fully skipped one degenerates to a unit variant.
                    match exprs.pop() {
                        Some(expr) => EnumVariant::Newtype(variant_name, expr),
                        None => EnumVariant::Unit(variant_name),
                    }
                } else {
                    EnumVariant::Tuple(variant_name, exprs)
                };
                out.push(variant);
            }
            Fields::Named(named) => {
                let mut fields = Vec::new();
                for field in &named.named {
                    let field_name = field
                        .ident
                        .as_ref()
                        .expect("named fields have idents")
                        .to_string();
                    let context = format!("{type_name}::{variant_name}.{field_name}");
                    match field_expr(field, &context, codegen, ctx) {
                        Ok(Some(expr)) => fields.push((field_name, expr)),
                        Ok(None) => {}
                        Err(diagnostic) => diagnostics.push(diagnostic),
                    }
                }
                out.push(EnumVariant::Struct(variant_name, fields));
            }
        }
    }

    if diagnostics.is_empty() {
        Ok(out)
    } else {
        Err(diagnostics)
    }
}

/// The inline codec expression for a struct (used for remote proxies).
fn struct_expr(fields: Vec<(String, CodecExpr)>) -> CodecExpr {
    CodecExpr::call(
        CodecExpr::runtime("struct"),
        [CodecExpr::object(fields)],
    )
}

/// The inline codec expression for an enum (used for remote proxies).
fn enum_expr(variants: Vec<EnumVariant>) -> CodecExpr {
    let entries = variants.into_iter().map(|variant| match variant {
        EnumVariant::Unit(name) => (name, CodecExpr::raw("null")),
        EnumVariant::Newtype(name, expr) => (name, expr),
        EnumVariant::Tuple(name, exprs) => (name, CodecExpr::array(exprs)),
        EnumVariant::Struct(name, fields) => (name, CodecExpr::object(fields)),
    });
    CodecExpr::call(
        CodecExpr::runtime("taggedEnum"),
        [CodecExpr::object(entries)],
    )
}

/// A top-level struct or enum item.
enum TypeItem<'a> {
    Struct(&'a syn::ItemStruct),
    Enum(&'a syn::ItemEnum),
}

impl TypeItem<'_> {
    fn attrs(&self) -> &[Attribute] {
        match self {
            TypeItem::Struct(item) => &item.attrs,
            TypeItem::Enum(item) => &item.attrs,
        }
    }

    fn ident(&self) -> &syn::Ident {
        match self {
            TypeItem::Struct(item) => &item.ident,
            TypeItem::Enum(item) => &item.ident,
        }
    }
}

fn type_items(file: &syn::File) -> Vec<TypeItem<'_>> {
    file.items
        .iter()
        .filter_map(|item| match item {
            syn::Item::Struct(s) => Some(TypeItem::Struct(s)),
            syn::Item::Enum(e) => Some(TypeItem::Enum(e)),
            _ => None,
        })
        .collect()
}

fn parse_source(
    codegen: &mut CodeGenerator,
    source: &str,
    file: Option<PathBuf>,
) -> Result<(), Error> {
    let parsed = syn::parse_file(source).map_err(|source| Error::Parse {
        file: file.clone(),
        source,
    })?;
    let ctx = build_source_context(&parsed, file);
    let items = type_items(&parsed);

    // Pass 1: remote proxies. `#[rkyv(remote = T)]` types register themselves as with-wrappers and emit no top-level export.
    // Running this pass first makes proxy usage order-independent within a file.
    for item in &items {
        if !has_marker_derive(item.attrs(), &ctx, codegen) {
            continue;
        }
        let attrs = parse_rkyv_type_attrs(item.attrs());
        if attrs.remote.is_none() {
            continue;
        }
        let name = item.ident().to_string();
        let built = match item {
            TypeItem::Struct(s) => {
                extract_struct_shape(&name, &s.fields, codegen, &ctx).map(|shape| match shape {
                    StructShape::Record(fields) => struct_expr(fields),
                    StructShape::Tuple(exprs) => tuple_struct_expr(exprs),
                })
            }
            TypeItem::Enum(e) => {
                extract_enum_variants(&name, &e.variants, codegen, &ctx).map(enum_expr)
            }
        };
        match built {
            Ok(expr) => {
                if let Some(fq_path) = ctx.imports.get(&name) {
                    codegen
                        .registry
                        .register_wrapper(fq_path.clone(), WithWrapper::replace(expr.clone()));
                }
                codegen
                    .registry
                    .register_wrapper(name, WithWrapper::replace(expr));
            }
            Err(diagnostics) => match codegen.on_unknown {
                OnUnknown::Error => codegen.add_diagnostics.extend(diagnostics),
                OnUnknown::SkipContainingType => {
                    for diagnostic in diagnostics {
                        eprintln!(
                            "cargo:warning=rkyv-js-codegen: skipping remote proxy `{name}`: \
                             {diagnostic}"
                        );
                    }
                }
            },
        }
    }

    // Pass 2: regular types.
    for item in &items {
        if !has_marker_derive(item.attrs(), &ctx, codegen) {
            continue;
        }
        let attrs = parse_rkyv_type_attrs(item.attrs());
        if attrs.remote.is_some() {
            continue;
        }
        let name = item.ident().to_string();
        let location = Some(ctx.location(item.ident().span()));
        let extracted = match item {
            TypeItem::Struct(s) => {
                extract_struct_shape(&name, &s.fields, codegen, &ctx).map(|shape| match shape {
                    StructShape::Record(fields) => TypeKind::Struct(fields),
                    StructShape::Tuple(exprs) => TypeKind::Alias(tuple_struct_expr(exprs)),
                })
            }
            TypeItem::Enum(e) => {
                extract_enum_variants(&name, &e.variants, codegen, &ctx).map(TypeKind::Enum)
            }
        };
        match extracted {
            Ok(kind) => codegen.add_type(name.clone(), kind, location),
            Err(diagnostics) => codegen.add_failed_type(name.clone(), diagnostics, location),
        }
        if let Some(archived) = attrs.archived {
            codegen.set_archived_name(name, archived);
        }
    }

    Ok(())
}

impl CodeGenerator {
    /// Parse a Rust source file and extract every type with a marker derive.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rkyv_js_codegen::CodeGenerator;
    ///
    /// fn main() -> Result<(), rkyv_js_codegen::Error> {
    ///     CodeGenerator::new()
    ///         .add_source_file("src/lib.rs")?
    ///         .write_to_file("generated/bindings.ts")?;
    ///     Ok(())
    /// }
    /// ```
    pub fn add_source_file(&mut self, path: impl AsRef<Path>) -> Result<&mut Self, Error> {
        let path = path.as_ref();
        let source = fs::read_to_string(path)?;
        parse_source(self, &source, Some(path.to_path_buf()))?;
        Ok(self)
    }

    /// Parse Rust source from a string and extract every type with a marker derive.
    pub fn add_source_str(&mut self, source: &str) -> Result<&mut Self, Error> {
        parse_source(self, source, None)?;
        Ok(self)
    }

    /// Recursively scan a directory for `.rs` files and extract every type with a marker derive.
    /// Files are processed in path order.
    pub fn add_source_dir(&mut self, path: impl AsRef<Path>) -> Result<&mut Self, Error> {
        let mut files: Vec<PathBuf> = Vec::new();
        for entry in WalkDir::new(path) {
            let entry = entry.map_err(io::Error::other)?;
            let entry_path = entry.path();
            if entry_path.extension().is_some_and(|ext| ext == "rs") {
                files.push(entry_path.to_path_buf());
            }
        }
        files.sort();
        for file in files {
            self.add_source_file(&file)?;
        }
        Ok(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::DiagnosticKind;
    use crate::registry::ExternalType;

    fn generate(source: &str) -> String {
        let mut codegen = CodeGenerator::new();
        codegen.add_source_str(source).unwrap();
        codegen.generate().unwrap()
    }

    fn generate_diagnostics(source: &str) -> Vec<Diagnostic> {
        let mut codegen = CodeGenerator::new();
        codegen.add_source_str(source).unwrap();
        match codegen.generate() {
            Err(Error::Codegen(diagnostics)) => diagnostics,
            other => panic!("expected codegen diagnostics, got {other:?}"),
        }
    }

    #[test]
    fn extracts_simple_struct() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Point { x: f64, y: f64 }
        "#,
        );
        assert!(code.contains("export const ArchivedPoint = r.struct({\n  x: r.f64,\n  y: r.f64,\n});"));
        assert!(code.contains("export type Point = r.Infer<typeof ArchivedPoint>;"));
    }

    #[test]
    fn extracts_containers_and_str() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Person {
                name: String,
                nickname: &'static str,
                age: u32,
                scores: Vec<u32>,
                email: Option<String>,
                boxed: Box<u64>,
                arr: [u16; 4],
                tup: (u8, String, f64),
                unit: (),
            }
        "#,
        );
        assert!(code.contains("name: r.string,"));
        assert!(code.contains("nickname: r.string,"));
        assert!(code.contains("scores: r.vec(r.u32),"));
        assert!(code.contains("email: r.option(r.string),"));
        assert!(code.contains("boxed: r.box(r.u64),"));
        assert!(code.contains("arr: r.array(r.u16, 4),"));
        assert!(code.contains("tup: r.tuple(r.u8, r.string, r.f64),"));
        assert!(code.contains("unit: r.unit,"));
    }

    #[test]
    fn extracts_enum_with_new_variant_shapes() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            enum Message {
                Quit,
                Move { x: i32, y: i32 },
                Write(String),
                ChangeColor(u8, u8, u8),
            }
        "#,
        );
        assert!(code.contains(
            "export const ArchivedMessage = r.taggedEnum({\n\
             \x20 Quit: null,\n\
             \x20 Move: { x: r.i32, y: r.i32 },\n\
             \x20 Write: r.string,\n\
             \x20 ChangeColor: [r.u8, r.u8, r.u8],\n\
             });"
        ));
    }

    #[test]
    fn extracts_tuple_struct() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Pair(u32, String);
        "#,
        );
        assert!(code.contains("export const ArchivedPair = r.tuple(r.u32, r.string);"));
    }

    #[test]
    fn cross_references_resolve_to_archived_names() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Inner { value: u32 }
            #[derive(Archive)]
            struct Outer { inner: Inner, items: Vec<Inner> }
        "#,
        );
        assert!(code.contains("inner: ArchivedInner,"));
        assert!(code.contains("items: r.vec(ArchivedInner),"));
        let inner_pos = code.find("export const ArchivedInner").unwrap();
        let outer_pos = code.find("export const ArchivedOuter").unwrap();
        assert!(inner_pos < outer_pos);
    }

    #[test]
    fn marker_via_plain_import() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Debug)]
            struct NotExported { x: i32 }
            #[derive(Debug, Archive)]
            struct Exported { y: i32 }
        "#,
        );
        assert!(!code.contains("ArchivedNotExported"));
        assert!(code.contains("ArchivedExported"));
    }

    #[test]
    fn marker_via_qualified_path() {
        let code = generate(
            r#"
            #[derive(rkyv::Archive)]
            struct QualifiedPath { value: u32 }
            #[derive(::rkyv::Archive)]
            struct LeadingColons { value: u32 }
        "#,
        );
        assert!(code.contains("ArchivedQualifiedPath"));
        assert!(code.contains("ArchivedLeadingColons"));
    }

    #[test]
    fn marker_via_rename() {
        let code = generate(
            r#"
            use rkyv::Archive as Rkyv;
            #[derive(Rkyv)]
            struct AliasedMarker { value: i32 }
        "#,
        );
        assert!(code.contains("ArchivedAliasedMarker"));
    }

    #[test]
    fn marker_via_glob_import() {
        let code = generate(
            r#"
            use rkyv::*;
            #[derive(Archive)]
            struct GlobMarked { value: i32 }
        "#,
        );
        assert!(code.contains("ArchivedGlobMarked"));
    }

    #[test]
    fn marker_via_add_marker_path() {
        let mut codegen = CodeGenerator::new();
        codegen.add_marker_path("my_macros::TS");
        codegen
            .add_source_str(
                r#"
                use my_macros::TS;
                #[derive(TS)]
                struct Custom { value: u8 }
                #[derive(my_macros::TS)]
                struct CustomQualified { value: u8 }
            "#,
            )
            .unwrap();
        let code = codegen.generate().unwrap();
        assert!(code.contains("ArchivedCustom"));
        assert!(code.contains("ArchivedCustomQualified"));
    }

    #[test]
    fn foreign_archive_paths_do_not_match() {
        // The old `ends_with("::Archive")` over-match must be gone.
        let mut codegen = CodeGenerator::new();
        codegen
            .add_source_str(
                r#"
                #[derive(some_alias::Archive)]
                struct NotOurs { id: u64 }
                #[derive(deeply::nested::module::Archive)]
                struct AlsoNotOurs { data: String }
            "#,
            )
            .unwrap();
        let code = codegen.generate().unwrap();
        assert!(!code.contains("ArchivedNotOurs"));
        assert!(!code.contains("ArchivedAlsoNotOurs"));
    }

    #[test]
    fn bare_marker_without_import_does_not_match() {
        let code = generate(
            r#"
            #[derive(Archive)]
            struct NotDetected { a: i32 }
            #[derive(Rkyv)]
            struct AlsoNotDetected { b: i32 }
        "#,
        );
        assert!(!code.contains("ArchivedNotDetected"));
        assert!(!code.contains("ArchivedAlsoNotDetected"));
    }

    #[test]
    fn builtin_leaf_types() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use uuid::Uuid;
            use bytes::Bytes;
            use smol_str::SmolStr;
            #[derive(Archive)]
            struct Record { id: Uuid, payload: Bytes, key: SmolStr }
        "#,
        );
        assert!(code.contains("import { bytes } from 'rkyv-js/lib/bytes';"));
        assert!(code.contains("import { uuid } from 'rkyv-js/lib/uuid';"));
        assert!(code.contains("id: uuid,"));
        assert!(code.contains("payload: bytes,"));
        assert!(code.contains("key: r.string,"));
    }

    #[test]
    fn builtin_vec_like_types() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use thin_vec::ThinVec;
            use arrayvec::ArrayVec;
            use smallvec::SmallVec;
            use tinyvec::TinyVec;
            use std::collections::VecDeque;
            #[derive(Archive)]
            struct Data {
                thin: ThinVec<u32>,
                array_vec: ArrayVec<u8, 64>,
                small: SmallVec<[u32; 4]>,
                tiny: TinyVec<[String; 8]>,
                deque: VecDeque<u32>,
            }
        "#,
        );
        assert!(code.contains("thin: r.vec(r.u32),"));
        assert!(code.contains("array_vec: r.vec(r.u8),"));
        assert!(code.contains("small: r.vec(r.u32),"));
        assert!(code.contains("tiny: r.vec(r.string),"));
        assert!(code.contains("deque: r.vec(r.u32),"));
    }

    #[test]
    fn builtin_maps_and_sets() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet};
            use indexmap::{IndexMap, IndexSet};
            #[derive(Archive)]
            struct Collections {
                hm: HashMap<String, u32>,
                hs: HashSet<String>,
                bm: BTreeMap<String, u64>,
                bs: BTreeSet<i64>,
                im: IndexMap<String, u32>,
                is: IndexSet<String>,
            }
        "#,
        );
        assert!(code.contains("import { btreeMap, btreeSet } from 'rkyv-js/lib/btreemap';"));
        assert!(code.contains("import { hashMap, hashSet } from 'rkyv-js/lib/hashmap';"));
        assert!(code.contains("import { indexMap, indexSet } from 'rkyv-js/lib/indexmap';"));
        assert!(code.contains("hm: hashMap(r.string, r.u32),"));
        assert!(code.contains("hs: hashSet(r.string),"));
        assert!(code.contains("bm: btreeMap(r.string, r.u64),"));
        assert!(code.contains("bs: btreeSet(r.i64),"));
        assert!(code.contains("im: indexMap(r.string, r.u32),"));
        assert!(code.contains("is: indexSet(r.string),"));
    }

    #[test]
    fn builtin_pointer_types() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use std::rc::{Rc, Weak};
            #[derive(Archive)]
            struct Shared { data: Rc<String>, weak_ref: Weak<u32>, arc: triomphe::Arc<String> }
        "#,
        );
        assert!(code.contains("data: r.rc(r.string),"));
        assert!(code.contains("weak_ref: r.weak(r.u32),"));
        assert!(code.contains("arc: r.rc(r.string),"));
    }

    #[test]
    fn renamed_imports_resolve() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use std::collections::{HashMap as Map, BTreeSet as SortedSet};
            use uuid::Uuid as Id;
            #[derive(Archive)]
            struct Data { map: Map<String, u32>, set: SortedSet<String>, id: Id }
        "#,
        );
        assert!(code.contains("map: hashMap(r.string, r.u32),"));
        assert!(code.contains("set: btreeSet(r.string),"));
        assert!(code.contains("id: uuid,"));
    }

    #[test]
    fn generic_type_alias_resolves_to_registry_path() {
        // The pinned-hasher alias pattern: generic parameters on the LEFT,
        // a trailing hasher argument on the RIGHT. Only the path resolves;
        // the use site supplies exactly K and V.
        let code = generate(
            r#"
            use rkyv::Archive;
            pub type FixedState = std::hash::BuildHasherDefault<Hasher13>;
            pub type HashMap<K, V> = std::collections::HashMap<K, V, FixedState>;
            #[derive(Archive)]
            struct Data { m: HashMap<String, u32> }
        "#,
        );
        assert!(code.contains("m: hashMap(r.string, r.u32),"));
    }

    #[test]
    fn inline_trailing_hasher_is_allowed() {
        let code = generate(
            r#"
            use rkyv::Archive;
            pub type State = std::hash::BuildHasherDefault<Hasher13>;
            #[derive(Archive)]
            struct Data { m: std::collections::HashMap<String, u32, State> }
        "#,
        );
        assert!(code.contains("m: hashMap(r.string, r.u32),"));
    }

    #[test]
    fn generic_arity_too_few_args() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            use std::collections::HashMap;
            #[derive(Archive)]
            struct Data { m: HashMap<String> }
        "#,
        );
        assert_eq!(diagnostics.len(), 1);
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::GenericArity { rust_path, expected: 2, found: 1 }
                if rust_path == "std::collections::HashMap"
        ));
        assert_eq!(diagnostics[0].referenced_by.as_deref(), Some("Data.m"));
    }

    #[test]
    fn generic_arity_too_many_args_without_trailing() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Data { m: std::collections::BTreeMap<String, u32, Extra> }
        "#,
        );
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::GenericArity { rust_path, expected: 2, found: 3 }
                if rust_path == "std::collections::BTreeMap"
        ));
    }

    #[test]
    fn unknown_type_reports_path_and_suggestion() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Event { id: my_uuid::Uuid, at: chrono::NaiveDate }
        "#,
        );
        assert_eq!(diagnostics.len(), 2);
        assert!(diagnostics.iter().any(|diagnostic| matches!(
            &diagnostic.kind,
            DiagnosticKind::UnknownType { rust_path, suggestion: Some(suggestion) }
                if rust_path == "my_uuid::Uuid" && suggestion == "uuid::Uuid"
        )));
        assert!(diagnostics.iter().any(|diagnostic| matches!(
            &diagnostic.kind,
            DiagnosticKind::UnknownType { rust_path, suggestion: None }
                if rust_path == "chrono::NaiveDate"
        )));
    }

    #[test]
    fn unknown_imported_type_is_not_a_dangling_ref() {
        // A single-segment ident that resolves via imports to an unregistered path must be an UnknownType error,
        // not a silent `ArchivedNaiveDate` type reference.
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            use chrono::NaiveDate;
            #[derive(Archive)]
            struct Event { at: NaiveDate }
        "#,
        );
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::UnknownType { rust_path, .. } if rust_path == "chrono::NaiveDate"
        ));
    }

    #[test]
    fn diagnostics_carry_source_locations() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Event { at: chrono::NaiveDate }
        "#,
        );
        let location = diagnostics[0].location.as_ref().unwrap();
        assert_eq!(location.file, None);
        assert_eq!(location.line, 4);
        assert!(location.column > 1);
    }

    #[test]
    fn undeclared_local_ref_is_unresolved() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Outer { inner: NeverDeclared }
        "#,
        );
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::UnresolvedTypeRef { name } if name == "NeverDeclared"
        ));
        assert_eq!(diagnostics[0].referenced_by.as_deref(), Some("Outer.inner"));
    }

    #[test]
    fn duplicate_types_across_sources() {
        let mut codegen = CodeGenerator::new();
        codegen
            .add_source_str("use rkyv::Archive; #[derive(Archive)] struct Point { x: f64 }")
            .unwrap();
        codegen
            .add_source_str("use rkyv::Archive; #[derive(Archive)] struct Point { y: f64 }")
            .unwrap();
        let Err(Error::Codegen(diagnostics)) = codegen.generate() else {
            panic!("expected duplicate diagnostic");
        };
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::DuplicateType { name } if name == "Point"
        ));
    }

    #[test]
    fn parse_errors_propagate() {
        let mut codegen = CodeGenerator::new();
        let error = codegen.add_source_str("struct {").unwrap_err();
        assert!(matches!(error, Error::Parse { file: None, .. }));
    }

    #[test]
    fn skip_mode_omits_unknown_and_dependents() {
        let mut codegen = CodeGenerator::new();
        codegen.on_unknown_type(OnUnknown::SkipContainingType);
        codegen
            .add_source_str(
                r#"
                use rkyv::Archive;
                #[derive(Archive)]
                struct Fine { x: u32 }
                #[derive(Archive)]
                struct Broken { at: chrono::NaiveDate }
                #[derive(Archive)]
                struct UsesBroken { broken: Broken }
                #[derive(Archive)]
                struct UsesUsesBroken { nested: UsesBroken }
            "#,
            )
            .unwrap();
        let code = codegen.generate().unwrap();
        assert!(code.contains("ArchivedFine"));
        assert!(!code.contains("ArchivedBroken"));
        assert!(!code.contains("ArchivedUsesBroken"));
        assert!(!code.contains("ArchivedUsesUsesBroken"));
    }

    #[test]
    fn with_asbox_boxes_the_underlying_codec() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use rkyv::with::AsBox;
            #[derive(Archive)]
            struct Data {
                #[rkyv(with = AsBox)]
                big: String,
            }
        "#,
        );
        assert!(code.contains("big: r.box(r.string),"));
    }

    #[test]
    fn with_inline_is_identity() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Data {
                #[rkyv(with = rkyv::with::Inline)]
                value: u32,
                #[rkyv(with = rkyv::with::InlineAsBox)]
                other: u64,
            }
        "#,
        );
        assert!(code.contains("value: r.u32,"));
        assert!(code.contains("other: r.box(r.u64),"));
    }

    #[test]
    fn with_skip_omits_the_field() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use rkyv::with::Skip;
            #[derive(Archive)]
            struct Data {
                kept: u32,
                #[rkyv(with = Skip)]
                dropped: String,
            }
        "#,
        );
        assert!(code.contains("kept: r.u32,"));
        assert!(!code.contains("dropped"));
    }

    #[test]
    fn with_unknown_wrapper_is_a_diagnostic() {
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Data {
                #[rkyv(with = Mystery)]
                value: u32,
            }
        "#,
        );
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::UnknownWithWrapper { wrapper_path } if wrapper_path == "Mystery"
        ));
        assert_eq!(diagnostics[0].referenced_by.as_deref(), Some("Data.value"));
    }

    #[test]
    fn with_replace_never_resolves_the_field_type() {
        // remote::Coord is not registered; a replace wrapper must not care.
        let mut codegen = CodeGenerator::new();
        codegen.register_with(
            "AsJson",
            WithWrapper::replace(CodecExpr::import_from("./coord.ts", "Coord")),
        );
        codegen
            .add_source_str(
                r#"
                use rkyv::Archive;
                #[derive(Archive)]
                struct RemoteEvent {
                    name: String,
                    #[rkyv(with = AsJson)]
                    location: remote::Coord,
                    priority: u32,
                }
            "#,
            )
            .unwrap();
        let code = codegen.generate().unwrap();
        assert!(code.contains("import { Coord } from './coord.ts';"));
        assert!(code.contains("location: Coord,"));
    }

    #[test]
    fn with_wrapper_resolves_through_glob_imports() {
        let code = generate(
            r#"
            use rkyv::Archive;
            use rkyv::with::*;
            #[derive(Archive)]
            struct Data {
                #[rkyv(with = AsBox)]
                big: String,
            }
        "#,
        );
        assert!(code.contains("big: r.box(r.string),"));
    }

    #[test]
    fn remote_proxy_registers_itself_as_wrapper() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            #[rkyv(remote = chrono::NaiveDate)]
            struct NaiveDateDef {
                year: i32,
                ordinal: u32,
            }
            #[derive(Archive)]
            struct Event {
                name: String,
                #[rkyv(with = NaiveDateDef)]
                date: chrono::NaiveDate,
            }
        "#,
        );
        // The proxy emits no top-level export...
        assert!(!code.contains("ArchivedNaiveDateDef"));
        // ...and the consuming field gets the proxy's own struct codec.
        assert!(code.contains("date: r.struct({ year: r.i32, ordinal: r.u32 }),"));
    }

    #[test]
    fn remote_proxy_is_order_independent_within_a_file() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            struct Event {
                #[rkyv(with = CoordDef)]
                location: remote::Coord,
            }
            #[derive(Archive)]
            #[rkyv(remote = remote::Coord)]
            struct CoordDef { x: f32, y: f32 }
        "#,
        );
        assert!(code.contains("location: r.struct({ x: r.f32, y: r.f32 }),"));
        assert!(!code.contains("ArchivedCoordDef"));
    }

    #[test]
    fn remote_field_without_with_is_unknown() {
        // rkyv 0.8 consumes remote proxies via #[rkyv(with = ProxyDef)];
        // a bare field of the remote type does not resolve.
        let diagnostics = generate_diagnostics(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            #[rkyv(remote = chrono::NaiveDate)]
            struct NaiveDateDef { year: i32, ordinal: u32 }
            #[derive(Archive)]
            struct Event { date: chrono::NaiveDate }
        "#,
        );
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::UnknownType { rust_path, .. } if rust_path == "chrono::NaiveDate"
        ));
    }

    #[test]
    fn archived_rename_attribute() {
        let code = generate(
            r#"
            use rkyv::Archive;
            #[derive(Archive)]
            #[rkyv(compare(PartialEq), archived = CustomPoint, derive(Debug))]
            struct Point { x: f64, y: f64 }
            #[derive(Archive)]
            struct Line { start: Point, end: Point }
        "#,
        );
        assert!(code.contains("export const CustomPoint = r.struct({"));
        assert!(code.contains("export type Point = r.Infer<typeof CustomPoint>;"));
        assert!(code.contains("start: CustomPoint,"));
        assert!(!code.contains("ArchivedPoint"));
    }

    #[test]
    fn custom_external_type() {
        let mut codegen = CodeGenerator::new();
        codegen.register_external(
            "my_crate::CustomVec",
            ExternalType::generic1(|t| {
                CodecExpr::call(CodecExpr::import_from("my-package/codecs", "customVec"), [t])
            }),
        );
        codegen
            .add_source_str(
                r#"
                use rkyv::Archive;
                use my_crate::CustomVec;
                #[derive(Archive)]
                struct MyData { custom: CustomVec<u32> }
            "#,
            )
            .unwrap();
        let code = codegen.generate().unwrap();
        assert!(code.contains("import { customVec } from 'my-package/codecs';"));
        assert!(code.contains("custom: customVec(r.u32),"));
    }

    #[test]
    fn unregister_external_removes_builtin() {
        let mut codegen = CodeGenerator::new();
        codegen.unregister_external("uuid::Uuid");
        codegen
            .add_source_str(
                r#"
                use rkyv::Archive;
                use uuid::Uuid;
                #[derive(Archive)]
                struct Record { id: Uuid }
            "#,
            )
            .unwrap();
        let Err(Error::Codegen(diagnostics)) = codegen.generate() else {
            panic!("expected unknown type diagnostic");
        };
        assert!(matches!(
            &diagnostics[0].kind,
            DiagnosticKind::UnknownType { rust_path, .. } if rust_path == "uuid::Uuid"
        ));
    }
}