alef 0.94.0

Opinionated polyglot binding generator for Rust libraries
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
use super::*;
use crate::core::ir::{EnumDef, EnumVariant, FieldDef, TypeDef, TypeRef};

#[test]
fn untagged_record_enum_retains_native_identity_and_typed_factories() {
    let def = EnumDef {
        name: "Record".into(),
        serde_untagged: true,
        variants: vec![
            EnumVariant {
                name: "Canonical".into(),
                is_tuple: true,
                fields: vec![FieldDef {
                    name: "_0".into(),
                    ty: TypeRef::Named("Payload".into()),
                    ..Default::default()
                }],
                ..Default::default()
            },
            EnumVariant {
                name: "Migration".into(),
                is_tuple: true,
                fields: vec![FieldDef {
                    name: "_0".into(),
                    ty: TypeRef::Named("Legacy".into()),
                    ..Default::default()
                }],
                ..Default::default()
            },
        ],
        ..Default::default()
    };
    let code = gen_enum_with_module(&def, "core", None, &[], "Example");
    assert!(code.contains("#[magnus::wrap(class = \"Example::Record\")]"), "{code}");
    assert!(code.contains("pub fn from_canonical(value: Payload) -> Self"), "{code}");
    assert!(code.contains("pub fn migration(&self) -> Option<Legacy>"), "{code}");
    assert!(
        !code.contains("json_to_ruby"),
        "records must not degrade into hashes: {code}"
    );
    assert!(
        !code.contains("serde_json::from_str"),
        "records must not guess a variant from a hash: {code}"
    );
    syn::parse_file(&code).expect("native enum Rust must parse");
}

#[test]
fn boxed_native_payload_enum_preserves_boxed_variants_and_unboxed_accessors() {
    let def = EnumDef {
        name: "Record".into(),
        serde_untagged: true,
        variants: vec![EnumVariant {
            name: "Canonical".into(),
            is_tuple: true,
            fields: vec![FieldDef {
                name: "_0".into(),
                ty: TypeRef::Named("Payload".into()),
                is_boxed: true,
                ..Default::default()
            }],
            ..Default::default()
        }],
        ..Default::default()
    };

    let code = gen_enum_with_module(&def, "core", None, &[], "Example");
    assert!(code.contains("Canonical(Box<Payload>)"), "{code}");
    assert!(code.contains("from_canonical(value: Payload)"), "{code}");
    assert!(code.contains("Self::Canonical(Box::new(value))"), "{code}");
    assert!(code.contains("Some((**value).clone())"), "{code}");
    syn::parse_file(&code).expect("boxed native enum Rust must parse");
}

#[test]
fn typed_tagged_newtypes_extract_native_payloads_before_json_fallback() {
    let mut def = EnumDef {
        name: "Policy".into(),
        serde_tag: Some("operation".into()),
        serde_content: Some("policy".into()),
        variants: vec![EnumVariant {
            name: "Analyze".into(),
            is_tuple: true,
            fields: vec![FieldDef {
                name: "_0".into(),
                ty: TypeRef::Named("AnalyzePolicy".into()),
                ..Default::default()
            }],
            ..Default::default()
        }],
        ..Default::default()
    };
    let types = vec![TypeDef {
        name: "AnalyzePolicy".into(),
        ..Default::default()
    }];
    let native_code = gen_enum_with_module(&def, "core", None, &types, "CustomModule");
    assert!(
        native_code.contains("#[magnus::wrap(class = \"CustomModule::Policy\")]"),
        "{native_code}"
    );
    assert!(
        native_code.contains("pub fn from_analyze(value: AnalyzePolicy) -> Self"),
        "{native_code}"
    );
    assert!(
        native_code.contains("pub fn analyze(&self) -> Option<AnalyzePolicy>"),
        "{native_code}"
    );
    assert!(!native_code.contains("json_to_ruby"), "{native_code}");
    syn::parse_file(&native_code).expect("tagged payloads must use valid native enum Rust");
    // Mixed unit/payload enums retain the legacy representation and input adapter.
    def.variants.push(EnumVariant {
        name: "None".into(),
        ..Default::default()
    });
    let code = gen_enum_with_module(&def, "core", None, &types, "CustomModule");
    assert!(code.contains("Some(\"CustomModule::PolicyAnalyze\")"), "{code}");
    assert!(
        code.contains("let payload: AnalyzePolicy = val.funcall(\"value\", ())?;"),
        "{code}"
    );
    assert!(code.contains("return Ok(Self::Analyze(payload));"), "{code}");
    assert!(code.find("let payload:").unwrap() < code.find("let json_str:").unwrap());
    syn::parse_file(&code).expect("native ingress conversion must be valid Rust");
}

fn make_field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
    FieldDef {
        version: Default::default(),
        name: name.to_string(),
        ty,
        optional,
        default: None,
        doc: String::new(),
        sanitized: false,
        is_boxed: false,
        type_rust_path: None,
        cfg: None,
        typed_default: None,
        core_wrapper: crate::core::ir::CoreWrapper::None,
        vec_inner_core_wrapper: crate::core::ir::CoreWrapper::None,
        newtype_wrapper: None,
        serde_rename: None,
        serde_flatten: false,
        serde_with: None,
        serde_skip_serializing_if: false,
        serde_skip: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        original_type: None,
    }
}

