internity_macros_impl 0.2.1

Procedural macro implementation for the internity crate.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use quote::quote;
use syn::{DeriveInput, Field, parse_quote};

use super::{default_root, expand_deserialize, expand_serialize, field_seed, parse_container, parse_field, resolve_de_root};

fn compact(tokens: impl quote::ToTokens) -> String {
    tokens.to_token_stream().to_string().replace(' ', "")
}

fn expand(input: &DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    expand_deserialize(input, &default_root())
}

fn serialize(input: &DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    expand_serialize(input, &default_root())
}

#[test]
fn parses_container_configuration() {
    let default_input: DeriveInput = parse_quote!(
        #[derive(Clone)]
        struct Example;
    );
    assert_eq!(compact(default_root()), "::internity");
    assert_eq!(
        compact(resolve_de_root(&parse_container(&default_input.attrs).unwrap(), &default_root())),
        "::internity::de"
    );

    let custom_input: DeriveInput = parse_quote!(
        #[internity(crate = "renamed")]
        struct Example;
    );
    assert_eq!(
        compact(resolve_de_root(&parse_container(&custom_input.attrs).unwrap(), &default_root())),
        "renamed::de"
    );

    let invalid_input: DeriveInput = parse_quote!(
        #[internity(other)]
        struct Example;
    );
    assert!(
        parse_container(&invalid_input.attrs).is_err(),
        "unsupported container attribute must fail"
    );
}

#[test]
fn parses_field_configuration() {
    let ordinary: Field = parse_quote!(
        #[doc = "ignored"]
        value: String
    );
    assert!(!parse_field(&ordinary.attrs).unwrap().via_serde);

    let via_serde: Field = parse_quote!(
        #[internity(via_serde)]
        value: String
    );
    assert!(parse_field(&via_serde.attrs).unwrap().via_serde);

    let invalid: Field = parse_quote!(
        #[internity(other)]
        value: String
    );
    assert!(parse_field(&invalid.attrs).is_err(), "unsupported field attribute must fail");
}

#[test]
fn selects_the_requested_field_seed() {
    let root = default_root();
    let root = super::append_module(&root, parse_quote!(de));
    let interner = parse_quote!(__Interner);
    let ty = parse_quote!(String);
    let access = quote!(self.__internity_interner);

    let via_serde_field: Field = parse_quote!(#[internity(via_serde)] value: String);
    let via_serde = parse_field(&via_serde_field.attrs).unwrap();
    assert_eq!(
        compact(field_seed(&root, &interner, &ty, &via_serde, None, &access)),
        "::internity::de::DeserializeSeed::<String>::new()"
    );

    let plain_field: Field = parse_quote!(value: String);
    let plain = parse_field(&plain_field.attrs).unwrap();
    assert_eq!(
        compact(field_seed(&root, &interner, &ty, &plain, None, &access)),
        "::internity::de::DeserializeInSeed::<String,__Interner>::new(&mut*self.__internity_interner)"
    );
}

#[test]
fn expands_named_tuple_and_unit_structs() {
    let named: DeriveInput = parse_quote! {
        struct Named {
            symbol: Sym,
            #[internity(via_serde)]
            count: u32,
        }
    };
    let named = compact(expand(&named).unwrap());
    assert!(named.contains("deserialize_struct"));
    assert!(named.contains("DeserializeInSeed::<Sym"));
    assert!(named.contains("DeserializeSeed::<u32>"));

    let newtype: DeriveInput = parse_quote!(
        struct Newtype(Sym);
    );
    let newtype = compact(expand(&newtype).unwrap());
    assert!(newtype.contains("deserialize_newtype_struct"));
    assert!(newtype.contains("visit_newtype_struct"));
    assert!(!newtype.contains("deserialize_tuple_struct"));

    let pair: DeriveInput = parse_quote!(
        struct Pair(Sym, u32);
    );
    let pair = compact(expand(&pair).unwrap());
    assert!(pair.contains("deserialize_tuple_struct"));
    assert!(!pair.contains("visit_newtype_struct"));

    let unit: DeriveInput = parse_quote!(
        struct Unit;
    );
    let unit = compact(expand(&unit).unwrap());
    assert!(unit.contains("deserialize_unit_struct"));
    assert!(unit.contains("Result::Ok(Unit)"));
}

#[test]
fn uses_unraw_identifiers_for_generated_names_and_wire_fields() {
    let input: DeriveInput = parse_quote! {
        struct r#Type {
            r#match: Sym,
        }
    };
    let output = compact(expand(&input).unwrap());
    assert!(output.contains("__InternityInternerForType"));
    assert!(output.contains("\"match\""));
    assert!(!output.contains("\"r#match\""));
}

