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 cfg-gated fields that must NOT be skipped in conversions because the binding
107    /// emits them (via [`super::generators::RustBindingConfig::never_skip_cfg_field_names`]).
108    /// Empty by default; backends populate from trait-bridge `bind_via = "options_field"` config.
109    pub never_skip_cfg_field_names: &'a [String],
110    /// Names of trait-bridge OptionsField fields whose binding wrapper holds the core value
111    /// as `inner: Arc<core::T>` (the standard codegen layout for every OptionsField bridge).
112    /// When a field matches both `is_opaque_no_wrapper_field` and this list, the binding→core
113    /// From impl emits `(*v.inner).clone()` instead of `Default::default()`, so the visitor
114    /// (or other bridge handle) is forwarded rather than silently dropped.
115    pub trait_bridge_arc_wrapper_field_names: &'a [String],
116    /// When true, cfg-gated fields (not listed in `never_skip_cfg_field_names`) are
117    /// stripped from the binding struct entirely (no field at all in the struct body).
118    /// Conversions must then skip those fields and rely on `..Default::default()` in
119    /// the template to fill the core struct slot.
120    ///
121    /// Set to `true` for backends whose binding crate does not carry feature gates into
122    /// its own Cargo.toml — e.g. extendr (R), where the binding struct is uniform across
123    /// all feature combinations.  PyO3/NAPI/PHP/etc keep cfg-gated fields in the binding
124    /// struct (decorated with `#[cfg(...)]`) and want them included in conversions.
125    pub strip_cfg_fields_from_binding_struct: bool,
126}
127
128impl<'a> ConversionConfig<'a> {
129    /// Look up the binding struct field name for a given type and IR field name.
130    ///
131    /// Returns the escaped name (e.g. `"class_"`) when the field was renamed due to a
132    /// reserved keyword conflict, or the original `field_name` when no rename applies.
133    pub fn binding_field_name<'b>(&self, type_name: &str, field_name: &'b str) -> &'b str
134    where
135        'a: 'b,
136    {
137        // &'b str: we return either the original (which has lifetime 'b from the parameter)
138        // or a &str from the HashMap (which would have lifetime 'a). Since 'a: 'b we can
139        // return either. But Rust's lifetime inference won't let us return `&'a str` from a
140        // `&'b str` parameter without unsafe. Use a helper that returns an owned String instead.
141        let _ = type_name;
142        field_name
143    }
144
145    /// Returns `true` when `field_name` is a trait-bridge OptionsField whose binding wrapper
146    /// stores the core value as `inner: Arc<core::T>`. Used by `gen_from_binding_to_core_cfg`
147    /// to emit `(*v.inner).clone()` instead of `Default::default()` for opaque-no-wrapper fields.
148    pub fn trait_bridge_field_is_arc_wrapper(&self, field_name: &str) -> bool {
149        self.trait_bridge_arc_wrapper_field_names
150            .iter()
151            .any(|n| n == field_name)
152    }
153
154    /// Like `binding_field_name` but returns an owned `String`, suitable for use in
155    /// format strings and string interpolation.
156    pub fn binding_field_name_owned(&self, type_name: &str, field_name: &str) -> String {
157        if let Some(map) = self.binding_field_renames {
158            let key = format!("{type_name}.{field_name}");
159            if let Some(renamed) = map.get(&key) {
160                return renamed.clone();
161            }
162        }
163        field_name.to_string()
164    }
165}
166
167// Re-export all public items so callers continue to use `conversions::foo`.
168pub use binding_to_core::{
169    apply_core_wrapper_to_core, field_conversion_to_core, field_conversion_to_core_cfg, gen_from_binding_to_core,
170    gen_from_binding_to_core_cfg,
171};
172pub use core_to_binding::{
173    field_conversion_from_core, field_conversion_from_core_cfg, gen_from_core_to_binding, gen_from_core_to_binding_cfg,
174};
175pub use enums::{
176    gen_enum_from_binding_to_core, gen_enum_from_binding_to_core_cfg, gen_enum_from_core_to_binding,
177    gen_enum_from_core_to_binding_cfg,
178};
179pub use helpers::{
180    apply_crate_remaps, binding_to_core_match_arm, build_type_path_map, can_generate_conversion,
181    can_generate_enum_conversion, can_generate_enum_conversion_from_core, convertible_types, core_enum_path,
182    core_enum_path_remapped, core_to_binding_convertible_types, core_to_binding_match_arm, core_type_path,
183    core_type_path_remapped, field_references_excluded_type, has_sanitized_fields, input_type_names, is_tuple_variant,
184    resolve_named_path,
185};
186
187#[cfg(test)]
188mod tests {
189    use super::*;
190    use alef_core::ir::*;
191
192    fn simple_type() -> TypeDef {
193        TypeDef {
194            name: "Config".to_string(),
195            rust_path: "my_crate::Config".to_string(),
196            original_rust_path: String::new(),
197            fields: vec![
198                FieldDef {
199                    name: "name".into(),
200                    ty: TypeRef::String,
201                    optional: false,
202                    default: None,
203                    doc: String::new(),
204                    sanitized: false,
205                    is_boxed: false,
206                    type_rust_path: None,
207                    cfg: None,
208                    typed_default: None,
209                    core_wrapper: CoreWrapper::None,
210                    vec_inner_core_wrapper: CoreWrapper::None,
211                    newtype_wrapper: None,
212                    serde_rename: None,
213                    serde_flatten: false,
214                    binding_excluded: false,
215                    binding_exclusion_reason: None,
216                },
217                FieldDef {
218                    name: "timeout".into(),
219                    ty: TypeRef::Primitive(PrimitiveType::U64),
220                    optional: true,
221                    default: None,
222                    doc: String::new(),
223                    sanitized: false,
224                    is_boxed: false,
225                    type_rust_path: None,
226                    cfg: None,
227                    typed_default: None,
228                    core_wrapper: CoreWrapper::None,
229                    vec_inner_core_wrapper: CoreWrapper::None,
230                    newtype_wrapper: None,
231                    serde_rename: None,
232                    serde_flatten: false,
233                    binding_excluded: false,
234                    binding_exclusion_reason: None,
235                },
236                FieldDef {
237                    name: "backend".into(),
238                    ty: TypeRef::Named("Backend".into()),
239                    optional: true,
240                    default: None,
241                    doc: String::new(),
242                    sanitized: false,
243                    is_boxed: false,
244                    type_rust_path: None,
245                    cfg: None,
246                    typed_default: None,
247                    core_wrapper: CoreWrapper::None,
248                    vec_inner_core_wrapper: CoreWrapper::None,
249                    newtype_wrapper: None,
250                    serde_rename: None,
251                    serde_flatten: false,
252                    binding_excluded: false,
253                    binding_exclusion_reason: None,
254                },
255            ],
256            methods: vec![],
257            is_opaque: false,
258            is_clone: true,
259            is_copy: false,
260            is_trait: false,
261            has_default: false,
262            has_stripped_cfg_fields: false,
263            is_return_type: false,
264            serde_rename_all: None,
265            has_serde: false,
266            super_traits: vec![],
267            doc: String::new(),
268            cfg: None,
269            binding_excluded: false,
270            binding_exclusion_reason: None,
271        }
272    }
273
274    fn simple_enum() -> EnumDef {
275        EnumDef {
276            name: "Backend".to_string(),
277            rust_path: "my_crate::Backend".to_string(),
278            original_rust_path: String::new(),
279            variants: vec![
280                EnumVariant {
281                    name: "Cpu".into(),
282                    fields: vec![],
283                    is_tuple: false,
284                    doc: String::new(),
285                    is_default: false,
286                    serde_rename: None,
287                },
288                EnumVariant {
289                    name: "Gpu".into(),
290                    fields: vec![],
291                    is_tuple: false,
292                    doc: String::new(),
293                    is_default: false,
294                    serde_rename: None,
295                },
296            ],
297            doc: String::new(),
298            cfg: None,
299            is_copy: false,
300            has_serde: false,
301            serde_tag: None,
302            serde_untagged: false,
303            serde_rename_all: None,
304            binding_excluded: false,
305            binding_exclusion_reason: None,
306        }
307    }
308
309    #[test]
310    fn test_from_binding_to_core() {
311        let typ = simple_type();
312        let result = gen_from_binding_to_core(&typ, "my_crate");
313        assert!(result.contains("impl From<Config> for my_crate::Config"));
314        assert!(result.contains("name: val.name"));
315        assert!(result.contains("timeout: val.timeout"));
316        assert!(result.contains("backend: val.backend.map(Into::into)"));
317    }
318
319    #[test]
320    fn test_from_core_to_binding() {
321        let typ = simple_type();
322        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
323        assert!(result.contains("impl From<my_crate::Config> for Config"));
324    }
325
326    #[test]
327    fn test_enum_from_binding_to_core() {
328        let enum_def = simple_enum();
329        let result = gen_enum_from_binding_to_core(&enum_def, "my_crate");
330        assert!(result.contains("impl From<Backend> for my_crate::Backend"));
331        assert!(result.contains("Backend::Cpu => Self::Cpu"));
332        assert!(result.contains("Backend::Gpu => Self::Gpu"));
333    }
334
335    #[test]
336    fn test_enum_from_core_to_binding() {
337        let enum_def = simple_enum();
338        let result = gen_enum_from_core_to_binding(&enum_def, "my_crate");
339        assert!(result.contains("impl From<my_crate::Backend> for Backend"));
340        assert!(result.contains("my_crate::Backend::Cpu => Self::Cpu"));
341        assert!(result.contains("my_crate::Backend::Gpu => Self::Gpu"));
342    }
343
344    #[test]
345    fn test_from_binding_to_core_with_cfg_gated_field() {
346        // Create a type with a cfg-gated field
347        let mut typ = simple_type();
348        typ.has_stripped_cfg_fields = true;
349        typ.fields.push(FieldDef {
350            name: "layout".into(),
351            ty: TypeRef::String,
352            optional: false,
353            default: None,
354            doc: String::new(),
355            sanitized: false,
356            is_boxed: false,
357            type_rust_path: None,
358            cfg: Some("feature = \"layout-detection\"".into()),
359            typed_default: None,
360            core_wrapper: CoreWrapper::None,
361            vec_inner_core_wrapper: CoreWrapper::None,
362            newtype_wrapper: None,
363            serde_rename: None,
364            serde_flatten: false,
365            binding_excluded: false,
366            binding_exclusion_reason: None,
367        });
368
369        let result = gen_from_binding_to_core(&typ, "my_crate");
370
371        // The impl should exist
372        assert!(result.contains("impl From<Config> for my_crate::Config"));
373        // Regular fields should be present
374        assert!(result.contains("name: val.name"));
375        assert!(result.contains("timeout: val.timeout"));
376        // Cfg-gated fields are now preserved on the binding struct, so the conversion
377        // accesses them directly rather than padding with ..Default::default().
378        assert!(result.contains("layout: val.layout"));
379    }
380
381    #[test]
382    fn test_from_core_to_binding_with_cfg_gated_field() {
383        // Create a type with a cfg-gated field
384        let mut typ = simple_type();
385        typ.fields.push(FieldDef {
386            name: "layout".into(),
387            ty: TypeRef::String,
388            optional: false,
389            default: None,
390            doc: String::new(),
391            sanitized: false,
392            is_boxed: false,
393            type_rust_path: None,
394            cfg: Some("feature = \"layout-detection\"".into()),
395            typed_default: None,
396            core_wrapper: CoreWrapper::None,
397            vec_inner_core_wrapper: CoreWrapper::None,
398            newtype_wrapper: None,
399            serde_rename: None,
400            serde_flatten: false,
401            binding_excluded: false,
402            binding_exclusion_reason: None,
403        });
404
405        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
406
407        // The impl should exist
408        assert!(result.contains("impl From<my_crate::Config> for Config"));
409        // Regular fields should be present
410        assert!(result.contains("name: val.name"));
411        // Cfg-gated fields are now preserved on the binding struct and round-tripped.
412        assert!(result.contains("layout: val.layout"));
413    }
414
415    #[test]
416    fn test_field_conversion_from_core_map_named_non_optional() {
417        // Map<K, Named> non-optional: each value needs .into() core→binding
418        let result = field_conversion_from_core(
419            "tags",
420            &TypeRef::Map(Box::new(TypeRef::String), Box::new(TypeRef::Named("Tag".into()))),
421            false,
422            false,
423            &AHashSet::new(),
424        );
425        assert_eq!(
426            result,
427            "tags: val.tags.into_iter().map(|(k, v)| (k, v.into())).collect()"
428        );
429    }
430
431    #[test]
432    fn test_field_conversion_from_core_option_map_named() {
433        // Option<Map<K, Named>>: .map() wrapper + per-element .into()
434        let result = field_conversion_from_core(
435            "tags",
436            &TypeRef::Optional(Box::new(TypeRef::Map(
437                Box::new(TypeRef::String),
438                Box::new(TypeRef::Named("Tag".into())),
439            ))),
440            false,
441            false,
442            &AHashSet::new(),
443        );
444        assert_eq!(
445            result,
446            "tags: val.tags.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())"
447        );
448    }
449
450    #[test]
451    fn test_field_conversion_from_core_vec_named_non_optional() {
452        // Vec<Named> non-optional: each element needs .into() core→binding
453        let result = field_conversion_from_core(
454            "items",
455            &TypeRef::Vec(Box::new(TypeRef::Named("Item".into()))),
456            false,
457            false,
458            &AHashSet::new(),
459        );
460        assert_eq!(result, "items: val.items.into_iter().map(Into::into).collect()");
461    }
462
463    #[test]
464    fn test_field_conversion_from_core_option_vec_named() {
465        // Option<Vec<Named>>: .map() wrapper + per-element .into()
466        let result = field_conversion_from_core(
467            "items",
468            &TypeRef::Optional(Box::new(TypeRef::Vec(Box::new(TypeRef::Named("Item".into()))))),
469            false,
470            false,
471            &AHashSet::new(),
472        );
473        assert_eq!(
474            result,
475            "items: val.items.map(|v| v.into_iter().map(Into::into).collect())"
476        );
477    }
478
479    #[test]
480    fn test_field_conversion_to_core_option_map_named_applies_per_value_into() {
481        // Bug A1 regression: Option<Map<K, Named>> must apply per-value .into() so that
482        // binding-side wrapper types (e.g. PyO3 / Magnus structs) are converted correctly.
483        let result = field_conversion_to_core(
484            "patterns",
485            &TypeRef::Map(
486                Box::new(TypeRef::String),
487                Box::new(TypeRef::Named("ExtractionPattern".into())),
488            ),
489            true,
490        );
491        assert!(
492            result.contains("m.into_iter().map(|(k, v)| (k.into(), v.into())).collect()"),
493            "expected per-value v.into() in optional Map<Named> conversion, got: {result}"
494        );
495        assert_eq!(
496            result,
497            "patterns: val.patterns.map(|m| m.into_iter().map(|(k, v)| (k.into(), v.into())).collect())"
498        );
499    }
500
501    #[test]
502    fn test_gen_optionalized_field_to_core_ir_optional_map_named_preserves_option() {
503        // Bug A3 regression: when field_is_ir_optional=true, gen_optionalized_field_to_core must
504        // preserve the Option layer via .map(|m| …) instead of dropping it with unwrap_or_default().
505        use super::binding_to_core::gen_optionalized_field_to_core;
506        let config = ConversionConfig::default();
507        let result = gen_optionalized_field_to_core(
508            "patterns",
509            &TypeRef::Map(
510                Box::new(TypeRef::String),
511                Box::new(TypeRef::Named("ExtractionPattern".into())),
512            ),
513            &config,
514            true,
515        );
516        assert!(
517            result.contains("m.into_iter().map(|(k, v)| (k, v.into())).collect()"),
518            "expected per-value v.into() in ir-optional Map<Named> conversion, got: {result}"
519        );
520        assert_eq!(
521            result,
522            "patterns: val.patterns.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())"
523        );
524    }
525
526    #[test]
527    fn test_optionalized_defaultable_struct_uses_core_default_as_base() {
528        let mut typ = simple_type();
529        typ.has_default = true;
530        typ.fields = vec![
531            FieldDef {
532                name: "language".into(),
533                ty: TypeRef::String,
534                optional: false,
535                default: None,
536                doc: String::new(),
537                sanitized: false,
538                is_boxed: false,
539                type_rust_path: None,
540                cfg: None,
541                typed_default: None,
542                core_wrapper: CoreWrapper::Cow,
543                vec_inner_core_wrapper: CoreWrapper::None,
544                newtype_wrapper: None,
545                serde_rename: None,
546                serde_flatten: false,
547                binding_excluded: false,
548                binding_exclusion_reason: None,
549            },
550            FieldDef {
551                name: "structure".into(),
552                ty: TypeRef::Primitive(PrimitiveType::Bool),
553                optional: false,
554                default: None,
555                doc: String::new(),
556                sanitized: false,
557                is_boxed: false,
558                type_rust_path: None,
559                cfg: None,
560                typed_default: None,
561                core_wrapper: CoreWrapper::None,
562                vec_inner_core_wrapper: CoreWrapper::None,
563                newtype_wrapper: None,
564                serde_rename: None,
565                serde_flatten: false,
566                binding_excluded: false,
567                binding_exclusion_reason: None,
568            },
569        ];
570        let config = ConversionConfig {
571            type_name_prefix: "Js",
572            optionalize_defaults: true,
573            ..ConversionConfig::default()
574        };
575
576        let result = gen_from_binding_to_core_cfg(&typ, "my_crate", &config);
577
578        assert!(result.contains("let mut __result = my_crate::Config::default();"));
579        assert!(result.contains("if let Some(__v) = val.language { __result.language = __v.into(); }"));
580        assert!(result.contains("if let Some(__v) = val.structure { __result.structure = __v; }"));
581        assert!(!result.contains("unwrap_or_default()"));
582    }
583
584    fn arc_field_type(field: FieldDef) -> TypeDef {
585        TypeDef {
586            name: "State".to_string(),
587            rust_path: "my_crate::State".to_string(),
588            original_rust_path: String::new(),
589            fields: vec![field],
590            methods: vec![],
591            is_opaque: false,
592            is_clone: true,
593            is_copy: false,
594            is_trait: false,
595            has_default: false,
596            has_stripped_cfg_fields: false,
597            is_return_type: false,
598            serde_rename_all: None,
599            has_serde: false,
600            super_traits: vec![],
601            doc: String::new(),
602            cfg: None,
603            binding_excluded: false,
604            binding_exclusion_reason: None,
605        }
606    }
607
608    fn arc_field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
609        FieldDef {
610            name: name.into(),
611            ty,
612            optional,
613            default: None,
614            doc: String::new(),
615            sanitized: false,
616            is_boxed: false,
617            type_rust_path: None,
618            cfg: None,
619            typed_default: None,
620            core_wrapper: CoreWrapper::Arc,
621            vec_inner_core_wrapper: CoreWrapper::None,
622            newtype_wrapper: None,
623            serde_rename: None,
624            serde_flatten: false,
625            binding_excluded: false,
626            binding_exclusion_reason: None,
627        }
628    }
629
630    /// Regression: Option<Arc<serde_json::Value>> must not chain `(*v).clone().into()`
631    /// on top of `as_ref().map(ToString::to_string)`, which would emit invalid
632    /// `(*String).clone()` (str: !Clone).
633    #[test]
634    fn test_arc_json_option_field_no_double_chain() {
635        let typ = arc_field_type(arc_field("registered_spec", TypeRef::Json, true));
636        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
637        assert!(
638            result.contains("val.registered_spec.as_ref().map(ToString::to_string)"),
639            "expected as_ref().map(ToString::to_string) for Option<Arc<Value>>, got: {result}"
640        );
641        assert!(
642            !result.contains("map(ToString::to_string).map("),
643            "must not chain a second map() on top of ToString::to_string, got: {result}"
644        );
645    }
646
647    /// Non-optional Arc<Value>: `(*val.X).clone().to_string()` is valid (Value: Clone).
648    #[test]
649    fn test_arc_json_non_optional_field() {
650        let typ = arc_field_type(arc_field("spec", TypeRef::Json, false));
651        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
652        assert!(
653            result.contains("(*val.spec).clone().to_string()"),
654            "expected (*val.spec).clone().to_string() for Arc<Value>, got: {result}"
655        );
656    }
657
658    /// Option<Arc<String>>: simple passthrough → `.map(|v| (*v).clone().into())` is valid
659    /// (String: Clone). Verifies the simple_passthrough branch is preserved.
660    #[test]
661    fn test_arc_string_option_field_passthrough() {
662        let typ = arc_field_type(arc_field("label", TypeRef::String, true));
663        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
664        assert!(
665            result.contains("val.label.map(|v| (*v).clone().into())"),
666            "expected .map(|v| (*v).clone().into()) for Option<Arc<String>>, got: {result}"
667        );
668    }
669}