fn make_typedef(name: &str, fields: Vec<FieldDef>) -> TypeDef {
    TypeDef {
        name: name.to_string(),
        rust_path: format!("test_lib::{name}"),
        original_rust_path: String::new(),
        fields,
        methods: vec![],
        is_opaque: false,
        is_clone: true,
        is_copy: false,
        is_trait: false,
        has_default: false,
        has_stripped_cfg_fields: false,
        is_return_type: false,
        serde_rename_all: None,
        has_serde: false,
        serde_container_default: false,
        serde_container_conversion: Default::default(),
        super_traits: vec![],
        doc: String::new(),
        cfg: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_variant_wrapper: false,
        has_lifetime_params: false,
        has_private_fields: false,
        version: Default::default(),
    }
}

#[test]
fn explicit_default_impl_preserves_serde_default_fn_instead_of_type_zero_value() {
    // Regression: this generator exists *because* a struct has field-level defaults that differ
    // from the derived Default, yet it called the context-free `default_value_for_field` and so
    // emitted `Default::default()` for `#[serde(default = "path")]` fields — the exact value it
    // was written to avoid. Against html-to-markdown's real `GridCell` that shipped
    // `GridCell.default().row_span == 0` while `default_span()` returns 1, and the kwargs
    // constructor in the same generated file returned the correct 1: two different defaults for
    // one field. ~keep
    let mut span = make_field(
        "row_span",
        TypeRef::Primitive(crate::core::ir::PrimitiveType::U32),
        false,
    );
    span.typed_default = Some(crate::core::ir::DefaultValue::FunctionCall("default_span".to_string()));

    let mut typ = make_typedef(
        "GridCell",
        vec![
            make_field("content", TypeRef::String, false),
            make_field("row", TypeRef::Primitive(crate::core::ir::PrimitiveType::U32), false),
            span,
        ],
    );
    typ.has_serde = true;

    let map_fn = |ty: &TypeRef| match ty {
        TypeRef::String => "String".to_string(),
        _ => "u32".to_string(),
    };

    let output = gen_struct_default_impl_explicit(&typ, &map_fn, &[], &std::collections::HashSet::default())
        .expect("a struct with a field-level default must get an explicit Default impl");

    assert!(
        !output.contains("row_span: Default::default()"),
        "row_span must not fall back to the type's zero value, which is not `default_span()`:\n{output}"
    );
    assert!(
        output.contains("serde_json::from_str::<test_lib::GridCell>"),
        "row_span must recover the real serde default by deserializing a stub:\n{output}"
    );
}

/// Regression: a struct with one field carrying a real default (`element_id`, via
/// `#[serde(default)]`) and a second, unrelated *required* field of a `Named` type that has no
/// `Default` impl of its own (mirrors `xberg::Element.metadata: ElementMetadata`, where
/// `ElementMetadata` derives no `Default`). Before the fix, `has_non_trivial_default` being true
/// because of `element_id` alone was enough to synthesize a whole-struct Default impl, and the
/// untyped fallback table blindly emitted `ElementMetadata::default()` for the `metadata` field —
/// code that fails to compile with "no function or associated item named `default` found for
/// struct `ElementMetadata`" because `ElementMetadata` never implements `Default`. The fix must
/// recognize this and skip the whole impl rather than emit a call the type cannot satisfy.
#[test]
fn gen_struct_default_impl_explicit_skips_struct_with_undefaultable_required_field() {
    let mut element_id = make_field("element_id", TypeRef::String, false);
    element_id.typed_default = Some(crate::core::ir::DefaultValue::Empty);

    let typ = make_typedef(
        "Element",
        vec![
            element_id,
            make_field("metadata", TypeRef::Named("ElementMetadata".to_string()), false),
        ],
    );

    let map_fn = |ty: &TypeRef| match ty {
        TypeRef::String => "String".to_string(),
        TypeRef::Named(name) => name.clone(),
        _ => "()".to_string(),
    };

    // `ElementMetadata` is deliberately absent from `types_with_default`: it has no `Default`
    // impl in the source, matching the real xberg struct.
    let output = gen_struct_default_impl_explicit(&typ, &map_fn, &[], &std::collections::HashSet::default());
    assert!(
        output.is_none(),
        "a struct with a required field whose type has no Default must not get a synthesized \
         Default impl: {output:?}"
    );
}

/// Positive control for the above: same shape, but `ElementMetadata` IS in `types_with_default`
/// (it derives `Default` in this scenario). The Default impl must still be generated, and the
/// `metadata` field must still call `ElementMetadata::default()` — proving the fix is a real
/// per-type lookup, not a blanket refusal to ever call `{Type}::default()`.
#[test]
fn gen_struct_default_impl_explicit_still_calls_default_when_field_type_has_one() {
    let mut element_id = make_field("element_id", TypeRef::String, false);
    element_id.typed_default = Some(crate::core::ir::DefaultValue::Empty);

    let typ = make_typedef(
        "Element",
        vec![
            element_id,
            make_field("metadata", TypeRef::Named("ElementMetadata".to_string()), false),
        ],
    );

    let map_fn = |ty: &TypeRef| match ty {
        TypeRef::String => "String".to_string(),
        TypeRef::Named(name) => name.clone(),
        _ => "()".to_string(),
    };

    let mut types_with_default = std::collections::HashSet::default();
    types_with_default.insert("ElementMetadata");

    let output = gen_struct_default_impl_explicit(&typ, &map_fn, &[], &types_with_default)
        .expect("a struct whose required field type has Default must still get an impl");
    assert!(
        output.contains("metadata: ElementMetadata::default()"),
        "metadata must still call the field type's own default(): {output}"
    );
}

