Skip to main content

alef_codegen/conversions/
mod.rs

1mod binding_to_core;
2mod core_to_binding;
3mod enums;
4pub(crate) mod helpers;
5
6use ahash::AHashSet;
7
8/// Backend-specific configuration for From/field conversion generation.
9/// Enables shared code to handle all backend differences via parameters.
10#[derive(Default, Clone)]
11pub struct ConversionConfig<'a> {
12    /// Prefix for binding type names ("Js" for NAPI/WASM, "" for others).
13    pub type_name_prefix: &'a str,
14    /// U64/Usize/Isize need `as i64` casts (NAPI, PHP — JS/PHP lack native u64).
15    pub cast_large_ints_to_i64: bool,
16    /// Enum names mapped to String in the binding layer (PHP only).
17    /// Named fields referencing these use `format!("{:?}")` in core→binding.
18    pub enum_string_names: Option<&'a AHashSet<String>>,
19    /// Map types use JsValue in the binding layer (WASM only).
20    /// When true, Map fields use `serde_wasm_bindgen` for conversion instead of
21    /// iterator-based collect patterns (JsValue is not iterable).
22    pub map_uses_jsvalue: bool,
23    /// When true, f32 is mapped to f64 (NAPI only — JS has no f32).
24    pub cast_f32_to_f64: bool,
25    /// When true, non-optional fields on defaultable types are wrapped in Option<T>
26    /// in the binding struct and need `.unwrap_or_default()` in binding→core From.
27    /// Used by NAPI to make JS-facing structs fully optional.
28    pub optionalize_defaults: bool,
29    /// When true, Json (serde_json::Value) fields are mapped to String in the binding layer.
30    /// Core→binding uses `.to_string()`, binding→core uses `Default::default()` (lossy).
31    /// Used by PHP where serde_json::Value can't cross the extension boundary.
32    pub json_to_string: bool,
33    /// When true, Json fields stay as `serde_json::Value` in the binding layer (no wrapping).
34    /// Core↔binding conversions are identity since both sides hold the same type.
35    /// Used by NAPI (with `serde-json` feature) so JS callers can pass arbitrary objects
36    /// directly without first stringifying them.
37    pub json_as_value: bool,
38    /// When true, add synthetic metadata field conversion for ConversionResult.
39    /// Only NAPI backend sets this (it adds metadata field to the struct).
40    pub include_cfg_metadata: bool,
41    /// When true, non-optional Duration fields on `has_default` types are stored as
42    /// `Option<u64>` in the binding struct.  The From conversion uses the builder
43    /// pattern so that `None` falls back to the core type's `Default` implementation
44    /// (giving the real default, e.g. `Duration::from_secs(30)`) instead of `Duration::ZERO`.
45    /// Used by PyO3 to prevent validation failures when `request_timeout` is unset.
46    pub option_duration_on_defaults: bool,
47    /// When true, binding enums include data variant fields (Magnus).
48    /// When false (default), binding enums are unit-only and data is lost in conversion.
49    pub binding_enums_have_data: bool,
50    /// Type names excluded from the binding layer. Fields referencing these types
51    /// are skipped in the binding struct and defaulted in From conversions.
52    /// Used by WASM to handle types excluded due to native dependency requirements.
53    pub exclude_types: &'a [String],
54    /// When true, Vec<Named> fields are stored as JSON strings in the binding layer.
55    /// Core→binding uses `serde_json::to_string`, binding→core uses `serde_json::from_str`.
56    /// Used by Magnus (Ruby) where Vec<Named> cannot cross the FFI boundary directly and
57    /// is collapsed to String by `field_type_for_serde`'s catch-all arm.
58    pub vec_named_to_string: bool,
59    /// When true, all Map(K, V) fields are stored as a plain `String` in the binding layer.
60    /// Core→binding uses `format!("{:?}", val.field)`, binding→core uses `Default::default()` (lossy).
61    /// Used by Rustler (Elixir NIFs) where `HashMap` cannot cross the NIF boundary directly.
62    pub map_as_string: bool,
63    /// Set of opaque type names in the binding layer.
64    /// When a field has `CoreWrapper::Arc` and its type is an opaque Named type,
65    /// the binding wrapper holds `inner: Arc<CoreT>` and the conversion must extract
66    /// `.inner` directly instead of calling `.into()` + wrapping in `Arc::new`.
67    pub opaque_types: Option<&'a AHashSet<String>>,
68    /// Type names that should use `Default::default()` in the binding→core From impl.
69    /// Used by PHP to skip bridge type fields (e.g., VisitorHandle) that can't be
70    /// auto-converted via Into and are always handled by the bridge machinery instead.
71    pub from_binding_skip_types: &'a [String],
72    /// When `core_crate_override` is set for a language, the IR's `rust_path` values
73    /// still contain the original source crate prefix (e.g. `mylib_core::Method`).
74    /// This field remaps those paths: `(original_crate_name, override_crate_name)`.
75    /// When set, any `rust_path` whose leading crate segment equals `original_crate_name`
76    /// is rewritten to use `override_crate_name` instead.
77    /// Example: `Some(("mylib_core", "mylib_http"))` rewrites
78    /// `mylib_core::Method` → `mylib_http::Method`.
79    pub source_crate_remaps: &'a [(&'a str, &'a str)],
80    /// Per-field binding name overrides.  Key is `"TypeName.field_name"` (using the original
81    /// IR field name); value is the binding struct's actual Rust field name (e.g. `"class_"`).
82    /// Used when a field name is a reserved keyword in the target language and must be escaped
83    /// in the binding struct (e.g. `class` → `class_`).
84    ///
85    /// When present, `val.<binding_name>` is used for binding-side access and the original
86    /// `field_name` is used for core-side access (struct literal and assignment targets).
87    pub binding_field_renames: Option<&'a std::collections::HashMap<String, String>>,
88    /// When true, U8/U16/U32 (and their signed counterparts I8/I16) need `as i32` casts.
89    /// extendr maps all small integers to R's native integer type (i32), so binding→core
90    /// conversions must cast back to the original unsigned/narrow types.
91    pub cast_uints_to_i32: bool,
92    /// When true, U64/Usize/Isize are mapped to f64 (R's native double type) rather than i64.
93    /// extendr uses f64 for large integers because R has no native 64-bit integer type.
94    /// Binding→core: `as usize`/`as u64` casts; core→binding: `as f64` casts.
95    pub cast_large_ints_to_f64: bool,
96    /// Names of untagged data enums (`#[serde(untagged)]` with at least one data variant —
97    /// e.g. `Single(String) | Multiple(Vec<String>)`). Fields referencing these types are
98    /// stored as `serde_json::Value` in the binding struct (the wire JSON shape varies per
99    /// variant, so we accept any value at the boundary). Used by the PHP backend; ext-php-rs
100    /// has no `FromZval`/`IntoZval` for typed Rust enums with mixed-shape variants, and the
101    /// only safe wire format is JSON-via-Value. Conversions:
102    ///
103    ///   - core→binding: `serde_json::to_value(val.<name>).unwrap_or_default()`
104    ///   - binding→core: `serde_json::from_value(val.<name>).unwrap_or_default()`
105    pub untagged_data_enum_names: Option<&'a AHashSet<String>>,
106    /// Names of tagged-data enums (`#[serde(tag = "...")]` with at least one data variant).
107    /// Fields referencing these types (or `Vec` of these types) are stored as `JsValue` in the
108    /// wasm binding struct so that plain JS objects `{ role: "user", content: "..." }` can be
109    /// passed without being wrapped in an explicit binding-class instance.
110    ///
111    /// Used by the WASM backend only; `map_uses_jsvalue` must also be `true`.
112    ///
113    /// Conversions:
114    ///   - core→binding: `serde_wasm_bindgen::to_value(&val.<name>).unwrap_or(JsValue::NULL)`
115    ///   - binding→core: `serde_wasm_bindgen::from_value(val.<name>.clone()).unwrap_or_default()`
116    pub tagged_data_enum_names: Option<&'a AHashSet<String>>,
117    /// Names of cfg-gated fields that must NOT be skipped in conversions because the binding
118    /// emits them (via [`super::generators::RustBindingConfig::never_skip_cfg_field_names`]).
119    /// Empty by default; backends populate from trait-bridge `bind_via = "options_field"` config.
120    pub never_skip_cfg_field_names: &'a [String],
121    /// Names of trait-bridge OptionsField fields whose binding wrapper holds the core value
122    /// as `inner: Arc<core::T>` (the standard codegen layout for every OptionsField bridge).
123    /// When a field matches both `is_opaque_no_wrapper_field` and this list, the binding→core
124    /// From impl emits `(*v.inner).clone()` instead of `Default::default()`, so the visitor
125    /// (or other bridge handle) is forwarded rather than silently dropped.
126    pub trait_bridge_arc_wrapper_field_names: &'a [String],
127    /// When true, cfg-gated fields (not listed in `never_skip_cfg_field_names`) are
128    /// stripped from the binding struct entirely (no field at all in the struct body).
129    /// Conversions must then skip those fields and rely on `..Default::default()` in
130    /// the template to fill the core struct slot.
131    ///
132    /// Set to `true` for backends whose binding crate does not carry feature gates into
133    /// its own Cargo.toml — e.g. extendr (R), where the binding struct is uniform across
134    /// all feature combinations.  PyO3/NAPI/PHP/etc keep cfg-gated fields in the binding
135    /// struct (decorated with `#[cfg(...)]`) and want them included in conversions.
136    pub strip_cfg_fields_from_binding_struct: bool,
137    /// When true, untagged-enum tuple variants in the binding use Rust tuple-form
138    /// `Variant(T)` instead of struct-form `Variant { _0: T }`. The conversion match
139    /// arms must destructure / construct in the same shape, otherwise rustc rejects
140    /// the From impls with E0559 / E0769.
141    /// Set true ONLY for backends whose enum body emitter switches to tuple form for
142    /// `serde_untagged && variant.is_tuple` — currently just Magnus (Ruby) since
143    /// commit a715f378. Other data-bearing backends (Rustler, NAPI, PyO3, …) keep
144    /// struct-form even for untagged enums and so this flag must stay false.
145    pub binding_tuple_form_for_untagged_variants: bool,
146}
147
148impl<'a> ConversionConfig<'a> {
149    /// Look up the binding struct field name for a given type and IR field name.
150    ///
151    /// Returns the escaped name (e.g. `"class_"`) when the field was renamed due to a
152    /// reserved keyword conflict, or the original `field_name` when no rename applies.
153    pub fn binding_field_name<'b>(&self, type_name: &str, field_name: &'b str) -> &'b str
154    where
155        'a: 'b,
156    {
157        // &'b str: we return either the original (which has lifetime 'b from the parameter)
158        // or a &str from the HashMap (which would have lifetime 'a). Since 'a: 'b we can
159        // return either. But Rust's lifetime inference won't let us return `&'a str` from a
160        // `&'b str` parameter without unsafe. Use a helper that returns an owned String instead.
161        let _ = type_name;
162        field_name
163    }
164
165    /// Returns `true` when `field_name` is a trait-bridge OptionsField whose binding wrapper
166    /// stores the core value as `inner: Arc<core::T>`. Used by `gen_from_binding_to_core_cfg`
167    /// to emit `(*v.inner).clone()` instead of `Default::default()` for opaque-no-wrapper fields.
168    pub fn trait_bridge_field_is_arc_wrapper(&self, field_name: &str) -> bool {
169        self.trait_bridge_arc_wrapper_field_names
170            .iter()
171            .any(|n| n == field_name)
172    }
173
174    /// Like `binding_field_name` but returns an owned `String`, suitable for use in
175    /// format strings and string interpolation.
176    pub fn binding_field_name_owned(&self, type_name: &str, field_name: &str) -> String {
177        if let Some(map) = self.binding_field_renames {
178            let key = format!("{type_name}.{field_name}");
179            if let Some(renamed) = map.get(&key) {
180                return renamed.clone();
181            }
182        }
183        field_name.to_string()
184    }
185}
186
187// Re-export all public items so callers continue to use `conversions::foo`.
188pub use binding_to_core::{
189    apply_core_wrapper_to_core, field_conversion_to_core, field_conversion_to_core_cfg, gen_from_binding_to_core,
190    gen_from_binding_to_core_cfg,
191};
192pub use core_to_binding::{
193    field_conversion_from_core, field_conversion_from_core_cfg, gen_from_core_to_binding, gen_from_core_to_binding_cfg,
194};
195pub use enums::{
196    gen_enum_from_binding_to_core, gen_enum_from_binding_to_core_cfg, gen_enum_from_core_to_binding,
197    gen_enum_from_core_to_binding_cfg,
198};
199pub use helpers::{
200    apply_crate_remaps, binding_to_core_match_arm, build_type_path_map, can_generate_conversion,
201    can_generate_enum_conversion, can_generate_enum_conversion_from_core, convertible_types, core_enum_path,
202    core_enum_path_remapped, core_to_binding_convertible_types, core_to_binding_match_arm, core_type_path,
203    core_type_path_remapped, field_references_excluded_type, has_sanitized_fields, input_type_names, is_tuple_variant,
204    resolve_named_path,
205};
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210    use alef_core::ir::*;
211
212    fn simple_type() -> TypeDef {
213        TypeDef {
214            name: "Config".to_string(),
215            rust_path: "my_crate::Config".to_string(),
216            original_rust_path: String::new(),
217            fields: vec![
218                FieldDef {
219                    name: "name".into(),
220                    ty: TypeRef::String,
221                    optional: false,
222                    default: None,
223                    doc: String::new(),
224                    sanitized: false,
225                    is_boxed: false,
226                    type_rust_path: None,
227                    cfg: None,
228                    typed_default: None,
229                    core_wrapper: CoreWrapper::None,
230                    vec_inner_core_wrapper: CoreWrapper::None,
231                    newtype_wrapper: None,
232                    serde_rename: None,
233                    serde_flatten: false,
234                    binding_excluded: false,
235                    binding_exclusion_reason: None,
236                    original_type: None,
237                },
238                FieldDef {
239                    name: "timeout".into(),
240                    ty: TypeRef::Primitive(PrimitiveType::U64),
241                    optional: true,
242                    default: None,
243                    doc: String::new(),
244                    sanitized: false,
245                    is_boxed: false,
246                    type_rust_path: None,
247                    cfg: None,
248                    typed_default: None,
249                    core_wrapper: CoreWrapper::None,
250                    vec_inner_core_wrapper: CoreWrapper::None,
251                    newtype_wrapper: None,
252                    serde_rename: None,
253                    serde_flatten: false,
254                    binding_excluded: false,
255                    binding_exclusion_reason: None,
256                    original_type: None,
257                },
258                FieldDef {
259                    name: "backend".into(),
260                    ty: TypeRef::Named("Backend".into()),
261                    optional: true,
262                    default: None,
263                    doc: String::new(),
264                    sanitized: false,
265                    is_boxed: false,
266                    type_rust_path: None,
267                    cfg: None,
268                    typed_default: None,
269                    core_wrapper: CoreWrapper::None,
270                    vec_inner_core_wrapper: CoreWrapper::None,
271                    newtype_wrapper: None,
272                    serde_rename: None,
273                    serde_flatten: false,
274                    binding_excluded: false,
275                    binding_exclusion_reason: None,
276                    original_type: None,
277                },
278            ],
279            methods: vec![],
280            is_opaque: false,
281            is_clone: true,
282            is_copy: false,
283            is_trait: false,
284            has_default: false,
285            has_stripped_cfg_fields: false,
286            is_return_type: false,
287            serde_rename_all: None,
288            has_serde: false,
289            super_traits: vec![],
290            doc: String::new(),
291            cfg: None,
292            binding_excluded: false,
293            binding_exclusion_reason: None,
294        }
295    }
296
297    fn simple_enum() -> EnumDef {
298        EnumDef {
299            name: "Backend".to_string(),
300            rust_path: "my_crate::Backend".to_string(),
301            original_rust_path: String::new(),
302            variants: vec![
303                EnumVariant {
304                    name: "Cpu".into(),
305                    fields: vec![],
306                    is_tuple: false,
307                    doc: String::new(),
308                    is_default: false,
309                    serde_rename: None,
310                },
311                EnumVariant {
312                    name: "Gpu".into(),
313                    fields: vec![],
314                    is_tuple: false,
315                    doc: String::new(),
316                    is_default: false,
317                    serde_rename: None,
318                },
319            ],
320            doc: String::new(),
321            cfg: None,
322            is_copy: false,
323            has_serde: false,
324            serde_tag: None,
325            serde_untagged: false,
326            serde_rename_all: None,
327            binding_excluded: false,
328            binding_exclusion_reason: None,
329        }
330    }
331
332    #[test]
333    fn test_from_binding_to_core() {
334        let typ = simple_type();
335        let result = gen_from_binding_to_core(&typ, "my_crate");
336        assert!(result.contains("impl From<Config> for my_crate::Config"));
337        assert!(result.contains("name: val.name"));
338        assert!(result.contains("timeout: val.timeout"));
339        assert!(result.contains("backend: val.backend.map(Into::into)"));
340    }
341
342    #[test]
343    fn test_from_core_to_binding() {
344        let typ = simple_type();
345        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
346        assert!(result.contains("impl From<my_crate::Config> for Config"));
347    }
348
349    #[test]
350    fn test_enum_from_binding_to_core() {
351        let enum_def = simple_enum();
352        let result = gen_enum_from_binding_to_core(&enum_def, "my_crate");
353        assert!(result.contains("impl From<Backend> for my_crate::Backend"));
354        assert!(result.contains("Backend::Cpu => Self::Cpu"));
355        assert!(result.contains("Backend::Gpu => Self::Gpu"));
356    }
357
358    #[test]
359    fn test_enum_from_core_to_binding() {
360        let enum_def = simple_enum();
361        let result = gen_enum_from_core_to_binding(&enum_def, "my_crate");
362        assert!(result.contains("impl From<my_crate::Backend> for Backend"));
363        assert!(result.contains("my_crate::Backend::Cpu => Self::Cpu"));
364        assert!(result.contains("my_crate::Backend::Gpu => Self::Gpu"));
365    }
366
367    fn untagged_tuple_enum() -> EnumDef {
368        EnumDef {
369            name: "UserContent".to_string(),
370            rust_path: "my_crate::UserContent".to_string(),
371            original_rust_path: String::new(),
372            variants: vec![
373                EnumVariant {
374                    name: "Text".into(),
375                    fields: vec![FieldDef {
376                        name: "_0".into(),
377                        ty: TypeRef::String,
378                        optional: false,
379                        default: None,
380                        doc: String::new(),
381                        sanitized: false,
382                        is_boxed: false,
383                        type_rust_path: None,
384                        cfg: None,
385                        typed_default: None,
386                        core_wrapper: CoreWrapper::None,
387                        vec_inner_core_wrapper: CoreWrapper::None,
388                        newtype_wrapper: None,
389                        serde_rename: None,
390                        serde_flatten: false,
391                        binding_excluded: false,
392                        binding_exclusion_reason: None,
393                        original_type: None,
394                    }],
395                    is_tuple: true,
396                    doc: String::new(),
397                    is_default: false,
398                    serde_rename: None,
399                },
400                EnumVariant {
401                    name: "Parts".into(),
402                    fields: vec![FieldDef {
403                        name: "_0".into(),
404                        ty: TypeRef::Vec(Box::new(TypeRef::String)),
405                        optional: false,
406                        default: None,
407                        doc: String::new(),
408                        sanitized: false,
409                        is_boxed: false,
410                        type_rust_path: None,
411                        cfg: None,
412                        typed_default: None,
413                        core_wrapper: CoreWrapper::None,
414                        vec_inner_core_wrapper: CoreWrapper::None,
415                        newtype_wrapper: None,
416                        serde_rename: None,
417                        serde_flatten: false,
418                        binding_excluded: false,
419                        binding_exclusion_reason: None,
420                        original_type: None,
421                    }],
422                    is_tuple: true,
423                    doc: String::new(),
424                    is_default: false,
425                    serde_rename: None,
426                },
427            ],
428            doc: String::new(),
429            cfg: None,
430            is_copy: false,
431            has_serde: true,
432            serde_tag: None,
433            serde_untagged: true,
434            serde_rename_all: None,
435            binding_excluded: false,
436            binding_exclusion_reason: None,
437        }
438    }
439
440    #[test]
441    fn test_enum_from_binding_to_core_untagged_tuple_emits_tuple_pattern() {
442        // Regression: untagged enums with tuple variants emit tuple-form `Variant(T)` in
443        // the binding (Magnus template since commit a715f378). Conversion match arms must
444        // destructure tuple-form, not struct-form `Variant { _0 }`.
445        let enum_def = untagged_tuple_enum();
446        let config = ConversionConfig {
447            binding_enums_have_data: true,
448            binding_tuple_form_for_untagged_variants: true,
449            ..ConversionConfig::default()
450        };
451        let result = gen_enum_from_binding_to_core_cfg(&enum_def, "my_crate", &config);
452        // MUST destructure as tuple, not struct
453        assert!(
454            result.contains("UserContent::Text(_0)"),
455            "expected tuple-form binding pattern, got: {result}"
456        );
457        assert!(
458            !result.contains("UserContent::Text { _0 }"),
459            "must NOT use struct-form for untagged enums, got: {result}"
460        );
461        // Construct core as tuple
462        assert!(result.contains("Self::Text("));
463    }
464
465    #[test]
466    fn test_enum_from_core_to_binding_untagged_tuple_emits_tuple_constructor() {
467        // Regression: untagged enums with tuple variants emit tuple-form `Variant(T)` in
468        // the binding. Constructor must use tuple form, not `Self::Variant { _0 }`.
469        let enum_def = untagged_tuple_enum();
470        let config = ConversionConfig {
471            binding_enums_have_data: true,
472            binding_tuple_form_for_untagged_variants: true,
473            ..ConversionConfig::default()
474        };
475        let result = gen_enum_from_core_to_binding_cfg(&enum_def, "my_crate", &config);
476        // Core destructured as tuple (already correct), binding constructed as tuple
477        assert!(
478            result.contains("Self::Text(_0)"),
479            "expected tuple-form binding constructor, got: {result}"
480        );
481        assert!(
482            !result.contains("Self::Text { _0 }"),
483            "must NOT use struct-form constructor for untagged enums, got: {result}"
484        );
485    }
486
487    #[test]
488    fn test_enum_tagged_data_keeps_struct_form_pattern() {
489        // Counter-regression: tagged (non-untagged) data enums must keep struct-form
490        // `Variant { _0 }` pattern/constructor — only untagged enums switch to tuple form.
491        let mut enum_def = untagged_tuple_enum();
492        enum_def.serde_untagged = false;
493        enum_def.serde_tag = Some("type".to_string());
494        let config = ConversionConfig {
495            binding_enums_have_data: true,
496            binding_tuple_form_for_untagged_variants: true,
497            ..ConversionConfig::default()
498        };
499        let result = gen_enum_from_binding_to_core_cfg(&enum_def, "my_crate", &config);
500        assert!(
501            result.contains("UserContent::Text { _0 }"),
502            "tagged enums must keep struct-form, got: {result}"
503        );
504    }
505
506    #[test]
507    fn test_enum_untagged_keeps_struct_form_when_backend_does_not_opt_in() {
508        // Counter-regression for the Rustler backend: untagged enums must remain in
509        // struct-form when the backend's enum body emitter does not switch to tuple
510        // form (every backend except Magnus). `binding_tuple_form_for_untagged_variants`
511        // is the opt-in flag.
512        let enum_def = untagged_tuple_enum();
513        let config = ConversionConfig {
514            binding_enums_have_data: true,
515            binding_tuple_form_for_untagged_variants: false,
516            ..ConversionConfig::default()
517        };
518        let result = gen_enum_from_binding_to_core_cfg(&enum_def, "my_crate", &config);
519        assert!(
520            result.contains("UserContent::Text { _0 }"),
521            "backends without the opt-in must keep struct-form, got: {result}"
522        );
523        let result2 = gen_enum_from_core_to_binding_cfg(&enum_def, "my_crate", &config);
524        assert!(
525            result2.contains("Self::Text { _0:"),
526            "backends without the opt-in must construct struct-form, got: {result2}"
527        );
528    }
529
530    #[test]
531    fn test_from_binding_to_core_with_cfg_gated_field() {
532        // Create a type with a cfg-gated field
533        let mut typ = simple_type();
534        typ.has_stripped_cfg_fields = true;
535        typ.fields.push(FieldDef {
536            name: "layout".into(),
537            ty: TypeRef::String,
538            optional: false,
539            default: None,
540            doc: String::new(),
541            sanitized: false,
542            is_boxed: false,
543            type_rust_path: None,
544            cfg: Some("feature = \"layout-detection\"".into()),
545            typed_default: None,
546            core_wrapper: CoreWrapper::None,
547            vec_inner_core_wrapper: CoreWrapper::None,
548            newtype_wrapper: None,
549            serde_rename: None,
550            serde_flatten: false,
551            binding_excluded: false,
552            binding_exclusion_reason: None,
553            original_type: None,
554        });
555
556        let result = gen_from_binding_to_core(&typ, "my_crate");
557
558        // The impl should exist
559        assert!(result.contains("impl From<Config> for my_crate::Config"));
560        // Regular fields should be present
561        assert!(result.contains("name: val.name"));
562        assert!(result.contains("timeout: val.timeout"));
563        // Cfg-gated fields are now preserved on the binding struct, so the conversion
564        // accesses them directly rather than padding with ..Default::default().
565        assert!(result.contains("layout: val.layout"));
566    }
567
568    #[test]
569    fn test_from_core_to_binding_with_cfg_gated_field() {
570        // Create a type with a cfg-gated field
571        let mut typ = simple_type();
572        typ.fields.push(FieldDef {
573            name: "layout".into(),
574            ty: TypeRef::String,
575            optional: false,
576            default: None,
577            doc: String::new(),
578            sanitized: false,
579            is_boxed: false,
580            type_rust_path: None,
581            cfg: Some("feature = \"layout-detection\"".into()),
582            typed_default: None,
583            core_wrapper: CoreWrapper::None,
584            vec_inner_core_wrapper: CoreWrapper::None,
585            newtype_wrapper: None,
586            serde_rename: None,
587            serde_flatten: false,
588            binding_excluded: false,
589            binding_exclusion_reason: None,
590            original_type: None,
591        });
592
593        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
594
595        // The impl should exist
596        assert!(result.contains("impl From<my_crate::Config> for Config"));
597        // Regular fields should be present
598        assert!(result.contains("name: val.name"));
599        // Cfg-gated fields are now preserved on the binding struct and round-tripped.
600        assert!(result.contains("layout: val.layout"));
601    }
602
603    #[test]
604    fn test_field_conversion_from_core_map_named_non_optional() {
605        // Map<K, Named> non-optional: each value needs .into() core→binding
606        let result = field_conversion_from_core(
607            "tags",
608            &TypeRef::Map(Box::new(TypeRef::String), Box::new(TypeRef::Named("Tag".into()))),
609            false,
610            false,
611            &AHashSet::new(),
612        );
613        assert_eq!(
614            result,
615            "tags: val.tags.into_iter().map(|(k, v)| (k, v.into())).collect()"
616        );
617    }
618
619    #[test]
620    fn test_field_conversion_from_core_option_map_named() {
621        // Option<Map<K, Named>>: .map() wrapper + per-element .into()
622        let result = field_conversion_from_core(
623            "tags",
624            &TypeRef::Optional(Box::new(TypeRef::Map(
625                Box::new(TypeRef::String),
626                Box::new(TypeRef::Named("Tag".into())),
627            ))),
628            false,
629            false,
630            &AHashSet::new(),
631        );
632        assert_eq!(
633            result,
634            "tags: val.tags.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())"
635        );
636    }
637
638    #[test]
639    fn test_field_conversion_from_core_vec_named_non_optional() {
640        // Vec<Named> non-optional: each element needs .into() core→binding
641        let result = field_conversion_from_core(
642            "items",
643            &TypeRef::Vec(Box::new(TypeRef::Named("Item".into()))),
644            false,
645            false,
646            &AHashSet::new(),
647        );
648        assert_eq!(result, "items: val.items.into_iter().map(Into::into).collect()");
649    }
650
651    #[test]
652    fn test_field_conversion_from_core_option_vec_named() {
653        // Option<Vec<Named>>: .map() wrapper + per-element .into()
654        let result = field_conversion_from_core(
655            "items",
656            &TypeRef::Optional(Box::new(TypeRef::Vec(Box::new(TypeRef::Named("Item".into()))))),
657            false,
658            false,
659            &AHashSet::new(),
660        );
661        assert_eq!(
662            result,
663            "items: val.items.map(|v| v.into_iter().map(Into::into).collect())"
664        );
665    }
666
667    #[test]
668    fn test_field_conversion_to_core_option_map_named_applies_per_value_into() {
669        // Bug A1 regression: Option<Map<K, Named>> must apply per-value .into() so that
670        // binding-side wrapper types (e.g. PyO3 / Magnus structs) are converted correctly.
671        let result = field_conversion_to_core(
672            "patterns",
673            &TypeRef::Map(
674                Box::new(TypeRef::String),
675                Box::new(TypeRef::Named("ExtractionPattern".into())),
676            ),
677            true,
678        );
679        assert!(
680            result.contains("m.into_iter().map(|(k, v)| (k.into(), v.into())).collect()"),
681            "expected per-value v.into() in optional Map<Named> conversion, got: {result}"
682        );
683        assert_eq!(
684            result,
685            "patterns: val.patterns.map(|m| m.into_iter().map(|(k, v)| (k.into(), v.into())).collect())"
686        );
687    }
688
689    #[test]
690    fn test_gen_optionalized_field_to_core_ir_optional_map_named_preserves_option() {
691        // Bug A3 regression: when field_is_ir_optional=true, gen_optionalized_field_to_core must
692        // preserve the Option layer via .map(|m| …) instead of dropping it with unwrap_or_default().
693        use super::binding_to_core::gen_optionalized_field_to_core;
694        let config = ConversionConfig::default();
695        let result = gen_optionalized_field_to_core(
696            "patterns",
697            &TypeRef::Map(
698                Box::new(TypeRef::String),
699                Box::new(TypeRef::Named("ExtractionPattern".into())),
700            ),
701            &config,
702            true,
703        );
704        assert!(
705            result.contains("m.into_iter().map(|(k, v)| (k, v.into())).collect()"),
706            "expected per-value v.into() in ir-optional Map<Named> conversion, got: {result}"
707        );
708        assert_eq!(
709            result,
710            "patterns: val.patterns.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())"
711        );
712    }
713
714    #[test]
715    fn test_optionalized_defaultable_struct_uses_core_default_as_base() {
716        let mut typ = simple_type();
717        typ.has_default = true;
718        typ.fields = vec![
719            FieldDef {
720                name: "language".into(),
721                ty: TypeRef::String,
722                optional: false,
723                default: None,
724                doc: String::new(),
725                sanitized: false,
726                is_boxed: false,
727                type_rust_path: None,
728                cfg: None,
729                typed_default: None,
730                core_wrapper: CoreWrapper::Cow,
731                vec_inner_core_wrapper: CoreWrapper::None,
732                newtype_wrapper: None,
733                serde_rename: None,
734                serde_flatten: false,
735                binding_excluded: false,
736                binding_exclusion_reason: None,
737                original_type: None,
738            },
739            FieldDef {
740                name: "structure".into(),
741                ty: TypeRef::Primitive(PrimitiveType::Bool),
742                optional: false,
743                default: None,
744                doc: String::new(),
745                sanitized: false,
746                is_boxed: false,
747                type_rust_path: None,
748                cfg: None,
749                typed_default: None,
750                core_wrapper: CoreWrapper::None,
751                vec_inner_core_wrapper: CoreWrapper::None,
752                newtype_wrapper: None,
753                serde_rename: None,
754                serde_flatten: false,
755                binding_excluded: false,
756                binding_exclusion_reason: None,
757                original_type: None,
758            },
759        ];
760        let config = ConversionConfig {
761            type_name_prefix: "Js",
762            optionalize_defaults: true,
763            ..ConversionConfig::default()
764        };
765
766        let result = gen_from_binding_to_core_cfg(&typ, "my_crate", &config);
767
768        assert!(result.contains("let mut __result = my_crate::Config::default();"));
769        assert!(result.contains("if let Some(__v) = val.language { __result.language = __v.into(); }"));
770        assert!(result.contains("if let Some(__v) = val.structure { __result.structure = __v; }"));
771        assert!(!result.contains("unwrap_or_default()"));
772    }
773
774    fn arc_field_type(field: FieldDef) -> TypeDef {
775        TypeDef {
776            name: "State".to_string(),
777            rust_path: "my_crate::State".to_string(),
778            original_rust_path: String::new(),
779            fields: vec![field],
780            methods: vec![],
781            is_opaque: false,
782            is_clone: true,
783            is_copy: false,
784            is_trait: false,
785            has_default: false,
786            has_stripped_cfg_fields: false,
787            is_return_type: false,
788            serde_rename_all: None,
789            has_serde: false,
790            super_traits: vec![],
791            doc: String::new(),
792            cfg: None,
793            binding_excluded: false,
794            binding_exclusion_reason: None,
795        }
796    }
797
798    fn arc_field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
799        FieldDef {
800            name: name.into(),
801            ty,
802            optional,
803            default: None,
804            doc: String::new(),
805            sanitized: false,
806            is_boxed: false,
807            type_rust_path: None,
808            cfg: None,
809            typed_default: None,
810            core_wrapper: CoreWrapper::Arc,
811            vec_inner_core_wrapper: CoreWrapper::None,
812            newtype_wrapper: None,
813            serde_rename: None,
814            serde_flatten: false,
815            binding_excluded: false,
816            binding_exclusion_reason: None,
817            original_type: None,
818        }
819    }
820
821    /// Regression: Option<Arc<serde_json::Value>> must not chain `(*v).clone().into()`
822    /// on top of `as_ref().map(ToString::to_string)`, which would emit invalid
823    /// `(*String).clone()` (str: !Clone).
824    #[test]
825    fn test_arc_json_option_field_no_double_chain() {
826        let typ = arc_field_type(arc_field("registered_spec", TypeRef::Json, true));
827        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
828        assert!(
829            result.contains("val.registered_spec.as_ref().map(ToString::to_string)"),
830            "expected as_ref().map(ToString::to_string) for Option<Arc<Value>>, got: {result}"
831        );
832        assert!(
833            !result.contains("map(ToString::to_string).map("),
834            "must not chain a second map() on top of ToString::to_string, got: {result}"
835        );
836    }
837
838    /// Non-optional Arc<Value>: `(*val.X).clone().to_string()` is valid (Value: Clone).
839    #[test]
840    fn test_arc_json_non_optional_field() {
841        let typ = arc_field_type(arc_field("spec", TypeRef::Json, false));
842        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
843        assert!(
844            result.contains("(*val.spec).clone().to_string()"),
845            "expected (*val.spec).clone().to_string() for Arc<Value>, got: {result}"
846        );
847    }
848
849    /// Option<Arc<String>>: simple passthrough → `.map(|v| (*v).clone().into())` is valid
850    /// (String: Clone). Verifies the simple_passthrough branch is preserved.
851    #[test]
852    fn test_arc_string_option_field_passthrough() {
853        let typ = arc_field_type(arc_field("label", TypeRef::String, true));
854        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
855        assert!(
856            result.contains("val.label.map(|v| (*v).clone().into())"),
857            "expected .map(|v| (*v).clone().into()) for Option<Arc<String>>, got: {result}"
858        );
859    }
860}