#[test]
fn rejects_unsupported_input_shapes() {
    let generic: DeriveInput = parse_quote!(
        struct Generic<T>(T);
    );
    expand(&generic).expect_err("generic structs must be rejected");
    serialize(&generic).expect_err("generic structs must be rejected by SerializeIn");

    let enumeration: DeriveInput = parse_quote!(
        enum Choice {
            A,
        }
    );
    expand(&enumeration).expect_err("enums must be rejected");

    let union: DeriveInput = syn::parse2(quote!(union Choice { value: u32 })).unwrap();
    expand(&union).expect_err("unions must be rejected");
}

#[test]
fn rejects_conversion_container_attributes() {
    expand(&parse_quote!(
        #[serde(from = "Other")]
        struct T {
            value: Sym,
        }
    ))
    .expect_err("`from` cannot be honored by DeserializeIn");
    expand(&parse_quote!(
        #[serde(try_from = "Other")]
        struct T {
            value: Sym,
        }
    ))
    .expect_err("`try_from` cannot be honored by DeserializeIn");
    serialize(&parse_quote!(
        #[serde(into = "Other")]
        struct T {
            value: Sym,
        }
    ))
    .expect_err("`into` cannot be honored by SerializeIn");
}

#[test]
fn expansion_propagates_attribute_parse_errors() {
    expand(&parse_quote!(
        #[serde(rename = 5)]
        struct T {
            value: Sym,
        }
    ))
    .expect_err("deserialize expansion must propagate container attribute errors");
    serialize(&parse_quote!(
        #[serde(rename = 5)]
        struct T {
            value: Sym,
        }
    ))
    .expect_err("serialize expansion must propagate container attribute errors");
    expand(&parse_quote!(
        struct T {
            #[serde(rename = 5)]
            value: Sym,
        }
    ))
    .expect_err("named deserialize expansion must propagate field attribute errors");
    expand(&parse_quote!(
        struct T(#[serde(rename = 5)] Sym);
    ))
    .expect_err("tuple deserialize expansion must propagate field attribute errors");
    serialize(&parse_quote!(
        struct T {
            #[serde(rename = 5)]
            value: Sym,
        }
    ))
    .expect_err("named serialize expansion must propagate field attribute errors");
    serialize(&parse_quote!(
        struct T(#[serde(rename = 5)] Sym);
    ))
    .expect_err("tuple serialize expansion must propagate field attribute errors");
}

#[test]
fn serialize_rejects_skip_serializing_if() {
    // `DeserializeIn` still accepts the attribute, but `SerializeIn` refuses it
    // rather than silently ignoring the runtime skip predicate.
    expand(&parse_quote!(
        struct T {
            #[serde(skip_serializing_if = "Option::is_none")]
            value: Option<Sym>,
        }
    ))
    .expect("deserialize must accept skip_serializing_if");
    let named = serialize(&parse_quote!(
        struct T {
            #[serde(skip_serializing_if = "Option::is_none")]
            value: Option<Sym>,
        }
    ))
    .expect_err("named serialize must reject skip_serializing_if");
    assert!(named.to_string().contains("skip_serializing_if"));
    serialize(&parse_quote!(
        struct T(#[serde(skip_serializing_if = "Option::is_none")] Option<Sym>);
    ))
    .expect_err("tuple serialize must reject skip_serializing_if");

    // `skip_serializing` already drops the field, so pairing it with
    // `skip_serializing_if` is accepted (the predicate is irrelevant).
    serialize(&parse_quote!(
        struct T {
            #[serde(skip_serializing, skip_serializing_if = "Option::is_none")]
            value: Option<Sym>,
            keep: Sym,
        }
    ))
    .expect("skip_serializing exempts a field from the skip_serializing_if rejection");
}

#[test]
fn conflicting_modes_are_rejected_per_direction() {
    // `via_serde` + a deserialize-only `deserialize_with` is a valid *serialize*
    // schema, so only the deserialize expander rejects the contradiction.
    serialize(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(deserialize_with = "d")]
            value: Sym,
        }
    ))
    .expect("serialize must accept via_serde alongside a deserialize-only deserialize_with");
    let de = expand(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(deserialize_with = "d")]
            value: Sym,
        }
    ))
    .expect_err("deserialize must reject via_serde combined with deserialize_with");
    assert!(de.to_string().contains("mutually exclusive"));

    // Symmetrically, `via_serde` + a serialize-only `serialize_with` is a valid
    // *deserialize* schema, so only the serialize expander rejects it.
    expand(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(serialize_with = "s")]
            value: Sym,
        }
    ))
    .expect("deserialize must accept via_serde alongside a serialize-only serialize_with");
    let se = serialize(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(serialize_with = "s")]
            value: Sym,
        }
    ))
    .expect_err("serialize must reject via_serde combined with serialize_with");
    assert!(se.to_string().contains("mutually exclusive"));

    // `with` configures both directions, so both expanders reject it with `via_serde`.
    expand(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(with = "m")]
            value: Sym,
        }
    ))
    .expect_err("deserialize must reject via_serde combined with with");
    serialize(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(with = "m")]
            value: Sym,
        }
    ))
    .expect_err("serialize must reject via_serde combined with with");

    // Skipping deserialization while also requesting a custom deserializer is a
    // deserialize-side contradiction only.
    let skip = expand(&parse_quote!(
        struct T {
            #[internity(via_serde)]
            #[serde(skip_deserializing)]
            value: Sym,
        }
    ))
    .expect_err("deserialize must reject skip_deserializing combined with via_serde");
    assert!(skip.to_string().contains("custom deserializer mode"));
}