#[test]
fn gen_enum_unit_variants_emit_ruby_symbols() {
    let enum_def = EnumDef {
        name: "Status".to_string(),
        rust_path: "test_lib::Status".to_string(),
        original_rust_path: String::new(),
        variants: vec![
            EnumVariant {
                serde_untagged: false,
                name: "Pending".to_string(),
                fields: vec![],
                doc: String::new(),
                is_default: false,
                serde_rename: None,
                binding_excluded: false,
                binding_exclusion_reason: None,
                is_tuple: false,
                originally_had_data_fields: false,
                cfg: None,
                version: Default::default(),
            },
            EnumVariant {
                serde_untagged: false,
                name: "Done".to_string(),
                fields: vec![],
                doc: String::new(),
                is_default: false,
                serde_rename: None,
                binding_excluded: false,
                binding_exclusion_reason: None,
                is_tuple: false,
                originally_had_data_fields: false,
                cfg: None,
                version: Default::default(),
            },
        ],
        methods: vec![],
        doc: String::new(),
        cfg: None,
        is_copy: false,
        has_serde: false,
        has_default: false,
        serde_content: None,
        serde_tag: None,
        serde_untagged: false,
        serde_rename_all: None,
        rename_all_fields: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        version: Default::default(),
    };
    let code = gen_enum(&enum_def, "test_lib", None, &[]);
    assert!(code.contains("enum Status"), "must emit enum definition");
    assert!(
        code.contains("PartialEq, Eq, Hash"),
        "unit enums must support Rust map keys: {code}"
    );
    assert!(code.contains("to_symbol"), "unit enums use Ruby symbols");
    assert!(
        code.contains("Status::Pending => \"Pending\","),
        "no rename_all declared, so the IntoValue output is the verbatim wire value:\n{code}"
    );
}

/// Serde's real default (no `#[serde(rename_all = "...")]` on the enum) serializes unit variants
/// verbatim under their Rust name, e.g. `"KeyValue"` — not snake_cased. Regression for a Magnus
/// `IntoValue` impl that unconditionally snake_cased every unit variant regardless of the
/// enum's actual serde attributes, so a Ruby caller comparing the returned Symbol against a
/// real JSON payload (e.g. `"kind": "KeyValue"`) never matched.
#[test]
fn gen_enum_unit_variant_wire_value_is_verbatim_without_rename_all() {
    let enum_def = EnumDef {
        name: "DataNodeKind".to_string(),
        rust_path: "test_lib::DataNodeKind".to_string(),
        variants: vec![make_variant("KeyValue", vec![]), make_variant("Sequence", vec![])],
        ..Default::default()
    };
    let code = gen_enum(&enum_def, "test_lib", None, &[]);
    assert!(
        code.contains("DataNodeKind::KeyValue => \"KeyValue\","),
        "serde's real default (no rename_all) serializes unit variants verbatim, not snake_cased:\n{code}"
    );
    assert!(
        !code.contains("=> \"key_value\","),
        "must not fabricate a snake_case wire value the Rust enum never declared:\n{code}"
    );
}

/// Widening the `IntoValue` output to the real wire value must not narrow what `TryConvert`
/// accepts on input — existing consumer code passing the old always-snake_case symbol
/// (`:key_value`) must keep working alongside the new verbatim wire spelling (`"KeyValue"`).
#[test]
fn gen_enum_unit_variant_try_convert_still_accepts_the_legacy_snake_case_spelling() {
    let enum_def = EnumDef {
        name: "DataNodeKind".to_string(),
        rust_path: "test_lib::DataNodeKind".to_string(),
        variants: vec![make_variant("KeyValue", vec![])],
        ..Default::default()
    };
    let code = gen_enum(&enum_def, "test_lib", None, &[]);
    assert!(code.contains("magnus::Symbol::from_value(val)"));
    assert!(code.contains("symbol.name()?.into_owned()"));
    assert!(!code.contains("funcall(\"to_s\""));
    assert!(
        code.contains("\"key_value\""),
        "existing consumer code passing the old snake_case symbol must keep working:\n{code}"
    );
    assert!(
        code.contains("\"KeyValue\""),
        "the new verbatim wire value must also be accepted on input:\n{code}"
    );
}

fn make_variant(name: &str, fields: Vec<FieldDef>) -> EnumVariant {
    EnumVariant {
        serde_untagged: false,
        name: name.to_string(),
        fields,
        doc: String::new(),
        is_default: false,
        serde_rename: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        is_tuple: false,
        originally_had_data_fields: false,
        cfg: None,
        version: Default::default(),
    }
}

