Skip to main content

alef_codegen/conversions/
core_to_binding.rs

1use ahash::AHashSet;
2use alef_core::ir::{CoreWrapper, PrimitiveType, TypeDef, TypeRef};
3
4use super::ConversionConfig;
5use super::binding_to_core::field_conversion_to_core;
6use super::helpers::is_newtype;
7use super::helpers::{binding_prim_str, core_type_path_remapped, needs_f64_cast, needs_i32_cast, needs_i64_cast};
8
9/// Generate `impl From<core::Type> for BindingType` (core -> binding).
10pub fn gen_from_core_to_binding(typ: &TypeDef, core_import: &str, opaque_types: &AHashSet<String>) -> String {
11    gen_from_core_to_binding_cfg(typ, core_import, opaque_types, &ConversionConfig::default())
12}
13
14/// Generate `impl From<core::Type> for BindingType` with backend-specific config.
15pub fn gen_from_core_to_binding_cfg(
16    typ: &TypeDef,
17    core_import: &str,
18    opaque_types: &AHashSet<String>,
19    config: &ConversionConfig,
20) -> String {
21    let core_path = core_type_path_remapped(typ, core_import, config.source_crate_remaps);
22    let binding_name = format!("{}{}", config.type_name_prefix, typ.name);
23
24    // Newtype structs: extract inner value with val.0
25    if is_newtype(typ) {
26        let field = &typ.fields[0];
27        let newtype_inner_expr = match &field.ty {
28            TypeRef::Named(_) => "val.0.into()".to_string(),
29            TypeRef::Path => "val.0.to_string_lossy().to_string()".to_string(),
30            TypeRef::Duration => "val.0.as_millis() as u64".to_string(),
31            _ => "val.0".to_string(),
32        };
33        return crate::template_env::render(
34            "conversions/core_to_binding_impl",
35            minijinja::context! {
36                core_path => core_path,
37                binding_name => binding_name,
38                is_newtype => true,
39                newtype_inner_expr => newtype_inner_expr,
40                fields => vec![] as Vec<String>,
41            },
42        );
43    }
44
45    let optionalized = config.optionalize_defaults && typ.has_default;
46
47    // Pre-compute all field conversions
48    let mut fields = Vec::new();
49    for field in &typ.fields {
50        // Fields referencing excluded types are not present in the binding struct — skip
51        if !config.exclude_types.is_empty()
52            && super::helpers::field_references_excluded_type(&field.ty, config.exclude_types)
53        {
54            continue;
55        }
56        let base_conversion = field_conversion_from_core_cfg(
57            &field.name,
58            &field.ty,
59            field.optional,
60            field.sanitized,
61            opaque_types,
62            config,
63        );
64        // Box<T> fields: dereference before conversion.
65        let base_conversion = if field.is_boxed && matches!(&field.ty, TypeRef::Named(_)) {
66            if field.optional {
67                // Optional<Box<T>>: replace .map(Into::into) with .map(|v| (*v).into())
68                let src = format!("{}: val.{}.map(Into::into)", field.name, field.name);
69                let dst = format!("{}: val.{}.map(|v| (*v).into())", field.name, field.name);
70                if base_conversion == src { dst } else { base_conversion }
71            } else {
72                // Box<T>: replace `val.{name}` with `(*val.{name})`
73                base_conversion.replace(&format!("val.{}", field.name), &format!("(*val.{})", field.name))
74            }
75        } else {
76            base_conversion
77        };
78        // Newtype unwrapping: when the field was resolved from a newtype (e.g. NodeIndex → u32),
79        // unwrap the core newtype by accessing `.0`.
80        // e.g. `source: val.source` → `source: val.source.0`
81        //      `parent: val.parent` → `parent: val.parent.map(|v| v.0)`
82        //      `children: val.children` → `children: val.children.iter().map(|v| v.0).collect()`
83        let base_conversion = if field.newtype_wrapper.is_some() {
84            match &field.ty {
85                TypeRef::Optional(_) => {
86                    // Replace `val.{name}` with `val.{name}.map(|v| v.0)` in the generated expression
87                    base_conversion.replace(
88                        &format!("val.{}", field.name),
89                        &format!("val.{}.map(|v| v.0)", field.name),
90                    )
91                }
92                TypeRef::Vec(_) => {
93                    // Replace `val.{name}` with `val.{name}.iter().map(|v| v.0).collect()` in expression
94                    base_conversion.replace(
95                        &format!("val.{}", field.name),
96                        &format!("val.{}.iter().map(|v| v.0).collect::<Vec<_>>()", field.name),
97                    )
98                }
99                // When `optional=true` and `ty` is a plain Primitive (not TypeRef::Optional), the core
100                // field is actually `Option<NewtypeT>`, so we must use `.map(|v| v.0)` not `.0`.
101                _ if field.optional => base_conversion.replace(
102                    &format!("val.{}", field.name),
103                    &format!("val.{}.map(|v| v.0)", field.name),
104                ),
105                _ => {
106                    // Direct field: append `.0` to access the inner primitive
107                    base_conversion.replace(&format!("val.{}", field.name), &format!("val.{}.0", field.name))
108                }
109            }
110        } else {
111            base_conversion
112        };
113        // When field.optional=true AND field.ty=Optional(T), the binding struct flattens
114        // Option<Option<T>> to Option<T>. Core produces Option<Option<T>>, binding needs
115        // Option<T>. Generate the conversion by treating the pre-flattened field as Option<T>:
116        // call the standard conversion for the inner type T with optional=true, substituting
117        // val.{name}.flatten() for val.{name} so all cast/conversion logic applies to T.
118        let is_flattened_optional = field.optional && matches!(field.ty, TypeRef::Optional(_));
119        let base_conversion = if is_flattened_optional {
120            if let TypeRef::Optional(inner) = &field.ty {
121                // Produce the conversion as if the field is Option<inner> with value val.name.flatten()
122                let inner_conv = field_conversion_from_core_cfg(
123                    &field.name,
124                    inner.as_ref(),
125                    true,
126                    field.sanitized,
127                    opaque_types,
128                    config,
129                );
130                // inner_conv references val.{name}; replace with val.{name}.flatten()
131                inner_conv.replace(&format!("val.{}", field.name), &format!("val.{}.flatten()", field.name))
132            } else {
133                base_conversion
134            }
135        } else {
136            base_conversion
137        };
138        // Optionalized non-optional fields need Some() wrapping in core→binding direction.
139        // This covers both NAPI-style full optionalization and PyO3-style Duration optionalization.
140        // Flattened-optional fields are already handled above with the correct type.
141        let needs_some_wrap = !is_flattened_optional
142            && ((optionalized && !field.optional)
143                || (config.option_duration_on_defaults
144                    && typ.has_default
145                    && !field.optional
146                    && matches!(field.ty, TypeRef::Duration)));
147        let conversion = if needs_some_wrap {
148            // Extract the value expression after "name: " and wrap in Some()
149            if let Some(expr) = base_conversion.strip_prefix(&format!("{}: ", field.name)) {
150                format!("{}: Some({})", field.name, expr)
151            } else {
152                base_conversion
153            }
154        } else {
155            base_conversion
156        };
157        // Opaque Named fields without CoreWrapper::Arc (e.g. visitor: Object<'static>) cannot be
158        // auto-converted via Arc::new — the binding stores a raw host object that needs a bridge.
159        // Emit Default::default() and let the caller (e.g. the convert function) set it separately.
160        let is_opaque_no_wrapper_field = field.core_wrapper == CoreWrapper::None
161            && matches!(&field.ty, TypeRef::Named(n) if config
162                .opaque_types
163                .is_some_and(|opaque| opaque.contains(n.as_str())));
164        // CoreWrapper: unwrap Arc, convert Cow→String, Bytes→Vec<u8>
165        // For sanitized fields, still apply Cow→String conversion: Cow<'_, str> sanitizes to
166        // TypeRef::String and the Debug-formatted fallback produces quotes, but Cow implements
167        // Display so .to_string() (emitted by apply_core_wrapper_from_core for Cow) is correct.
168        // Other sanitized fields (unknown Named types) still fall through to Debug formatting.
169        let conversion = if is_opaque_no_wrapper_field {
170            format!("{}: Default::default()", field.name)
171        } else if !field.sanitized || field.core_wrapper == alef_core::ir::CoreWrapper::Cow {
172            apply_core_wrapper_from_core(
173                &conversion,
174                &field.name,
175                &field.core_wrapper,
176                &field.vec_inner_core_wrapper,
177                field.optional,
178            )
179        } else {
180            conversion
181        };
182        // Skip cfg-gated fields unless they're force-restored via never_skip_cfg_field_names
183        // (trait-bridge bind_via = "options_field" config).
184        if field.cfg.is_some() && !config.never_skip_cfg_field_names.contains(&field.name) {
185            continue;
186        }
187        // In core→binding direction, the binding struct field may be keyword-escaped
188        // (e.g. `class_` for `class`). The generated conversion has `field.name: expr`
189        // on the left side — rename it to `binding_name: expr` when needed.
190        let binding_field = config.binding_field_name_owned(&typ.name, &field.name);
191        let conversion = if binding_field != field.name {
192            if let Some(expr) = conversion.strip_prefix(&format!("{}: ", field.name)) {
193                format!("{binding_field}: {expr}")
194            } else {
195                conversion
196            }
197        } else {
198            conversion
199        };
200        fields.push(conversion);
201    }
202
203    crate::template_env::render(
204        "conversions/core_to_binding_impl",
205        minijinja::context! {
206            core_path => core_path,
207            binding_name => binding_name,
208            is_newtype => false,
209            newtype_inner_expr => "",
210            fields => fields,
211        },
212    )
213}
214
215/// Same but for core -> binding direction.
216/// Some types are asymmetric (PathBuf→String, sanitized fields need .to_string()).
217pub fn field_conversion_from_core(
218    name: &str,
219    ty: &TypeRef,
220    optional: bool,
221    sanitized: bool,
222    opaque_types: &AHashSet<String>,
223) -> String {
224    // Sanitized fields: the binding type differs from core (e.g. Box<str>→String, Cow<str>→String).
225    // Box<str>, Cow<str>, and Arc<str> all implement Display, so use .to_string() not {:?}.
226    // {:?} on string-like types produces debug-escaped output with surrounding quotes.
227    if sanitized {
228        // Vec<Primitive>: sanitized from tuple types like (u32, u32) → Vec<u32>.
229        // Core has a tuple, binding expects Vec — destructure the tuple.
230        if let TypeRef::Vec(inner) = ty {
231            if matches!(inner.as_ref(), TypeRef::Primitive(_)) {
232                if optional {
233                    return format!(
234                        "{name}: val.{name}.map(|t| {{ let arr: Vec<_> = [t.0, t.1].into_iter().map(|v| v as _).collect(); arr }})"
235                    );
236                }
237                return format!("{name}: vec![val.{name}.0 as _, val.{name}.1 as _]");
238            }
239        }
240        // Optional(Vec<Primitive>): sanitized from Option<(T, T)> → Option<Vec<T>>.
241        if let TypeRef::Optional(opt_inner) = ty {
242            if let TypeRef::Vec(vec_inner) = opt_inner.as_ref() {
243                if matches!(vec_inner.as_ref(), TypeRef::Primitive(_)) {
244                    return format!("{name}: val.{name}.map(|t| vec![t.0 as _, t.1 as _])");
245                }
246            }
247        }
248        // Map(String, String): sanitized from Map(Box<str>, Box<str>) etc.
249        if let TypeRef::Map(k, v) = ty {
250            if matches!(k.as_ref(), TypeRef::String) && matches!(v.as_ref(), TypeRef::String) {
251                if optional {
252                    return format!(
253                        "{name}: val.{name}.as_ref().map(|m| m.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect())"
254                    );
255                }
256                return format!(
257                    "{name}: val.{name}.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()"
258                );
259            }
260        }
261        // Vec<String>: sanitized from Vec<Box<str>>, Vec<Cow<str>>, Vec<Named>, etc.
262        // Use Debug formatting — the original core type may not implement Display.
263        if let TypeRef::Vec(inner) = ty {
264            if matches!(inner.as_ref(), TypeRef::String) {
265                if optional {
266                    return format!(
267                        "{name}: val.{name}.as_ref().map(|v| v.iter().map(|i| format!(\"{{:?}}\", i)).collect())"
268                    );
269                }
270                return format!("{name}: val.{name}.iter().map(|i| format!(\"{{:?}}\", i)).collect()");
271            }
272        }
273        // Optional<Vec<String>>: sanitized from Optional<Vec<Box<str>>>, Optional<Vec<Cow<str>>>, etc.
274        if let TypeRef::Optional(opt_inner) = ty {
275            if let TypeRef::Vec(vec_inner) = opt_inner.as_ref() {
276                if matches!(vec_inner.as_ref(), TypeRef::String) {
277                    return format!(
278                        "{name}: val.{name}.as_ref().map(|v| v.iter().map(|i| format!(\"{{:?}}\", i)).collect())"
279                    );
280                }
281            }
282        }
283        // String: sanitized from Box<str>, Cow<str>, (u32, u32), etc.
284        // Use Debug formatting — it works for all types (including tuples) and avoids Display
285        // trait bound failures when the original core type doesn't implement Display.
286        // Note: Cow<str> is handled before this point via the CoreWrapper::Cow path above.
287        if matches!(ty, TypeRef::String) {
288            if optional {
289                return format!("{name}: val.{name}.as_ref().map(|v| format!(\"{{v:?}}\"))");
290            }
291            return format!("{name}: format!(\"{{:?}}\", val.{name})");
292        }
293        // Fallback for truly unknown sanitized types — the core type may not implement Display,
294        // so use Debug formatting which is always available (required by the sanitized field's derive).
295        if optional {
296            return format!("{name}: val.{name}.as_ref().map(|v| format!(\"{{v:?}}\"))");
297        }
298        return format!("{name}: format!(\"{{:?}}\", val.{name})");
299    }
300    match ty {
301        // Duration: core uses std::time::Duration, binding uses u64 (millis)
302        TypeRef::Duration => {
303            if optional {
304                return format!("{name}: val.{name}.map(|d| d.as_millis() as u64)");
305            }
306            format!("{name}: val.{name}.as_millis() as u64")
307        }
308        // Path: core uses PathBuf, binding uses String — PathBuf→String needs special handling
309        TypeRef::Path => {
310            if optional {
311                format!("{name}: val.{name}.map(|p| p.to_string_lossy().to_string())")
312            } else {
313                format!("{name}: val.{name}.to_string_lossy().to_string()")
314            }
315        }
316        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Path) => {
317            format!("{name}: val.{name}.map(|p| p.to_string_lossy().to_string())")
318        }
319        // Char: core uses char, binding uses String — convert char to string
320        TypeRef::Char => {
321            if optional {
322                format!("{name}: val.{name}.map(|c| c.to_string())")
323            } else {
324                format!("{name}: val.{name}.to_string()")
325            }
326        }
327        // Bytes: core uses bytes::Bytes, binding uses Vec<u8> or napi `Buffer`.
328        // `.into()` is a no-op when destination is Vec<u8> (identity From) and
329        // a Vec→Buffer wrap when destination is `napi::bindgen_prelude::Buffer`.
330        TypeRef::Bytes => {
331            if optional {
332                format!("{name}: val.{name}.map(|v| v.to_vec().into())")
333            } else {
334                format!("{name}: val.{name}.to_vec().into()")
335            }
336        }
337        // Opaque Named types: wrap in Arc to create the binding wrapper
338        TypeRef::Named(n) if opaque_types.contains(n.as_str()) => {
339            if optional {
340                format!("{name}: val.{name}.map(|v| {n} {{ inner: Arc::new(v) }})")
341            } else {
342                format!("{name}: {n} {{ inner: Arc::new(val.{name}) }}")
343            }
344        }
345        // Json: core uses serde_json::Value, binding uses String — use .to_string()
346        TypeRef::Json => {
347            if optional {
348                format!("{name}: val.{name}.as_ref().map(ToString::to_string)")
349            } else {
350                format!("{name}: val.{name}.to_string()")
351            }
352        }
353        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Json) => {
354            format!("{name}: val.{name}.as_ref().map(ToString::to_string)")
355        }
356        TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Json) => {
357            if optional {
358                format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|i| i.to_string()).collect())")
359            } else {
360                format!("{name}: val.{name}.iter().map(ToString::to_string).collect()")
361            }
362        }
363        // Vec<Optional<Json>>: each element is Option<Value> → Option<String>
364        TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Optional(oi) if matches!(oi.as_ref(), TypeRef::Json)) => {
365            if optional {
366                format!(
367                    "{name}: val.{name}.as_ref().map(|v| v.iter().map(|i| i.as_ref().map(ToString::to_string)).collect())"
368                )
369            } else {
370                format!("{name}: val.{name}.iter().map(|i| i.as_ref().map(ToString::to_string)).collect()")
371            }
372        }
373        // Map with Json values: core uses HashMap<K, serde_json::Value>, binding uses HashMap<K, String>.
374        // Always emit `k.to_string()` so Cow<'_, str> / Box<str> / Arc<str> keys (which the type
375        // resolver normalizes to TypeRef::String) convert correctly. For an actual `String` key
376        // this is a clone, accepted under the existing `#[allow(clippy::useless_conversion)]`.
377        TypeRef::Map(_k, v) if matches!(v.as_ref(), TypeRef::Json) => {
378            if optional {
379                format!(
380                    "{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect())"
381                )
382            } else {
383                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()")
384            }
385        }
386        // Map with Json keys: core uses HashMap<serde_json::Value, V>, binding uses HashMap<String, V>
387        TypeRef::Map(k, _v) if matches!(k.as_ref(), TypeRef::Json) => {
388            if optional {
389                format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k.to_string(), v)).collect())")
390            } else {
391                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k.to_string(), v)).collect()")
392            }
393        }
394        // Map<String, String>: core may have Box<str> keys/values, binding has String keys/values.
395        // Emit .map() with .into() conversions, which are no-ops when both sides are String.
396        // This handles cases like HashMap<Box<str>, Box<str>> (core) → HashMap<String, String> (binding).
397        TypeRef::Map(k, v) if matches!(k.as_ref(), TypeRef::String) && matches!(v.as_ref(), TypeRef::String) => {
398            if optional {
399                format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k.into(), v.into())).collect())")
400            } else {
401                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k.into(), v.into())).collect()")
402            }
403        }
404        // Map<K, Bytes>: core uses bytes::Bytes (or Vec<u8>), binding uses Vec<u8> or napi Buffer.
405        // `.to_vec().into()` converts Bytes→Vec<u8> (identity for Vec<u8>) or Bytes→Buffer (napi).
406        TypeRef::Map(_k, v) if matches!(v.as_ref(), TypeRef::Bytes) => {
407            if optional {
408                format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k, v.to_vec().into())).collect())")
409            } else {
410                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k, v.to_vec().into())).collect()")
411            }
412        }
413        // Map<K, Named>: each value needs .into() to convert core→binding
414        TypeRef::Map(_k, v) if matches!(v.as_ref(), TypeRef::Named(_)) => {
415            if optional {
416                format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())")
417            } else {
418                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k, v.into())).collect()")
419            }
420        }
421        // Optional(Map<K, Named>): same but wrapped in Option
422        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Map(_k, v) if matches!(v.as_ref(), TypeRef::Named(_))) =>
423        {
424            format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k, v.into())).collect())")
425        }
426        // Vec<Named>: each element needs .into() to convert core→binding
427        TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Named(_)) => {
428            if optional {
429                format!("{name}: val.{name}.map(|v| v.into_iter().map(Into::into).collect())")
430            } else {
431                format!("{name}: val.{name}.into_iter().map(Into::into).collect()")
432            }
433        }
434        // Optional(Vec<Named>): same but wrapped in Option
435        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Named(_))) =>
436        {
437            format!("{name}: val.{name}.map(|v| v.into_iter().map(Into::into).collect())")
438        }
439        // Everything else is symmetric
440        _ => field_conversion_to_core(name, ty, optional),
441    }
442}
443
444/// Core→binding field conversion with backend-specific config.
445pub fn field_conversion_from_core_cfg(
446    name: &str,
447    ty: &TypeRef,
448    optional: bool,
449    sanitized: bool,
450    opaque_types: &AHashSet<String>,
451    config: &ConversionConfig,
452) -> String {
453    // Sanitized fields: for WASM (map_uses_jsvalue), Map and Vec<Json> fields target JsValue
454    // and need serde_wasm_bindgen::to_value() instead of iterator-based .collect().
455    // Note: Vec<String> sanitized does NOT use the JsValue path because Vec<String> maps to
456    // Vec<String> in WASM (not JsValue) — use the normal sanitized iterator path instead.
457    if sanitized {
458        if config.map_uses_jsvalue {
459            // Map(String, String) sanitized → JsValue (HashMap maps to JsValue in WASM)
460            // Use js_sys::JSON::parse(json_str) to get a plain JS object (not ES6 Map).
461            if let TypeRef::Map(k, v) = ty {
462                if matches!(k.as_ref(), TypeRef::String) && matches!(v.as_ref(), TypeRef::String) {
463                    if optional {
464                        return format!(
465                            "{name}: val.{name}.as_ref().and_then(|v| serde_json::to_string(v).ok()).and_then(|s| js_sys::JSON::parse(&s).ok())"
466                        );
467                    }
468                    return format!(
469                        "{name}: js_sys::JSON::parse(&serde_json::to_string(&val.{name}).unwrap_or_default()).unwrap_or(JsValue::NULL)"
470                    );
471                }
472            }
473            // Vec<Json> sanitized → JsValue (Vec<Json> maps to JsValue in WASM via nested-vec path)
474            if let TypeRef::Vec(inner) = ty {
475                if matches!(inner.as_ref(), TypeRef::Json) {
476                    if optional {
477                        return format!(
478                            "{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())"
479                        );
480                    }
481                    return format!("{name}: serde_wasm_bindgen::to_value(&val.{name}).unwrap_or(JsValue::NULL)");
482                }
483            }
484        }
485        return field_conversion_from_core(name, ty, optional, sanitized, opaque_types);
486    }
487
488    // Untagged data enum field (core holds the typed enum, binding holds serde_json::Value):
489    // serialize via serde_json::to_value.  Handles direct, Optional, and Vec wrappings.
490    if let Some(untagged_names) = config.untagged_data_enum_names {
491        let direct_named = matches!(ty, TypeRef::Named(n) if untagged_names.contains(n));
492        let optional_named = matches!(ty, TypeRef::Optional(inner)
493            if matches!(inner.as_ref(), TypeRef::Named(n) if untagged_names.contains(n)));
494        let vec_named = matches!(ty, TypeRef::Vec(inner)
495            if matches!(inner.as_ref(), TypeRef::Named(n) if untagged_names.contains(n)));
496        let optional_vec_named = matches!(ty, TypeRef::Optional(outer)
497            if matches!(outer.as_ref(), TypeRef::Vec(inner)
498                if matches!(inner.as_ref(), TypeRef::Named(n) if untagged_names.contains(n))));
499        if direct_named {
500            if optional {
501                return format!("{name}: val.{name}.as_ref().and_then(|v| serde_json::to_value(v).ok())");
502            }
503            return format!("{name}: serde_json::to_value(&val.{name}).unwrap_or(serde_json::Value::Null)");
504        }
505        if optional_named {
506            return format!("{name}: val.{name}.as_ref().and_then(|v| serde_json::to_value(v).ok())");
507        }
508        if vec_named {
509            if optional {
510                return format!(
511                    "{name}: val.{name}.as_ref().map(|v| v.iter().filter_map(|x| serde_json::to_value(x).ok()).collect())"
512                );
513            }
514            return format!("{name}: val.{name}.iter().filter_map(|x| serde_json::to_value(x).ok()).collect()");
515        }
516        if optional_vec_named {
517            return format!(
518                "{name}: val.{name}.as_ref().map(|v| v.iter().filter_map(|x| serde_json::to_value(x).ok()).collect())"
519            );
520        }
521    }
522
523    // Vec<Named>→String core→binding: binding holds JSON string, core has Vec<Named>.
524    // Only apply serde round-trip for Vec<Named> types (complex structs that can't cross FFI).
525    // Vec<String>, Vec<Primitive>, etc. stay as-is since they map directly.
526    if config.vec_named_to_string {
527        if let TypeRef::Vec(inner) = ty {
528            if matches!(inner.as_ref(), TypeRef::Named(_)) {
529                if optional {
530                    return format!("{name}: val.{name}.as_ref().and_then(|v| serde_json::to_string(v).ok())");
531                }
532                return format!("{name}: serde_json::to_string(&val.{name}).unwrap_or_default()");
533            }
534        }
535    }
536
537    // Map→String core→binding: binding holds Debug-formatted string, core has HashMap.
538    // Used by Rustler (Elixir NIFs) where HashMap cannot cross the NIF boundary directly.
539    if config.map_as_string && matches!(ty, TypeRef::Map(_, _)) {
540        if optional {
541            return format!("{name}: val.{name}.as_ref().map(|m| format!(\"{{m:?}}\"))");
542        }
543        return format!("{name}: format!(\"{{:?}}\", val.{name})");
544    }
545    if config.map_as_string {
546        if let TypeRef::Optional(inner) = ty {
547            if matches!(inner.as_ref(), TypeRef::Map(_, _)) {
548                return format!("{name}: val.{name}.as_ref().map(|m| format!(\"{{m:?}}\"))");
549            }
550        }
551    }
552
553    // WASM JsValue: use js_sys::JSON::parse for Map types (produces plain JS objects, not ES6
554    // Maps which serde_wasm_bindgen would produce for serialize_map calls). Use
555    // serde_wasm_bindgen for nested Vec types.
556    if config.map_uses_jsvalue {
557        let is_nested_vec = matches!(ty, TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Vec(_)));
558        let is_map = matches!(ty, TypeRef::Map(_, _));
559        if is_map {
560            if optional {
561                return format!(
562                    "{name}: val.{name}.as_ref().and_then(|v| serde_json::to_string(v).ok()).and_then(|s| js_sys::JSON::parse(&s).ok())"
563                );
564            }
565            return format!(
566                "{name}: js_sys::JSON::parse(&serde_json::to_string(&val.{name}).unwrap_or_default()).unwrap_or(JsValue::NULL)"
567            );
568        }
569        if is_nested_vec {
570            if optional {
571                return format!("{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())");
572            }
573            return format!("{name}: serde_wasm_bindgen::to_value(&val.{name}).unwrap_or(JsValue::NULL)");
574        }
575        if let TypeRef::Optional(inner) = ty {
576            let is_inner_nested = matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Vec(_)));
577            let is_inner_map = matches!(inner.as_ref(), TypeRef::Map(_, _));
578            if is_inner_map {
579                return format!(
580                    "{name}: val.{name}.as_ref().and_then(|v| serde_json::to_string(v).ok()).and_then(|s| js_sys::JSON::parse(&s).ok())"
581                );
582            }
583            if is_inner_nested {
584                return format!("{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())");
585            }
586        }
587    }
588
589    let prefix = config.type_name_prefix;
590    let is_enum_string = |n: &str| -> bool { config.enum_string_names.as_ref().is_some_and(|names| names.contains(n)) };
591
592    match ty {
593        // i64 casting for large int primitives
594        TypeRef::Primitive(p) if config.cast_large_ints_to_i64 && needs_i64_cast(p) => {
595            let cast_to = binding_prim_str(p);
596            if optional {
597                format!("{name}: val.{name}.map(|v| v as {cast_to})")
598            } else {
599                format!("{name}: val.{name} as {cast_to}")
600            }
601        }
602        // Optional(large_int) with i64 casting
603        TypeRef::Optional(inner)
604            if config.cast_large_ints_to_i64
605                && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p)) =>
606        {
607            if let TypeRef::Primitive(p) = inner.as_ref() {
608                let cast_to = binding_prim_str(p);
609                format!("{name}: val.{name}.map(|v| v as {cast_to})")
610            } else {
611                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
612            }
613        }
614        // i32 casting for small uint primitives (extendr/R only)
615        TypeRef::Primitive(p) if config.cast_uints_to_i32 && needs_i32_cast(p) => {
616            if optional {
617                format!("{name}: val.{name}.map(|v| v as i32)")
618            } else {
619                format!("{name}: val.{name} as i32")
620            }
621        }
622        // Optional(small_uint) with i32 casting
623        TypeRef::Optional(inner)
624            if config.cast_uints_to_i32 && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i32_cast(p)) =>
625        {
626            format!("{name}: val.{name}.map(|v| v as i32)")
627        }
628        // Vec<u8/u16/u32/i8/i16> needs element-wise core→i32 casting (extendr/R only)
629        TypeRef::Vec(inner)
630            if config.cast_uints_to_i32 && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i32_cast(p)) =>
631        {
632            if let TypeRef::Primitive(_p) = inner.as_ref() {
633                if optional {
634                    format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as i32).collect())")
635                } else {
636                    format!("{name}: val.{name}.iter().map(|&v| v as i32).collect()")
637                }
638            } else {
639                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
640            }
641        }
642        // f64 casting for large int primitives (extendr/R only)
643        TypeRef::Primitive(p) if config.cast_large_ints_to_f64 && needs_f64_cast(p) => {
644            if optional {
645                format!("{name}: val.{name}.map(|v| v as f64)")
646            } else {
647                format!("{name}: val.{name} as f64")
648            }
649        }
650        // Optional(large_int) with f64 casting
651        TypeRef::Optional(inner)
652            if config.cast_large_ints_to_f64
653                && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p)) =>
654        {
655            format!("{name}: val.{name}.map(|v| v as f64)")
656        }
657        // Vec<usize/u64/i64/isize/f32> needs element-wise f64 cast for extendr/R backend
658        TypeRef::Vec(inner)
659            if config.cast_large_ints_to_f64
660                && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p)) =>
661        {
662            if optional {
663                format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as f64).collect())")
664            } else {
665                format!("{name}: val.{name}.iter().map(|&v| v as f64).collect()")
666            }
667        }
668        // Optional(Vec(usize/u64/i64/isize/f32)) needs element-wise f64 cast
669        TypeRef::Optional(inner)
670            if config.cast_large_ints_to_f64
671                && matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p))) =>
672        {
673            format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as f64).collect())")
674        }
675        // Vec<Vec<usize/u64/i64/isize/f32>> needs nested element-wise f64 cast (embeddings)
676        TypeRef::Vec(outer)
677            if config.cast_large_ints_to_f64
678                && matches!(outer.as_ref(), TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p))) =>
679        {
680            if optional {
681                format!(
682                    "{name}: val.{name}.as_ref().map(|v| v.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect())"
683                )
684            } else {
685                format!("{name}: val.{name}.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect()")
686            }
687        }
688        // Optional(Vec<Vec<usize/u64/i64/isize/f32>>) needs nested element-wise f64 cast
689        TypeRef::Optional(inner)
690            if config.cast_large_ints_to_f64
691                && matches!(inner.as_ref(), TypeRef::Vec(outer) if matches!(outer.as_ref(), TypeRef::Vec(prim) if matches!(prim.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p)))) =>
692        {
693            format!(
694                "{name}: val.{name}.as_ref().map(|v| v.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect())"
695            )
696        }
697        // Map values that are usize/u64/i64/isize/f32 stored as f64 in binding → cast when reading core
698        TypeRef::Map(_k, v)
699            if config.cast_large_ints_to_f64 && matches!(v.as_ref(), TypeRef::Primitive(p) if needs_f64_cast(p)) =>
700        {
701            if optional {
702                format!("{name}: val.{name}.as_ref().map(|m| m.iter().map(|(k, v)| (k.clone(), *v as f64)).collect())")
703            } else {
704                format!("{name}: val.{name}.iter().map(|(k, v)| (k.clone(), *v as f64)).collect()")
705            }
706        }
707        // Duration with f64 casting (R: no u64, use f64 millis)
708        TypeRef::Duration if config.cast_large_ints_to_f64 => {
709            if optional {
710                format!("{name}: val.{name}.map(|d| d.as_millis() as f64)")
711            } else {
712                format!("{name}: val.{name}.as_millis() as f64")
713            }
714        }
715        // f32→f64 casting (NAPI only)
716        TypeRef::Primitive(PrimitiveType::F32) if config.cast_f32_to_f64 => {
717            if optional {
718                format!("{name}: val.{name}.map(|v| v as f64)")
719            } else {
720                format!("{name}: val.{name} as f64")
721            }
722        }
723        // Duration with i64 casting
724        TypeRef::Duration if config.cast_large_ints_to_i64 => {
725            if optional {
726                format!("{name}: val.{name}.map(|d| d.as_millis() as u64 as i64)")
727            } else {
728                format!("{name}: val.{name}.as_millis() as u64 as i64")
729            }
730        }
731        // Opaque Named types with prefix: wrap in Arc with prefixed binding name
732        TypeRef::Named(n) if opaque_types.contains(n.as_str()) && !prefix.is_empty() => {
733            let prefixed = format!("{prefix}{n}");
734            if optional {
735                format!("{name}: val.{name}.map(|v| {prefixed} {{ inner: Arc::new(v) }})")
736            } else {
737                format!("{name}: {prefixed} {{ inner: Arc::new(val.{name}) }}")
738            }
739        }
740        // Enum-to-String Named types (PHP pattern)
741        TypeRef::Named(n) if is_enum_string(n) => {
742            // Use serde serialization to get the correct serde(rename) value, not Debug format.
743            // serde_json::to_value gives Value::String("auto") which we extract.
744            if optional {
745                format!(
746                    "{name}: val.{name}.as_ref().map(|v| serde_json::to_value(v).ok().and_then(|s| s.as_str().map(String::from)).unwrap_or_default())"
747                )
748            } else {
749                format!(
750                    "{name}: serde_json::to_value(val.{name}).ok().and_then(|s| s.as_str().map(String::from)).unwrap_or_default()"
751                )
752            }
753        }
754        // Vec<Enum-to-String> Named types: element-wise serde serialization
755        TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Named(n) if is_enum_string(n)) => {
756            if optional {
757                format!(
758                    "{name}: val.{name}.as_ref().map(|v| v.iter().map(|x| serde_json::to_value(x).ok().and_then(|s| s.as_str().map(String::from)).unwrap_or_default()).collect())"
759                )
760            } else {
761                format!(
762                    "{name}: val.{name}.iter().map(|v| serde_json::to_value(v).ok().and_then(|s| s.as_str().map(String::from)).unwrap_or_default()).collect()"
763                )
764            }
765        }
766        // Optional(Vec<Enum-to-String>) Named types (PHP pattern)
767        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Named(n) if is_enum_string(n))) =>
768        {
769            format!(
770                "{name}: val.{name}.as_ref().map(|v| v.iter().map(|x| serde_json::to_value(x).ok().and_then(|s| s.as_str().map(String::from)).unwrap_or_default()).collect())"
771            )
772        }
773        // Vec<f32> needs element-wise cast to f64 when f32→f64 mapping is active
774        TypeRef::Vec(inner)
775            if config.cast_f32_to_f64 && matches!(inner.as_ref(), TypeRef::Primitive(PrimitiveType::F32)) =>
776        {
777            if optional {
778                format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as f64).collect())")
779            } else {
780                format!("{name}: val.{name}.iter().map(|&v| v as f64).collect()")
781            }
782        }
783        // Optional(Vec(f32)) needs element-wise cast to f64
784        TypeRef::Optional(inner)
785            if config.cast_f32_to_f64
786                && matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Primitive(PrimitiveType::F32))) =>
787        {
788            format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as f64).collect())")
789        }
790        // Optional(Vec(u64/usize/isize)) needs element-wise i64 casting
791        TypeRef::Optional(inner)
792            if config.cast_large_ints_to_i64
793                && matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p))) =>
794        {
795            if let TypeRef::Vec(vi) = inner.as_ref() {
796                if let TypeRef::Primitive(p) = vi.as_ref() {
797                    let cast_to = binding_prim_str(p);
798                    if sanitized {
799                        // Sanitized from Option<(T, T)> → Option<Vec<T>>: destructure tuple
800                        format!("{name}: val.{name}.map(|(a, b)| vec![a as {cast_to}, b as {cast_to}])")
801                    } else {
802                        format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as {cast_to}).collect())")
803                    }
804                } else {
805                    field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
806                }
807            } else {
808                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
809            }
810        }
811        // Vec<Vec<f32>> needs nested element-wise cast to f64 (for embeddings, etc.)
812        TypeRef::Vec(outer)
813            if config.cast_f32_to_f64
814                && matches!(outer.as_ref(), TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Primitive(PrimitiveType::F32))) =>
815        {
816            if optional {
817                format!(
818                    "{name}: val.{name}.as_ref().map(|v| v.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect())"
819                )
820            } else {
821                format!("{name}: val.{name}.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect()")
822            }
823        }
824        // Optional(Vec<Vec<f32>>) needs nested element-wise cast to f64
825        TypeRef::Optional(inner)
826            if config.cast_f32_to_f64
827                && matches!(inner.as_ref(), TypeRef::Vec(outer) if matches!(outer.as_ref(), TypeRef::Vec(prim) if matches!(prim.as_ref(), TypeRef::Primitive(PrimitiveType::F32)))) =>
828        {
829            format!(
830                "{name}: val.{name}.as_ref().map(|v| v.iter().map(|inner| inner.iter().map(|&x| x as f64).collect()).collect())"
831            )
832        }
833        // Optional with i64-cast inner
834        TypeRef::Optional(inner)
835            if config.cast_large_ints_to_i64
836                && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p)) =>
837        {
838            if let TypeRef::Primitive(p) = inner.as_ref() {
839                let cast_to = binding_prim_str(p);
840                format!("{name}: val.{name}.map(|v| v as {cast_to})")
841            } else {
842                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
843            }
844        }
845        // HashMap value type casting: when value type needs i64 casting
846        TypeRef::Map(_k, v)
847            if config.cast_large_ints_to_i64 && matches!(v.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p)) =>
848        {
849            if let TypeRef::Primitive(p) = v.as_ref() {
850                let cast_to = binding_prim_str(p);
851                if optional {
852                    format!(
853                        "{name}: val.{name}.as_ref().map(|m| m.iter().map(|(k, v)| (k.clone(), *v as {cast_to})).collect())"
854                    )
855                } else {
856                    format!("{name}: val.{name}.iter().map(|(k, v)| (k.clone(), *v as {cast_to})).collect()")
857                }
858            } else {
859                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
860            }
861        }
862        // Vec<u64/usize/isize> needs element-wise i64 casting (core→binding)
863        TypeRef::Vec(inner)
864            if config.cast_large_ints_to_i64
865                && matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p)) =>
866        {
867            if let TypeRef::Primitive(p) = inner.as_ref() {
868                let cast_to = binding_prim_str(p);
869                if sanitized {
870                    // Sanitized from tuple (T, T) → Vec<T>: destructure tuple into vec
871                    if optional {
872                        format!("{name}: val.{name}.map(|(a, b)| vec![a as {cast_to}, b as {cast_to}])")
873                    } else {
874                        format!("{name}: {{ let (a, b) = val.{name}; vec![a as {cast_to}, b as {cast_to}] }}")
875                    }
876                } else if optional {
877                    format!("{name}: val.{name}.as_ref().map(|v| v.iter().map(|&x| x as {cast_to}).collect())")
878                } else {
879                    format!("{name}: val.{name}.iter().map(|&v| v as {cast_to}).collect()")
880                }
881            } else {
882                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
883            }
884        }
885        // Vec<Vec<u64/usize/isize>> needs nested element-wise i64 casting (core→binding)
886        TypeRef::Vec(outer)
887            if config.cast_large_ints_to_i64
888                && matches!(outer.as_ref(), TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::Primitive(p) if needs_i64_cast(p))) =>
889        {
890            if let TypeRef::Vec(inner) = outer.as_ref() {
891                if let TypeRef::Primitive(p) = inner.as_ref() {
892                    let cast_to = binding_prim_str(p);
893                    if optional {
894                        format!(
895                            "{name}: val.{name}.as_ref().map(|v| v.iter().map(|inner| inner.iter().map(|&x| x as {cast_to}).collect()).collect())"
896                        )
897                    } else {
898                        format!(
899                            "{name}: val.{name}.iter().map(|inner| inner.iter().map(|&x| x as {cast_to}).collect()).collect()"
900                        )
901                    }
902                } else {
903                    field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
904                }
905            } else {
906                field_conversion_from_core(name, ty, optional, sanitized, opaque_types)
907            }
908        }
909        // Json→String: core uses serde_json::Value, binding uses String (PHP)
910        TypeRef::Json if config.json_to_string => {
911            if optional {
912                format!("{name}: val.{name}.as_ref().map(ToString::to_string)")
913            } else {
914                format!("{name}: val.{name}.to_string()")
915            }
916        }
917        // Json stays as serde_json::Value: identity passthrough.
918        TypeRef::Json if config.json_as_value => {
919            format!("{name}: val.{name}")
920        }
921        TypeRef::Optional(inner) if config.json_as_value && matches!(inner.as_ref(), TypeRef::Json) => {
922            format!("{name}: val.{name}")
923        }
924        TypeRef::Vec(inner) if config.json_as_value && matches!(inner.as_ref(), TypeRef::Json) => {
925            if optional {
926                format!("{name}: Some(val.{name})")
927            } else {
928                format!("{name}: val.{name}")
929            }
930        }
931        TypeRef::Map(_k, v) if config.json_as_value && matches!(v.as_ref(), TypeRef::Json) => {
932            if optional {
933                format!("{name}: val.{name}.map(|m| m.into_iter().map(|(k, v)| (k.into(), v)).collect())")
934            } else {
935                format!("{name}: val.{name}.into_iter().map(|(k, v)| (k.into(), v)).collect()")
936            }
937        }
938        // Json→JsValue: core uses serde_json::Value, binding uses JsValue (WASM)
939        TypeRef::Json if config.map_uses_jsvalue => {
940            if optional {
941                format!("{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())")
942            } else {
943                format!("{name}: serde_wasm_bindgen::to_value(&val.{name}).unwrap_or(JsValue::NULL)")
944            }
945        }
946        // Vec<Json>→JsValue: core uses Vec<serde_json::Value>, binding uses JsValue (WASM)
947        TypeRef::Vec(inner) if config.map_uses_jsvalue && matches!(inner.as_ref(), TypeRef::Json) => {
948            if optional {
949                format!("{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())")
950            } else {
951                format!("{name}: serde_wasm_bindgen::to_value(&val.{name}).unwrap_or(JsValue::NULL)")
952            }
953        }
954        // Optional(Vec<Json>)→JsValue (WASM)
955        TypeRef::Optional(inner)
956            if config.map_uses_jsvalue
957                && matches!(inner.as_ref(), TypeRef::Vec(vi) if matches!(vi.as_ref(), TypeRef::Json)) =>
958        {
959            format!("{name}: val.{name}.as_ref().and_then(|v| serde_wasm_bindgen::to_value(v).ok())")
960        }
961        // Fall through to default (handles paths, opaque without prefix, etc.)
962        _ => field_conversion_from_core(name, ty, optional, sanitized, opaque_types),
963    }
964}
965
966/// Apply CoreWrapper transformations for core→binding direction.
967/// Unwraps Arc, converts Cow→String, Bytes→Vec<u8>.
968fn apply_core_wrapper_from_core(
969    conversion: &str,
970    name: &str,
971    core_wrapper: &CoreWrapper,
972    vec_inner_core_wrapper: &CoreWrapper,
973    optional: bool,
974) -> String {
975    // Handle Vec<Arc<T>>: unwrap Arc elements
976    if *vec_inner_core_wrapper == CoreWrapper::Arc {
977        return conversion
978            .replace(".map(Into::into).collect()", ".map(|v| (*v).clone().into()).collect()")
979            .replace(
980                "map(|v| v.into_iter().map(Into::into)",
981                "map(|v| v.into_iter().map(|v| (*v).clone().into())",
982            );
983    }
984
985    match core_wrapper {
986        CoreWrapper::None => conversion.to_string(),
987        CoreWrapper::Cow => {
988            // Cow<str> → String: core val.name is Cow<'static, str>, binding needs String.
989            // Always emit val.{name}.into_owned() regardless of what the base conversion emits.
990            // This handles both the normal path (base = "name: val.name") and the sanitized path
991            // (base = "name: format!(\"{:?}\", val.name)") which produces debug-escaped strings.
992            // When the binding has been optionalized (e.g. NAPI default-optional fields), the
993            // upstream pass already wrapped the conversion in Some(...) — preserve that wrap.
994            let prefix = format!("{name}: ");
995            let already_some_wrapped = conversion
996                .strip_prefix(&prefix)
997                .is_some_and(|expr| expr.starts_with("Some("));
998            if optional {
999                format!("{name}: val.{name}.as_ref().map(|v| v.to_string())")
1000            } else if already_some_wrapped {
1001                format!("{name}: Some(val.{name}.to_string())")
1002            } else {
1003                format!("{name}: val.{name}.to_string()")
1004            }
1005        }
1006        CoreWrapper::Arc => {
1007            // Arc<T> → T: unwrap via clone.
1008            //
1009            // Special case: opaque Named types build the binding wrapper with
1010            // `{ inner: Arc::new(v) }` in the base conversion, but when the core
1011            // field is `Arc<T>`, `v` IS already the `Arc<T>` — wrapping it again
1012            // with `Arc::new` produces `Arc<Arc<T>>`.  Detect this pattern and
1013            // replace `Arc::new(v)` with `v`, and `Arc::new(val.{name})` with
1014            // `val.{name}`, then return without adding an extra unwrap chain.
1015            if conversion.contains("{ inner: Arc::new(") {
1016                return conversion.replace("{ inner: Arc::new(v) }", "{ inner: v }").replace(
1017                    &format!("{{ inner: Arc::new(val.{name}) }}"),
1018                    &format!("{{ inner: val.{name} }}"),
1019                );
1020            }
1021            if let Some(expr) = conversion.strip_prefix(&format!("{name}: ")) {
1022                if optional {
1023                    // When the base conversion is the simple passthrough `val.{name}`,
1024                    // the Option carries Arc<T> elements; deref-clone each.
1025                    // When the base is already a complex expression (e.g.
1026                    // `val.{name}.as_ref().map(ToString::to_string)` for Json fields),
1027                    // the Arc is transparently handled via Display/Deref coercion;
1028                    // chaining another `.map(|v| (*v).clone().into())` would operate
1029                    // on the already-converted value (e.g. String) and emit invalid
1030                    // codegen such as `(*String).clone()` (since str: !Clone).
1031                    let simple_passthrough = format!("val.{name}");
1032                    if expr == simple_passthrough {
1033                        format!("{name}: {expr}.map(|v| (*v).clone().into())")
1034                    } else {
1035                        format!("{name}: {expr}")
1036                    }
1037                } else {
1038                    let unwrapped = expr.replace(&format!("val.{name}"), &format!("(*val.{name}).clone()"));
1039                    format!("{name}: {unwrapped}")
1040                }
1041            } else {
1042                conversion.to_string()
1043            }
1044        }
1045        CoreWrapper::Bytes => {
1046            // Bytes → Vec<u8> (or napi Buffer via From<Vec<u8>>): .to_vec().into()
1047            // The TypeRef::Bytes field_conversion already emits the correct expression
1048            // (`.to_vec().into()` non-optional, `.map(|v| v.to_vec().into())` optional).
1049            // Detect those forms and pass through unchanged to avoid double conversion.
1050            if let Some(expr) = conversion.strip_prefix(&format!("{name}: ")) {
1051                let already_converted_non_opt = expr == format!("val.{name}.to_vec().into()");
1052                let already_converted_opt = expr == format!("val.{name}.map(|v| v.to_vec().into())");
1053                if already_converted_non_opt || already_converted_opt {
1054                    conversion.to_string()
1055                } else if optional {
1056                    format!("{name}: {expr}.map(|v| v.to_vec().into())")
1057                } else if expr == format!("val.{name}") {
1058                    format!("{name}: val.{name}.to_vec().into()")
1059                } else {
1060                    conversion.to_string()
1061                }
1062            } else {
1063                conversion.to_string()
1064            }
1065        }
1066        CoreWrapper::ArcMutex => {
1067            // Arc<Mutex<T>> → T: lock and clone
1068            if let Some(expr) = conversion.strip_prefix(&format!("{name}: ")) {
1069                if optional {
1070                    format!("{name}: {expr}.map(|v| v.lock().unwrap().clone().into())")
1071                } else if expr == format!("val.{name}") {
1072                    format!("{name}: val.{name}.lock().unwrap().clone().into()")
1073                } else {
1074                    conversion.to_string()
1075                }
1076            } else {
1077                conversion.to_string()
1078            }
1079        }
1080    }
1081}