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