fn make_data_enum(name: &str, serde_tag: Option<&str>) -> EnumDef {
    EnumDef {
        name: name.to_string(),
        rust_path: format!("test_lib::{name}"),
        original_rust_path: String::new(),
        variants: vec![
            make_variant("Png", vec![]),
            make_variant("Jpeg", vec![make_field("quality", TypeRef::String, false)]),
        ],
        methods: vec![],
        doc: String::new(),
        cfg: None,
        is_copy: false,
        has_serde: true,
        has_default: false,
        serde_content: None,
        serde_tag: serde_tag.map(str::to_string),
        serde_untagged: false,
        serde_rename_all: None,
        rename_all_fields: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        version: Default::default(),
    }
}

#[test]
fn gen_enum_wraps_string_for_internally_tagged_enum() {
    // For an internally-tagged enum (`#[serde(tag = "...")]`), serde cannot deserialize a bare
    let code = gen_enum(
        &make_data_enum("ImageOutputFormat", Some("type")),
        "test_lib",
        None,
        &[],
    );
    assert!(
        code.contains(r#".or_else(|_| serde_json::from_value(serde_json::json!({ "type": json_str })))"#),
        "expected tagged string wrap for internally-tagged enum: {code}"
    );
}

#[test]
fn gen_enum_keeps_bare_string_for_externally_tagged_enum() {
    // An externally-tagged data enum (no `#[serde(tag)]`) must not gain the tag-wrap branch.
    let code = gen_enum(&make_data_enum("ExternallyTagged", None), "test_lib", None, &[]);
    assert!(
        !code.contains("impl Default"),
        "required data enums must not get invented defaults: {code}"
    );
    assert!(
        !code.contains("serde_json::from_value(serde_json::json!({"),
        "externally-tagged enum must not wrap the string in a tag object: {code}"
    );
    assert!(
        code.contains("serde_json::from_str(&json_str)"),
        "data enum must keep the from_str path: {code}"
    );
}

#[test]
fn gen_enum_emits_adjacent_serde_representation() {
    let mut enum_def = make_data_enum("OperationResult", Some("type"));
    enum_def.serde_content = Some("output".to_string());
    enum_def.variants[1].is_tuple = true;
    enum_def.variants[1].fields[0].name = "_0".to_string();

    let code = gen_enum(&enum_def, "test_lib", None, &[]);

    assert!(code.contains(r#"#[serde(tag = "type", content = "output")]"#));
    assert!(code.contains("Jpeg(String)"));
    assert!(code.contains("Self::Jpeg(_0) => Some(_0)"), "{code}");
    assert!(!code.contains("Self::Jpeg { _0 }"), "{code}");
    syn::parse_file(&code).unwrap_or_else(|error| panic!("generated Rust must parse: {error}\n{code}"));
}

#[test]
fn adjacent_tuple_default_uses_tuple_constructor_syntax() {
    let mut enum_def = make_data_enum("OperationResult", Some("type"));
    enum_def.has_default = true;
    enum_def.serde_content = Some("output".to_string());
    enum_def.variants[1].is_tuple = true;
    enum_def.variants[1].is_default = true;
    enum_def.variants[1].fields[0].name = "_0".to_string();

    let code = gen_enum(&enum_def, "test_lib", None, &[]);

    assert!(code.contains("Self::Jpeg(Default::default())"), "{code}");
    assert!(!code.contains("Self::Jpeg { _0:"), "{code}");
    syn::parse_file(&code).unwrap_or_else(|error| panic!("generated Rust must parse: {error}\n{code}"));
}

/// `EntityCategory`/`OutputFormat`/`PiiCategory` (real xberg consumer enums) are
/// externally-tagged (`#[serde(rename_all = "snake_case")]`, no `tag`/`content`, not
/// `#[serde(untagged)]`) unit variants plus one newtype `Custom(String)` variant. A tuple
/// variant is modeled in the IR with BOTH `is_tuple: true` AND fields named `_0`, `_1`, ... --
/// set together by the extractor -- so this fixture sets both, matching an IR state the
/// extractor can actually produce (a fixture setting only the field name would model a
/// named-fields variant whose field happens to be called `_0`, a shape serde never emits).
///
/// Regression: this used to emit STRUCT form for the newtype variant (`Custom { _0: String }`),
/// so `serde_json::to_value` produced `{"custom": {"_0": "foo"}}` -- an extra level of nesting
/// versus core's own wire `{"custom": "foo"}` (confirmed against
/// `crates/xberg/src/types/entity.rs`'s `EntityCategory::Custom(String)` in the consumer repo),
/// with `_0` as the most visible symptom. `variant_emits_tuple_form`
/// (`codegen/conversions/helpers/eligibility.rs`) now emits TUPLE form for externally-tagged
/// newtype variants too, matching core's wire exactly with no field name on the wire at all.
#[test]
fn gen_enum_emits_tuple_form_matching_core_wire_for_externally_tagged_newtype_variant() {
    let mut enum_def = make_data_enum("EntityCategory", None);
    enum_def.variants[1].name = "Custom".to_string();
    enum_def.variants[1].is_tuple = true;
    enum_def.variants[1].fields[0].name = "_0".to_string();

    let code = gen_enum(&enum_def, "test_lib", None, &[]);

    assert!(
        code.contains("Custom(String)"),
        "a single-field newtype variant under default (externally-tagged) serde representation \
         must emit tuple form, matching core's own `Custom(String)` declaration and its \
         `{{\"custom\": \"foo\"}}` wire -- struct form would add a nesting level core's wire \
         does not have:\n{code}"
    );
    assert!(
        !code.contains("_0"),
        "tuple form carries no field name at all, so the synthesized positional name `_0` must \
         not reach the generated code in any form:\n{code}"
    );
    syn::parse_file(&code).unwrap_or_else(|error| panic!("generated Rust must parse: {error}\n{code}"));
}

/// The one shape `positional_field_serde_rename` still guards after the predicate widening
/// above: an INTERNALLY-tagged (`tag` set, no `content`) newtype variant whose payload type is
/// `Named` and serde would flatten at runtime, but whose type definition is not present in the
/// `types` slice `gen_enum` received (passing `&[]` here, as if the payload type's `TypeDef`
/// were simply unreachable from this call site). `serde_flattens_newtype_payload` then falls
/// back to `false`, so this generator keeps struct form -- correctly, since internally-tagged
/// newtype variants have no positional slot for tuple form to fill -- and the field still needs
/// a semantic rename rather than the bare synthesized `_0`. Mirrors
/// `backends::kotlin::gen_bindings::shared::kotlin_field_name_with_type`'s type-derived naming.
#[test]
fn gen_enum_renames_positional_field_for_internally_tagged_variant_with_unreachable_payload_type() {
    let mut enum_def = make_data_enum("DocumentSource", Some("type"));
    enum_def.variants[1].name = "Pdf".to_string();
    enum_def.variants[1].is_tuple = true;
    enum_def.variants[1].fields[0] = make_field("_0", TypeRef::Named("PdfMetadata".to_string()), false);

    let code = gen_enum(&enum_def, "test_lib", None, &[]);

    assert!(
        code.contains("Pdf { "),
        "internally-tagged must stay struct form even though the variant is a tuple in the IR:\n{code}"
    );
    assert!(
        code.contains(r#"#[serde(rename = "metadata")] _0: PdfMetadata"#),
        "a `Pdf(PdfMetadata)` payload should derive `metadata`, not the generic `value`:\n{code}"
    );
    syn::parse_file(&code).unwrap_or_else(|error| panic!("generated Rust must parse: {error}\n{code}"));
}

/// A multi-field tuple variant (`_0`, `_1`, ...) under externally-tagged representation now also
/// gets tuple form from the widened predicate, so neither field name reaches the wire at all --
/// the fix for the historical `_1`-never-renamed gap (`tagged_enums.rs` matched only the literal
/// `"_0"`, never `"_1"`) is that tuple form carries no field names to begin with.
#[test]
fn gen_enum_emits_tuple_form_for_multi_field_externally_tagged_variant() {
    let mut enum_def = make_data_enum("Pair", None);
    enum_def.variants[1].name = "Range".to_string();
    enum_def.variants[1].is_tuple = true;
    enum_def.variants[1].fields = vec![
        make_field("_0", TypeRef::Primitive(crate::core::ir::PrimitiveType::U32), false),
        make_field("_1", TypeRef::Primitive(crate::core::ir::PrimitiveType::U32), false),
    ];

    let code = gen_enum(&enum_def, "test_lib", None, &[]);

    assert!(
        code.contains("Range(u32, u32)"),
        "a multi-field tuple variant under externally-tagged representation must emit tuple \
         form:\n{code}"
    );
    assert!(
        !code.contains("_0") && !code.contains("_1"),
        "tuple form carries no field names, so neither synthesized positional name may reach \
         the generated code:\n{code}"
    );
    syn::parse_file(&code).unwrap_or_else(|error| panic!("generated Rust must parse: {error}\n{code}"));
}

#[test]
fn gen_struct_emits_magnus_wrap_attribute() {
    let typ = make_typedef("Config", vec![make_field("value", TypeRef::String, false)]);
    let mapper = crate::backends::magnus::type_map::MagnusMapper;
    let code = gen_struct(&typ, &mapper, "TestLib", "test_lib", false, &[], false);
    assert!(code.contains("magnus::wrap"), "struct must have magnus::wrap");
    assert!(code.contains("struct Config"), "must emit struct Config");
}

fn container_conversion() -> crate::core::ir::SerdeContainerConversion {
    crate::core::ir::SerdeContainerConversion {
        from: Some("(f64, f64)".to_string()),
        into: Some("(f64, f64)".to_string()),
        try_from: None,
        transparent: false,
    }
}

/// Wire-shape class: a two-field primitive pair (e.g. `Point { x, y }` with a positional-array
/// `#[serde(from/into)]`). Asserts on the actual rendered code, not an intermediate flag.
#[test]
fn gen_struct_delegates_deserialize_when_caller_confirms_eligibility() {
    let mut typ = make_typedef(
        "Point",
        vec![
            make_field("x", TypeRef::Primitive(crate::core::ir::PrimitiveType::F64), false),
            make_field("y", TypeRef::Primitive(crate::core::ir::PrimitiveType::F64), false),
        ],
    );
    typ.serde_container_conversion = container_conversion();
    let mapper = crate::backends::magnus::type_map::MagnusMapper;
    let code = gen_struct(&typ, &mapper, "TestLib", "test_lib", false, &[], true);

    let derive_line = code.lines().find(|l| l.trim_start().starts_with("#[derive(")).unwrap();
    assert!(
        !derive_line.contains("serde::Deserialize"),
        "derive must drop Deserialize when delegating: {derive_line}"
    );
    assert!(
        derive_line.contains("serde::Serialize"),
        "Serialize stays derived: {derive_line}"
    );
    assert!(
        code.contains("impl<'de> serde::Deserialize<'de> for Point {"),
        "expected a delegating Deserialize impl in: {code}"
    );
    assert!(
        code.contains("<test_lib::Point as serde::Deserialize>::deserialize(deserializer).map(Into::into)"),
        "delegating impl must read the core type: {code}"
    );
    // The pre-existing TryConvert bridge is untouched and picks up whichever Deserialize impl
    // the type has -- verify it still calls through to `Self`, not a hand-picked shape.
    assert!(code.contains("serde_json::from_str::<Point>(&json_str)"));
}

/// Caller did not confirm eligibility (e.g. no matching `From<core::Type>` this run) -- must
/// keep the ordinary derived, field-by-field `Deserialize` so the existing
/// `SerdeContainerConversionUnsupported` diagnostic keeps naming the real gap.
#[test]
fn gen_struct_keeps_derive_when_delegation_not_confirmed() {
    let mut typ = make_typedef(
        "Point",
        vec![
            make_field("x", TypeRef::Primitive(crate::core::ir::PrimitiveType::F64), false),
            make_field("y", TypeRef::Primitive(crate::core::ir::PrimitiveType::F64), false),
        ],
    );
    typ.serde_container_conversion = container_conversion();
    let mapper = crate::backends::magnus::type_map::MagnusMapper;
    let code = gen_struct(&typ, &mapper, "TestLib", "test_lib", false, &[], false);

    let derive_line = code.lines().find(|l| l.trim_start().starts_with("#[derive(")).unwrap();
    assert!(derive_line.contains("serde::Deserialize"), "{derive_line}");
    assert!(!code.contains("impl<'de> serde::Deserialize<'de> for Point"));
}

#[test]
fn gen_opaque_struct_emits_arc_inner() {
    let typ = make_typedef("Handle", vec![]);
    let code = gen_opaque_struct(&typ, "test_lib", "TestLib");
    assert!(code.contains("inner: Arc<"), "opaque struct must have Arc inner");
    assert!(code.contains("struct Handle"), "must emit struct Handle");
}

use crate::core::ir::MethodDef;

fn shape_enum() -> EnumDef {
    EnumDef {
        name: "Shape".to_string(),
        rust_path: "test_lib::Shape".to_string(),
        original_rust_path: String::new(),
        variants: vec![
            make_variant("Circle", vec![make_field("radius", TypeRef::String, false)]),
            make_variant(
                "Rect",
                vec![
                    make_field("width", TypeRef::String, false),
                    make_field("height", TypeRef::String, false),
                ],
            ),
        ],
        methods: vec![],
        doc: String::new(),
        cfg: None,
        is_copy: false,
        has_serde: true,
        has_default: false,
        serde_content: None,
        serde_tag: Some("type".to_string()),
        serde_untagged: false,
        serde_rename_all: None,
        rename_all_fields: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        version: Default::default(),
    }
}

#[test]
fn variant_constructors_emit_singleton_per_struct_variant() {
    let code = gen_data_enum_variant_constructors(&shape_enum(), "test_lib", None);

    assert!(code.contains("impl Shape {"), "must emit an impl block: {code}");
    assert!(
        code.contains("pub fn _factory_circle(radius: String) -> Self"),
        "{code}"
    );
    assert!(code.contains("Self::Circle { radius }"), "{code}");
    assert!(
        code.contains("pub fn _factory_rect(width: String, height: String) -> Self"),
        "{code}"
    );
    assert!(code.contains("Self::Rect { width, height }"), "{code}");
}

#[test]
fn variant_constructors_use_serde_shaped_named_field_type() {
    let def = EnumDef {
        name: "Wrapper".to_string(),
        rust_path: "test_lib::Wrapper".to_string(),
        original_rust_path: String::new(),
        variants: vec![make_variant(
            "Llm",
            vec![
                make_field("llm", TypeRef::Named("LlmConfig".to_string()), false),
                make_field(
                    "opts",
                    TypeRef::Map(Box::new(TypeRef::String), Box::new(TypeRef::String)),
                    false,
                ),
            ],
        )],
        methods: vec![],
        doc: String::new(),
        cfg: None,
        is_copy: false,
        has_serde: true,
        has_default: false,
        serde_content: None,
        serde_tag: Some("type".to_string()),
        serde_untagged: false,
        serde_rename_all: None,
        rename_all_fields: None,
        binding_excluded: false,
        binding_exclusion_reason: None,
        excluded_variants: vec![],
        version: Default::default(),
    };

    let code = gen_data_enum_variant_constructors(&def, "test_lib", None);

    assert!(
        code.contains("pub fn _factory_llm(llm: LlmConfig, opts: String) -> Self"),
        "{code}"
    );
    assert!(code.contains("Self::Llm { llm, opts }"), "{code}");
    assert!(
        !code.contains("_core"),
        "magnus enum is binding-shaped, no core conversion: {code}"
    );
}

#[test]
fn variant_constructors_skip_unit_tuple_and_excluded() {
    let mut tuple_variant = make_variant("Pair", vec![make_field("_0", TypeRef::String, false)]);
    tuple_variant.is_tuple = true;
    let mut excluded = make_variant("Hidden", vec![make_field("value", TypeRef::String, false)]);
    excluded.binding_excluded = true;

    let def = EnumDef {
        variants: vec![
            make_variant("Empty", vec![]),
            tuple_variant,
            excluded,
            make_variant("Real", vec![make_field("value", TypeRef::String, false)]),
        ],
        ..shape_enum()
    };

    let code = gen_data_enum_variant_constructors(&def, "test_lib", None);

    assert!(!code.contains("_factory_empty"), "{code}");
    assert!(!code.contains("_factory_pair"), "{code}");
    assert!(!code.contains("_factory_hidden"), "{code}");
    assert!(code.contains("pub fn _factory_real(value: String) -> Self"), "{code}");
}

/// Regression for the `ContentPart` bug: no backend forwards `enum_def.methods` (a hand-written
/// inherent static method extracted from a separate `impl EnumType { .. }` block) into the
/// generated Ruby bindings, so suppressing the derived factory on a name collision used to drop
/// the constructor entirely with nothing to replace it. Every data-carrying variant must always
/// get a reachable factory.
#[test]
fn variant_constructors_emit_factory_even_with_colliding_hand_written_method() {
    let def = EnumDef {
        methods: vec![MethodDef {
            name: "circle".to_string(),
            is_static: true,
            ..Default::default()
        }],
        ..shape_enum()
    };

    let code = gen_data_enum_variant_constructors(&def, "test_lib", None);

    assert!(
        code.contains("pub fn _factory_circle(radius: String) -> Self"),
        "Circle factory must stay reachable despite the colliding hand-written method: {code}"
    );
    assert!(code.contains("Self::Circle { radius }"), "{code}");
    assert!(
        code.contains("pub fn _factory_rect(width: String, height: String) -> Self"),
        "{code}"
    );
}

#[test]
fn variant_constructors_empty_for_unit_only_enum() {
    let def = EnumDef {
        variants: vec![make_variant("A", vec![]), make_variant("B", vec![])],
        ..shape_enum()
    };
    let code = gen_data_enum_variant_constructors(&def, "test_lib", None);
    assert!(code.is_empty(), "expected no output for unit-only enum: {code}");
}

fn cfg_shape_enum_with_rust_path(rust_path: &str) -> EnumDef {
    let mut gated = make_variant(
        "Rect",
        vec![
            make_field("width", TypeRef::String, false),
            make_field("height", TypeRef::String, false),
        ],
    );
    gated.cfg = Some(r#"feature = "extra-shapes""#.to_string());
    EnumDef {
        rust_path: rust_path.to_string(),
        variants: vec![
            make_variant("Circle", vec![make_field("radius", TypeRef::String, false)]),
            gated,
            make_variant("Point", vec![make_field("value", TypeRef::String, false)]),
        ],
        ..shape_enum()
    }
}

/// The factory builds `Self::<Variant> { .. }` against the SAME wrapper `enum` `gen_enum` declares
/// (see `declared_enum_variants`'s doc comment): a FOREIGN cfg-gated variant `gen_enum` already
/// drops must not leave a constructor still naming it -- that literal would be a hard `E0599`
/// against the (correctly) narrower wrapper `enum` `gen_enum` renders alongside it.
#[test]
fn variant_constructors_drop_foreign_cfg_gated_variant() {
    let def = cfg_shape_enum_with_rust_path("dep_crate::Shape");

    // `Some(&[])` is load-bearing, not `None`: a foreign cfg-gated variant is only DROPPED once
    // the configured feature set is known and provably excludes its gate. With `None` the
    // nuanced authority keeps it unconditionally on purpose -- feature unification could enable
    // a dependency's feature in a way alef's static read cannot observe. ~keep
    let code = gen_data_enum_variant_constructors(&def, "crate", Some(&[]));

    assert!(!code.contains("_factory_rect"), "{code}");
    assert!(
        code.contains("pub fn _factory_circle(radius: String) -> Self"),
        "{code}"
    );
    assert!(code.contains("pub fn _factory_point(value: String) -> Self"), "{code}");
}

/// Control: the identical gate on a HOST-owned enum is never dropped -- `enum_variant_declaration`
/// never resolves a host-owned gate to `Drop`.
#[test]
fn variant_constructors_keep_host_owned_cfg_gated_variant() {
    let def = cfg_shape_enum_with_rust_path("crate::Shape");

    let code = gen_data_enum_variant_constructors(&def, "crate", None);

    assert!(
        code.contains("pub fn _factory_rect(width: String, height: String) -> Self"),
        "a host-owned cfg-gated variant's factory must stay reachable: {code}"
    );
}

/// The registration list feeding `method!(Shape::_factory_rect, ..)` must resolve the identical
/// FOREIGN/host verdict as the constructor generator above -- registering a path the constructor
/// no longer emits is a hard `E0599` at the registration site, not a missing Ruby method.
#[test]
fn variant_constructor_registrations_match_generated_constructors() {
    let foreign = cfg_shape_enum_with_rust_path("dep_crate::Shape");
    let registrations = data_enum_variant_constructor_registrations(&foreign, "crate", Some(&[]));
    let names: std::collections::BTreeSet<&str> = registrations
        .iter()
        .map(|(ruby_name, _, _)| ruby_name.as_str())
        .collect();

    assert_eq!(
        names,
        ["circle", "point"].into_iter().collect(),
        "registrations must exclude the dropped foreign cfg-gated `rect` variant: {registrations:?}"
    );
}

/// Issue #232: an adjacently-tagged enum (`tag` + `content`) emits tuple-form variants
/// exactly like an untagged one, but the conversion match arms keyed only on
/// `serde_untagged` and so destructured struct-form. Definition and `From` impls
/// disagreed in shape and rustc rejected them (E0559 / E0769). Both sides must now
/// consult the same predicate.
#[test]
fn adjacently_tagged_tuple_variant_uses_tuple_form_in_both_definition_and_conversions() {
    use crate::codegen::conversions::helpers::variant_emits_tuple_form;

    let mut adjacent = make_data_enum("OperationResult", Some("type"));
    adjacent.serde_content = Some("output".to_string());
    adjacent.variants[1].is_tuple = true;
    adjacent.variants[1].fields[0].name = "_0".to_string();

    // The definition emits tuple form ...
    let code = gen_enum(&adjacent, "test_lib", None, &[]);
    assert!(code.contains("Jpeg(String)"), "{code}");
    assert!(!code.contains("Self::Jpeg { _0 }"), "{code}");

    // ... and the shared predicate agrees, so conversions destructure the same way.
    assert!(
        variant_emits_tuple_form(&adjacent, &adjacent.variants[1]),
        "adjacently-tagged tuple variant must report tuple form to the conversion layer"
    );

    // Untagged keeps working.
    let mut untagged = make_data_enum("OperationResult", None);
    untagged.serde_untagged = true;
    untagged.variants[1].is_tuple = true;
    untagged.variants[1].fields[0].name = "_0".to_string();
    assert!(variant_emits_tuple_form(&untagged, &untagged.variants[1]));

    // A non-tuple variant of an adjacently-tagged enum keeps struct form.
    assert!(
        !variant_emits_tuple_form(&adjacent, &adjacent.variants[0]),
        "struct-form variants must not be reported as tuple form"
    );
}

/// alef #102: an async method with no declared `error_type` still opens its delegable body with
/// `let rt = tokio::runtime::Builder::new_multi_thread()...build().map_err(...)?;` (building the
/// tokio runtime is itself fallible, independent of whether the core method's own return type
/// is `Result`). If the
/// annotation is keyed on `method.error_type.is_some()` instead of that fact, a no-error async
/// method gets a bare-`T` signature around a body that still has a `?` in it — rustc rejects the
/// generated Ruby extension with E0277. ~keep
fn async_method_without_error(name: &str, receiver: ReceiverKind) -> MethodDef {
    MethodDef {
        name: name.to_string(),
        return_type: TypeRef::String,
        is_async: true,
        error_type: None,
        receiver: Some(receiver),
        cfg: None,
        ..Default::default()
    }
}

#[test]
fn opaque_async_method_without_error_type_still_returns_result() {
    let typ = make_typedef("Widget", vec![]);
    let method = async_method_without_error("process", ReceiverKind::Ref);
    let mapper = MagnusMapper;
    let code = gen_opaque_async_instance_method(
        &typ,
        &method,
        &mapper,
        "Widget",
        &AHashSet::default(),
        &AHashSet::default(),
        "test_lib",
        false,
    );
    assert!(
        code.contains("fn process_async(&self, ) -> Result<String, Error> {"),
        "async opaque method must stay Result-shaped even without a declared error type, got: {code}"
    );
    assert!(
        code.contains("tokio::runtime::Builder::new_multi_thread()"),
        "delegable async body must build a runtime, got: {code}"
    );
    assert!(
        code.contains(".thread_stack_size(ASYNC_METHOD_RUNTIME_STACK_SIZE_BYTES)"),
        "delegable async body's runtime must set an explicit worker stack size, got: {code}"
    );
    assert!(
        !code.contains("tokio::runtime::Runtime::new()"),
        "delegable async body must not use tokio's default (undersized) worker stack, got: {code}"
    );
}

#[test]
fn non_opaque_async_method_without_error_type_still_returns_result() {
    let typ = make_typedef("Widget", vec![]);
    let method = async_method_without_error("process", ReceiverKind::Ref);
    let mapper = MagnusMapper;
    let code = gen_async_instance_method(&method, &mapper, &typ, &AHashSet::default(), "test_lib");
    assert!(
        code.contains("fn process_async(&self, ) -> Result<String, Error> {"),
        "async instance method must stay Result-shaped even without a declared error type, got: {code}"
    );
    assert!(
        code.contains("tokio::runtime::Builder::new_multi_thread()"),
        "delegable async body must build a runtime, got: {code}"
    );
    assert!(
        code.contains(".thread_stack_size(ASYNC_METHOD_RUNTIME_STACK_SIZE_BYTES)"),
        "delegable async body's runtime must set an explicit worker stack size, got: {code}"
    );
    assert!(
        !code.contains("tokio::runtime::Runtime::new()"),
        "delegable async body must not use tokio's default (undersized) worker stack, got: {code}"
    );
}