#[test]
fn with_seed_def_returns_none_without_custom_deserializer() {
    let root = super::append_module(&default_root(), parse_quote!(de));
    let name = parse_quote!(__Seed);
    let ty = parse_quote!(Sym);
    assert!(super::with_seed_def(&root, &name, &ty, &super::FieldAttrs::default()).is_none());
}

#[test]
fn transparent_validation_rejects_non_structs_when_called_directly() {
    let input: DeriveInput = parse_quote! {
        #[serde(transparent)]
        enum Choice { A }
    };
    let container = parse_container(&input.attrs).unwrap();
    super::validate_transparent_container(&input, &container).expect_err("transparent applies only to structs");
}

#[test]
fn supports_tuple_container_default_and_transparent_newtypes() {
    let defaulted = compact(
        expand(&parse_quote!(
            #[serde(default)]
            struct T(Sym);
        ))
        .unwrap(),
    );
    assert!(defaulted.contains("__container_default"));

    let transparent = compact(
        expand(&parse_quote!(
            #[serde(transparent)]
            struct T(Sym);
        ))
        .unwrap(),
    );
    assert!(transparent.contains("DeserializeSeed::deserialize"));

    expand(&parse_quote!(
        #[serde(default)]
        struct Unit;
    ))
    .expect_err("container default is invalid on unit structs");
    expand(&parse_quote!(
        #[serde(transparent)]
        struct Pair(Sym, Sym);
    ))
    .expect_err("transparent tuple structs must have one field");
    serialize(&parse_quote!(
        #[serde(transparent)]
        struct Pair(Sym, Sym);
    ))
    .expect_err("serialize transparent tuple structs must have one field");
}

#[test]
fn rejects_transparent_tuple_fields_that_cannot_be_targets() {
    expand(&parse_quote!(
        #[serde(transparent)]
        struct T(#[serde(skip)] u32);
    ))
    .expect_err("skipped transparent tuple field is not a target");
    expand(&parse_quote!(
        #[serde(transparent)]
        struct T(#[serde(default)] u32);
    ))
    .expect_err("defaulted transparent tuple field is not a deserialize target");
    expand(&parse_quote!(
        #[serde(transparent)]
        struct T(::core::marker::PhantomData<u32>);
    ))
    .expect_err("PhantomData transparent tuple field is not a target");
}

#[test]
fn expands_tuple_container_default_from_path() {
    let output = compact(
        expand(&parse_quote! {
            #[serde(default = "make_default")]
            struct T(Sym);
        })
        .unwrap(),
    );
    assert!(output.contains("make_default()"));
    assert!(output.contains("__container_default.0"));
}

#[test]
fn expands_container_default_named_structs() {
    // `Default::default()`-based container default: the present field with no
    // own default pulls from the container default binding.
    let trait_default = compact(
        expand(&parse_quote! {
            #[serde(default)]
            struct T { present: Sym, #[serde(default = "make")] own: u32 }
        })
        .unwrap(),
    );
    assert!(trait_default.contains("__container_default"));
    assert!(trait_default.contains("asDefault") || trait_default.contains("Default>::default"));

    // `default = "path"`-based container default uses the named constructor.
    let path_default = compact(
        expand(&parse_quote! {
            #[serde(default = "make_default")]
            struct T { present: Sym }
        })
        .unwrap(),
    );
    assert!(path_default.contains("make_default()"));
}

#[test]
fn uses_container_rename_in_deserializer_entry_points() {
    let named = compact(
        expand(&parse_quote! {
            #[serde(rename = "Wire")]
            struct T { value: Sym }
        })
        .unwrap(),
    );
    assert!(named.contains("deserialize_struct(\"Wire\""));

    let tuple = compact(
        expand(&parse_quote! {
            #[serde(rename = "WireTuple")]
            struct T(Sym);
        })
        .unwrap(),
    );
    assert!(tuple.contains("deserialize_newtype_struct(\"WireTuple\""));
}

#[test]
fn expands_skipped_field_defaults() {
    // A skipped field with its own `default = "path"` uses that constructor;
    // one without falls back to `Default::default()`.
    let output = compact(
        expand(&parse_quote! {
            struct T {
                kept: Sym,
                #[serde(skip, default = "seven")]
                skipped_path: u32,
                #[serde(skip)]
                skipped_trait: u32,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("seven()"));
    assert!(output.contains("Default::default()"));
}

#[test]
fn expands_transparent_named_structs() {
    // Transparent: the single non-skipped field is deserialized directly and
    // the skipped companion is defaulted.
    let output = compact(
        expand(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: Sym,
                #[serde(skip)]
                tag: u32,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("DeserializeSeed::deserialize"));
    assert!(output.contains("Default::default()"));

    let defaulted = compact(
        expand(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: Sym,
                #[serde(default)]
                tag: u32,
            }
        })
        .unwrap(),
    );
    assert!(defaulted.contains("letinner="));

    let phantom = compact(
        expand(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: Sym,
                marker: ::core::marker::PhantomData<u32>,
            }
        })
        .unwrap(),
    );
    assert!(phantom.contains("letinner="));

    // More than one transparent field is rejected.
    expand(&parse_quote! {
        #[serde(transparent)]
        struct T { a: Sym, b: Sym }
    })
    .expect_err("transparent requires exactly one transparent field");
}

#[test]
fn expands_tuple_field_defaults() {
    // Tuple fields with defaults fill in on a short sequence; a skipped tuple
    // field is constructed from its default rather than the wire.
    let output = compact(
        expand(&parse_quote! {
            struct T(Sym, #[serde(default)] u32, #[serde(default = "seven")] u32, #[serde(skip)] u32);
        })
        .unwrap(),
    );
    assert!(output.contains("Default::default()"));
    assert!(output.contains("seven()"));
    assert!(output.contains("invalid_length"));
}

#[test]
fn rejects_required_tuple_field_after_default_without_container_default() {
    expand(&parse_quote! {
        struct T(#[serde(default)] u32, Sym);
    })
    .expect_err("serde rejects required tuple fields after defaulted fields");

    expand(&parse_quote! {
        #[serde(default)]
        struct T(#[serde(default)] u32, Sym);
    })
    .expect("container default makes later required tuple fields reachable");
}

#[test]
fn deserialize_tuple_builds_with_seed_defs() {
    let output = compact(
        expand(&parse_quote! {
            struct T(
                #[serde(deserialize_with = "my_de")]
                Sym,
                #[serde(with = "module")]
                Sym,
            );
        })
        .unwrap(),
    );
    assert!(output.contains("__InternityWithSeed0ForT"));
    assert!(output.contains("my_de(__d)"));
    assert!(output.contains("__InternityWithSeed1ForT"));
    assert!(output.contains("module::deserialize(__d)"));
}

#[test]
fn generated_helper_idents_are_collision_free_with_input_names() {
    // A custom `deserialize_with` function whose path collides with the
    // generated helper unit-struct name must not be shadowed: the helper is
    // renamed (trailing `_`) while the user's path is called unchanged.
    let output = compact(
        expand(&parse_quote! {
            struct T(#[serde(deserialize_with = "__InternityWithSeed0ForT")] Sym);
        })
        .unwrap(),
    );
    assert!(
        output.contains("struct__InternityWithSeed0ForT_;"),
        "generated seed struct should be renamed to avoid the collision: {output}"
    );
    assert!(
        output.contains("__InternityWithSeed0ForT(__d)"),
        "user's colliding function must still be invoked: {output}"
    );
}

#[test]
fn serialize_helper_idents_are_collision_free_with_input_names() {
    // Same guarantee on the serialization side for `serialize_with`.
    let output = compact(
        serialize(&parse_quote! {
            struct T(#[serde(serialize_with = "__InternitySerializeWith0ForT")] Sym);
        })
        .unwrap(),
    );
    assert!(
        output.contains("struct__InternitySerializeWith0ForT_"),
        "generated serialize adapter should be renamed to avoid the collision: {output}"
    );
    assert!(
        output.contains("__InternitySerializeWith0ForT(self.0,__serializer)"),
        "user's colliding function must still be invoked: {output}"
    );
}

#[test]
fn missing_value_expr_covers_every_fallback() {
    use super::{ContainerAttrs, DefaultValue, FieldAttrs, missing_value_expr};

    let ident: syn::Ident = parse_quote!(field);
    let binding = quote!(__container_default);
    let missing = quote!(return_missing);

    let no_container = ContainerAttrs::default();
    let di: DeriveInput = parse_quote!(
        #[serde(default)]
        struct S;
    );
    let with_container = parse_container(&di.attrs).unwrap();

    // Field with its own trait default.
    let own_trait = FieldAttrs {
        default: Some(DefaultValue::Trait),
        ..FieldAttrs::default()
    };
    assert!(compact(missing_value_expr(&ident, &own_trait, &no_container, &binding, &missing)).contains("Default::default()"));

    // Field with its own `default = "path"`.
    let own_path = FieldAttrs {
        default: Some(DefaultValue::Path(parse_quote!(make))),
        ..FieldAttrs::default()
    };
    assert!(compact(missing_value_expr(&ident, &own_path, &no_container, &binding, &missing)).contains("make()"));

    // No own default but a container default: pull from the container binding.
    let none = FieldAttrs::default();
    assert_eq!(
        compact(missing_value_expr(&ident, &none, &with_container, &binding, &missing)),
        "__container_default.field"
    );

    // No default anywhere: fall back to the hard-error token.
    assert_eq!(
        compact(missing_value_expr(&ident, &none, &no_container, &binding, &missing)),
        "return_missing"
    );
}

#[test]
fn codegen_pins_field_seed_and_with_definitions() {
    // A `deserialize_with` field emits its seed definition, whose body calls
    // the named function -- the only place that path token appears.
    let output = compact(
        expand(&parse_quote! {
            struct T {
                #[serde(deserialize_with = "myfn")]
                value: Sym,
            }
        })
        .unwrap(),
    );
    assert!(
        output.contains("myfn(__d)"),
        "with-seed body must invoke the named function: {output}"
    );
}

#[test]
fn codegen_container_default_binding_is_omitted_when_unneeded() {
    // Container default present, but every field either is skipped or carries
    // its own default: no field pulls from the container binding, so the
    // scratch binding must NOT be emitted.
    let output = compact(
        expand(&parse_quote! {
            #[serde(default)]
            struct T {
                #[serde(skip)]
                a: u32,
                #[serde(default = "mk")]
                b: Sym,
            }
        })
        .unwrap(),
    );
    assert!(
        !output.contains("__container_default"),
        "unneeded container default binding must be omitted: {output}"
    );
}

#[test]
fn codegen_transparent_targets_the_non_skipped_field() {
    let output = compact(
        expand(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: Sym,
                #[serde(skip)]
                tag: u32,
            }
        })
        .unwrap(),
    );
    assert!(
        output.contains("letinner="),
        "transparent must deserialize the non-skipped field: {output}"
    );
}

#[test]
fn codegen_wire_surface_excludes_skipped_fields() {
    let output = compact(
        expand(&parse_quote! {
            struct T {
                kept: Sym,
                #[serde(skip)]
                hidden: u32,
            }
        })
        .unwrap(),
    );
    // The wire-name string only appears for non-skipped fields.
    assert!(output.contains("\"kept\""), "kept field must appear on the wire surface: {output}");
}

#[test]
fn codegen_wire_surface_includes_aliases() {
    let output = compact(
        expand(&parse_quote! {
            struct T {
                #[serde(alias = "also", alias = "legacy")]
                kept: Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("\"also\""));
    assert!(output.contains("\"legacy\""));
}

#[test]
fn deserialize_named_honors_rename_all_and_serialize_only_adapter_names() {
    let output = compact(
        expand(&parse_quote! {
            #[serde(rename_all = "camelCase")]
            struct T {
                #[serde(serialize_with = "my_ser")]
                first_field: Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("\"firstField\""));
}

#[test]
fn transparent_named_accepts_non_path_targets() {
    let output = compact(
        expand(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: &'static Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("letinner="));
}

#[test]
fn codegen_ignore_variant_and_arm_track_deny_unknown_fields() {
    let lenient = compact(
        expand(&parse_quote! {
            struct T { a: Sym }
        })
        .unwrap(),
    );
    // The field enum carries the `__ignore` catch-all variant...
    assert!(
        lenient.contains("{__field0,__ignore,}"),
        "lenient field enum must declare __ignore: {lenient}"
    );
    // ...and the map visitor drains unknown values through IgnoredAny.
    assert!(
        lenient.contains("IgnoredAny"),
        "lenient map arm must ignore unknown values: {lenient}"
    );

    let strict = compact(
        expand(&parse_quote! {
            #[serde(deny_unknown_fields)]
            struct T { a: Sym }
        })
        .unwrap(),
    );
    assert!(
        !strict.contains("__ignore"),
        "deny_unknown_fields must not emit any __ignore handling: {strict}"
    );
}

#[test]
fn codegen_named_sequence_indices_increment() {
    let output = compact(
        expand(&parse_quote! {
            struct T { a: Sym, b: Sym }
        })
        .unwrap(),
    );
    // The second field's missing-in-sequence error reports index 1...
    assert!(
        output.contains("invalid_length(1,&self)"),
        "sequence index must advance to 1: {output}"
    );
    // ...and the trailing-element guard reports one past the arity (2 + 1).
    assert!(
        output.contains("invalid_length(3,&self)"),
        "trailing guard must report arity + 1: {output}"
    );
}

#[test]
fn codegen_tuple_sequence_indices_increment() {
    let output = compact(
        expand(&parse_quote! {
            struct T(Sym, Sym);
        })
        .unwrap(),
    );
    assert!(
        output.contains("invalid_length(1,&self)"),
        "tuple sequence index must advance to 1: {output}"
    );
    assert!(
        output.contains("invalid_length(3,&self)"),
        "tuple trailing guard must report arity + 1: {output}"
    );
}

#[test]
fn codegen_single_skipped_tuple_field_is_not_a_newtype() {
    let output = compact(
        expand(&parse_quote! {
            struct T(#[serde(skip)] u32);
        })
        .unwrap(),
    );
    assert!(
        !output.contains("visit_newtype_struct"),
        "a lone skipped field is not a newtype: {output}"
    );
}

#[test]
fn serializes_named_tuple_newtype_and_unit_structs() {
    let named = compact(
        serialize(&parse_quote! {
            struct Named {
                symbol: Sym,
                #[internity(via_serde)]
                count: u32,
            }
        })
        .unwrap(),
    );
    assert!(named.contains("serialize_struct"));
    assert!(named.contains("SerializeInWith::new(&self.symbol,__reader)"));
    assert!(named.contains("&&self.count"));

    let tuple = compact(
        serialize(&parse_quote! {
            struct Pair(Sym, u32);
        })
        .unwrap(),
    );
    assert!(tuple.contains("serialize_tuple_struct"));
    assert!(tuple.contains("SerializeTupleStruct::serialize_field"));

    let newtype = compact(
        serialize(&parse_quote! {
            struct Newtype(Sym);
        })
        .unwrap(),
    );
    assert!(newtype.contains("serialize_newtype_struct"));
    assert!(!newtype.contains("serialize_tuple_struct"));

    let unit = compact(
        serialize(&parse_quote! {
            struct Unit;
        })
        .unwrap(),
    );
    assert!(unit.contains("serialize_unit_struct"));
}

#[test]
fn serialize_honors_renames_and_skips() {
    let output = compact(
        serialize(&parse_quote! {
            #[serde(rename = "Wire", rename_all(serialize = "kebab-case", deserialize = "snake_case"))]
            struct T {
                first_field: Sym,
                #[serde(rename(serialize = "renamed", deserialize = "read_name"))]
                second_field: Sym,
                #[serde(skip_serializing)]
                hidden: Sym,
                #[serde(skip_deserializing)]
                write_only: Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("serialize_struct(__serializer,\"Wire\",3"));
    assert!(output.contains("\"first-field\""));
    assert!(output.contains("\"renamed\""));
    assert!(output.contains("\"write-only\""));
    assert!(!output.contains("\"hidden\""));
    assert!(!output.contains("\"read_name\""));
}

#[test]
fn serialize_named_honors_combined_rename_all() {
    let output = compact(
        serialize(&parse_quote! {
            #[serde(rename_all = "camelCase")]
            struct T {
                first_field: Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("\"firstField\""));
}

#[test]
fn serialize_transparent_delegates_to_the_target_field() {
    let output = compact(
        serialize(&parse_quote! {
            #[serde(transparent)]
            struct T {
                inner: Sym,
                marker: ::core::marker::PhantomData<u32>,
                #[serde(skip_serializing)]
                hidden: u32,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("SerializeIn::serialize_in(&self.inner,__reader,__serializer)"));

    serialize(&parse_quote! {
        #[serde(transparent)]
        struct T {
            inner: Sym,
            #[serde(default)]
            extra: u32,
        }
    })
    .expect_err("serde Serialize rejects a defaulted non-skipped transparent companion");
}

#[test]
fn serialize_transparent_direct_call_modes_are_covered() {
    let via_serde = compact(
        serialize(&parse_quote! {
            #[serde(transparent)]
            struct T {
                #[internity(via_serde)]
                inner: Sym,
            }
        })
        .unwrap(),
    );
    assert!(via_serde.contains("Serialize::serialize(&self.inner,__serializer)"));

    let custom = compact(
        serialize(&parse_quote! {
            #[serde(transparent)]
            struct T {
                #[serde(serialize_with = "my_ser")]
                inner: Sym,
            }
        })
        .unwrap(),
    );
    assert!(custom.contains("my_ser(&self.inner,__serializer)"));

    let tuple = compact(
        serialize(&parse_quote! {
            #[serde(transparent)]
            struct T(Sym);
        })
        .unwrap(),
    );
    assert!(tuple.contains("SerializeIn::serialize_in(&self.0,__reader,__serializer)"));
}

#[test]
fn serialize_transparent_tuple_rejects_non_targets() {
    serialize(&parse_quote!(
        #[serde(transparent)]
        struct T(#[serde(skip_serializing)] u32);
    ))
    .expect_err("skip_serializing transparent tuple field is not a target");
    serialize(&parse_quote!(
        #[serde(transparent)]
        struct T(::core::marker::PhantomData<u32>);
    ))
    .expect_err("PhantomData transparent tuple field is not a target");
}

#[test]
fn serialize_uses_custom_serializer_adapters() {
    let output = compact(
        serialize(&parse_quote! {
            struct T {
                #[serde(serialize_with = "my_ser")]
                value: Sym,
                #[serde(with = "module")]
                other: Sym,
            }
        })
        .unwrap(),
    );
    assert!(output.contains("struct__InternitySerializeWith0ForT"));
    assert!(output.contains("my_ser(self.0,__serializer)"));
    assert!(output.contains("module::serialize(self.0,__serializer)"));
    assert!(output.contains("__InternitySerializeWith0ForT(&self.value)"));

    let tuple = compact(
        serialize(&parse_quote! {
            struct T(
                #[serde(serialize_with = "my_ser")]
                Sym,
                Sym,
            );
        })
        .unwrap(),
    );
    assert!(tuple.contains("__InternitySerializeWith0ForT"));
    assert!(tuple.contains("my_ser(self.0,__serializer)"));
    assert!(tuple.contains("__InternitySerializeWith0ForT(&self.0)"));

    let tuple_with = compact(
        serialize(&parse_quote! {
            struct T(
                #[serde(with = "module")]
                Sym,
                Sym,
            );
        })
        .unwrap(),
    );
    assert!(tuple_with.contains("__InternitySerializeWith0ForT"));
    assert!(tuple_with.contains("module::serialize(self.0,__serializer)"));
}

#[test]
fn public_entry_points_return_compile_errors_for_bad_input() {
    let root = default_root();
    let bad: proc_macro2::TokenStream = quote!(
        enum E {
            A,
        }
    );
    let deserialize = compact(super::derive_deserialize_in(bad.clone(), &root));
    let serialize = compact(super::derive_serialize_in(bad, &root));
    assert!(deserialize.contains("compile_error!"));
    assert!(serialize.contains("compile_error!"));
}

#[test]
fn transparent_named_struct_fills_its_non_target_siblings() {
    // A transparent struct deserializes its one real field and fills every
    // *other* field from a default. The target must be excluded from that
    // "others" set (via `!core::ptr::eq`); dropping the `!` would fill the
    // target twice and never mention the sibling.
    let input: DeriveInput = parse_quote! {
        #[serde(transparent)]
        struct Wrapper {
            inner: Sym,
            #[serde(skip)]
            marker: u32,
        }
    };
    let output = compact(expand(&input).unwrap());
    assert!(output.contains("marker"), "the skipped sibling must be filled: {output}");
}

#[test]
fn tuple_field_default_does_not_bind_the_container_default() {
    // With a container `default`, only *required* fields (not skipped, no own
    // default) force the container-default binding. A field carrying its own
    // `default` is not required, so no `__container_default` is emitted.
    // Guards the inner `&&` of that required-field predicate.
    let input: DeriveInput = parse_quote! {
        #[serde(default)]
        struct T(#[serde(default = "make")] Sym);
    };
    let output = compact(expand(&input).unwrap());
    assert!(
        !output.contains("__container_default"),
        "a field-defaulted tuple must not bind the container default: {output}"
    );
}

#[test]
fn serialize_tuple_with_a_skipped_field_stays_a_tuple_struct() {
    // A two-field tuple with one `skip_serializing` field has `plans.len() ==
    // 2` but `field_count == 1`; it must serialize as a tuple struct, not
    // collapse to a newtype. Guards the `&&` in the newtype predicate.
    let input: DeriveInput = parse_quote! {
        struct Pair(Sym, #[serde(skip_serializing)] u32);
    };
    let output = compact(serialize(&input).unwrap());
    assert!(
        output.contains("serialize_tuple_struct"),
        "must serialize as a tuple struct: {output}"
    );
    assert!(
        !output.contains("serialize_newtype_struct"),
        "must not collapse to a newtype: {output}"
    );
}