Skip to main content

alef_codegen/conversions/
helpers.rs

1use ahash::{AHashMap, AHashSet};
2use alef_core::ir::{ApiSurface, EnumDef, FieldDef, PrimitiveType, TypeDef, TypeRef};
3
4use crate::conversions::ConversionConfig;
5
6/// Collect all Named type names that appear in the API surface — both as
7/// function/method input parameters AND as function/method return types.
8/// These are types that need binding→core `From` impls.
9///
10/// Return types need binding→core From impls because:
11/// - Users may construct binding types and convert them to core types
12/// - Generated code may use `.into()` on nested Named fields in From impls
13/// - Round-trip conversion completeness ensures the API is fully usable
14///
15/// The result includes transitive dependencies: if `ConversionResult` is a
16/// return type and it has a field `metadata: HtmlMetadata`, then `HtmlMetadata`
17/// is also included.
18pub fn input_type_names(surface: &ApiSurface) -> AHashSet<String> {
19    let mut names = AHashSet::new();
20
21    // Collect Named types from function params
22    for func in &surface.functions {
23        for param in &func.params {
24            collect_named_types(&param.ty, &mut names);
25        }
26    }
27    // Collect Named types from method params
28    for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
29        for method in &typ.methods {
30            for param in &method.params {
31                collect_named_types(&param.ty, &mut names);
32            }
33        }
34    }
35    // Collect Named types from function return types.
36    // Return types and their transitive field types need binding→core From impls
37    // for round-trip conversion completeness.
38    for func in &surface.functions {
39        collect_named_types(&func.return_type, &mut names);
40    }
41    // Collect Named types from method return types.
42    for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
43        for method in &typ.methods {
44            collect_named_types(&method.return_type, &mut names);
45        }
46    }
47    // Collect Named types from fields of non-opaque types that have methods.
48    // When a non-opaque type has methods, codegen generates binding→core struct conversion
49    // (gen_lossy_binding_to_core_fields) which calls `.into()` on Named fields.
50    // Those field types need binding→core From impls.
51    for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
52        if !typ.is_opaque && !typ.methods.is_empty() {
53            for field in &typ.fields {
54                if !field.sanitized {
55                    collect_named_types(&field.ty, &mut names);
56                }
57            }
58        }
59    }
60
61    // Collect Named types from enum variant fields — enum variants that are
62    // used as input types need From impls for their nested Named fields too.
63    for e in &surface.enums {
64        if names.contains(&e.name) {
65            for variant in &e.variants {
66                for field in &variant.fields {
67                    collect_named_types(&field.ty, &mut names);
68                }
69            }
70        }
71    }
72
73    // Transitive closure: if type A is an input and has field of type B, B is also an input
74    let mut changed = true;
75    while changed {
76        changed = false;
77        let snapshot: Vec<String> = names.iter().cloned().collect();
78        for name in &snapshot {
79            if let Some(typ) = surface.types.iter().find(|t| t.name == *name) {
80                for field in &typ.fields {
81                    let mut field_names = AHashSet::new();
82                    collect_named_types(&field.ty, &mut field_names);
83                    for n in field_names {
84                        if names.insert(n) {
85                            changed = true;
86                        }
87                    }
88                }
89            }
90            // Also walk enum variant fields in the transitive closure
91            if let Some(e) = surface.enums.iter().find(|e| e.name == *name) {
92                for variant in &e.variants {
93                    for field in &variant.fields {
94                        let mut field_names = AHashSet::new();
95                        collect_named_types(&field.ty, &mut field_names);
96                        for n in field_names {
97                            if names.insert(n) {
98                                changed = true;
99                            }
100                        }
101                    }
102                }
103            }
104        }
105    }
106
107    names
108}
109
110/// Recursively collect all `Named(name)` from a TypeRef.
111fn collect_named_types(ty: &TypeRef, out: &mut AHashSet<String>) {
112    match ty {
113        TypeRef::Named(name) => {
114            out.insert(name.clone());
115        }
116        TypeRef::Optional(inner) | TypeRef::Vec(inner) => collect_named_types(inner, out),
117        TypeRef::Map(k, v) => {
118            collect_named_types(k, out);
119            collect_named_types(v, out);
120        }
121        _ => {}
122    }
123}
124
125/// Check if a TypeRef references a Named type that is in the exclude list.
126/// Used to skip fields whose types were excluded from binding generation,
127/// preventing references to non-existent wrapper types (e.g. Js* in WASM).
128pub fn field_references_excluded_type(ty: &TypeRef, exclude_types: &[String]) -> bool {
129    match ty {
130        TypeRef::Named(name) => exclude_types.iter().any(|e| e == name),
131        TypeRef::Optional(inner) | TypeRef::Vec(inner) => field_references_excluded_type(inner, exclude_types),
132        TypeRef::Map(k, v) => {
133            field_references_excluded_type(k, exclude_types) || field_references_excluded_type(v, exclude_types)
134        }
135        _ => false,
136    }
137}
138
139/// Returns true if a primitive type needs i64 casting (NAPI/PHP — JS/PHP lack native u64).
140pub(crate) fn needs_i64_cast(p: &PrimitiveType) -> bool {
141    matches!(p, PrimitiveType::U64 | PrimitiveType::Usize | PrimitiveType::Isize)
142}
143
144/// Returns the core primitive type string for cast primitives.
145pub(crate) fn core_prim_str(p: &PrimitiveType) -> &'static str {
146    match p {
147        PrimitiveType::U64 => "u64",
148        PrimitiveType::Usize => "usize",
149        PrimitiveType::Isize => "isize",
150        PrimitiveType::F32 => "f32",
151        PrimitiveType::Bool => "bool",
152        PrimitiveType::U8 => "u8",
153        PrimitiveType::U16 => "u16",
154        PrimitiveType::U32 => "u32",
155        PrimitiveType::I8 => "i8",
156        PrimitiveType::I16 => "i16",
157        PrimitiveType::I32 => "i32",
158        PrimitiveType::I64 => "i64",
159        PrimitiveType::F64 => "f64",
160    }
161}
162
163/// Returns the binding primitive type string for cast primitives (core→binding direction).
164pub(crate) fn binding_prim_str(p: &PrimitiveType) -> &'static str {
165    match p {
166        PrimitiveType::U64 | PrimitiveType::Usize | PrimitiveType::Isize => "i64",
167        PrimitiveType::F32 => "f64",
168        PrimitiveType::Bool => "bool",
169        PrimitiveType::U8 | PrimitiveType::U16 | PrimitiveType::U32 => "i32",
170        PrimitiveType::I8 | PrimitiveType::I16 | PrimitiveType::I32 => "i32",
171        PrimitiveType::I64 => "i64",
172        PrimitiveType::F64 => "f64",
173    }
174}
175
176/// Build the set of types that can have core→binding From safely generated.
177/// More permissive than binding→core: allows sanitized fields (uses format!("{:?}"))
178/// and accepts data enums (data discarded with `..` in match arms).
179pub fn core_to_binding_convertible_types(surface: &ApiSurface) -> AHashSet<String> {
180    let convertible_enums: AHashSet<&str> = surface
181        .enums
182        .iter()
183        .filter(|e| can_generate_enum_conversion_from_core(e))
184        .map(|e| e.name.as_str())
185        .collect();
186
187    let opaque_type_names: AHashSet<&str> = surface
188        .types
189        .iter()
190        .filter(|t| t.is_opaque)
191        .map(|t| t.name.as_str())
192        .collect();
193
194    // Data enums (enums with data variants) are generated as opaque wrappers in most
195    // backends. They don't have From conversions but are still "known" types — structs
196    // containing them can still generate core→binding conversions (the field uses the
197    // opaque wrapper's From impl or format!("{:?}")).
198    let data_enum_names: AHashSet<&str> = surface
199        .enums
200        .iter()
201        .filter(|e| e.variants.iter().any(|v| !v.fields.is_empty()))
202        .map(|e| e.name.as_str())
203        .collect();
204
205    // Build rust_path maps for detecting type_rust_path mismatches.
206    let (enum_paths, type_paths) = build_rust_path_maps(surface);
207
208    // All non-opaque types are candidates (sanitized fields use format!("{:?}"))
209    let mut convertible: AHashSet<String> = surface
210        .types
211        .iter()
212        .filter(|t| !t.is_opaque)
213        .map(|t| t.name.clone())
214        .collect();
215
216    let mut changed = true;
217    while changed {
218        changed = false;
219        let snapshot: Vec<String> = convertible.iter().cloned().collect();
220        let mut known: AHashSet<&str> = convertible.iter().map(|s| s.as_str()).collect();
221        known.extend(&opaque_type_names);
222        known.extend(&data_enum_names);
223        let mut to_remove = Vec::new();
224        for type_name in &snapshot {
225            if let Some(typ) = surface.types.iter().find(|t| t.name == *type_name) {
226                let ok = typ.fields.iter().all(|f| {
227                    if f.sanitized {
228                        true
229                    } else if field_has_path_mismatch(f, &enum_paths, &type_paths) {
230                        false
231                    } else {
232                        is_field_convertible(&f.ty, &convertible_enums, &known)
233                    }
234                });
235                if !ok {
236                    to_remove.push(type_name.clone());
237                }
238            }
239        }
240        for name in to_remove {
241            if convertible.remove(&name) {
242                changed = true;
243            }
244        }
245    }
246    convertible
247}
248
249/// Build the set of types that can have binding→core From safely generated.
250/// Strict: excludes types with sanitized fields (lossy conversion).
251/// This is transitive: a type is convertible only if all its Named field types
252/// are also convertible (or are enums with From/Into support).
253pub fn convertible_types(surface: &ApiSurface) -> AHashSet<String> {
254    // Build set of enums that have From/Into impls (unit-variant enums only)
255    let convertible_enums: AHashSet<&str> = surface
256        .enums
257        .iter()
258        .filter(|e| can_generate_enum_conversion(e))
259        .map(|e| e.name.as_str())
260        .collect();
261
262    // Build set of all known type names (including opaques) — opaque Named fields
263    // are convertible because we wrap/unwrap them via Arc.
264    let _all_type_names: AHashSet<&str> = surface.types.iter().map(|t| t.name.as_str()).collect();
265
266    // Build set of Named types that implement Default — sanitized fields referencing
267    // Named types without Default would cause a compile error in the generated From impl.
268    let default_type_names: AHashSet<&str> = surface
269        .types
270        .iter()
271        .filter(|t| t.has_default)
272        .map(|t| t.name.as_str())
273        .collect();
274
275    // Start with all non-opaque types as candidates.
276    // Types with sanitized fields use Default::default() for the sanitized field
277    // in the binding→core direction — but only if the field type implements Default.
278    let mut convertible: AHashSet<String> = surface
279        .types
280        .iter()
281        .filter(|t| !t.is_opaque)
282        .map(|t| t.name.clone())
283        .collect();
284
285    // Set of opaque type names — Named fields referencing opaques are always convertible
286    // (they use Arc wrap/unwrap), so include them in the known-types check.
287    let opaque_type_names: AHashSet<&str> = surface
288        .types
289        .iter()
290        .filter(|t| t.is_opaque)
291        .map(|t| t.name.as_str())
292        .collect();
293
294    // Data enums (enums with data variants) are generated as opaque wrappers — include
295    // them in the known set so structs containing them remain convertible.
296    let data_enum_names: AHashSet<&str> = surface
297        .enums
298        .iter()
299        .filter(|e| e.variants.iter().any(|v| !v.fields.is_empty()))
300        .map(|e| e.name.as_str())
301        .collect();
302
303    // Build rust_path maps for detecting type_rust_path mismatches.
304    let (enum_paths, type_paths) = build_rust_path_maps(surface);
305
306    // Iteratively remove types whose fields reference non-convertible Named types.
307    // We check against `convertible ∪ opaque_types ∪ data_enums` so that types
308    // referencing excluded types are transitively removed, while opaque and data
309    // enum Named fields remain valid.
310    let mut changed = true;
311    while changed {
312        changed = false;
313        let snapshot: Vec<String> = convertible.iter().cloned().collect();
314        let mut known: AHashSet<&str> = convertible.iter().map(|s| s.as_str()).collect();
315        known.extend(&opaque_type_names);
316        known.extend(&data_enum_names);
317        let mut to_remove = Vec::new();
318        for type_name in &snapshot {
319            if let Some(typ) = surface.types.iter().find(|t| t.name == *type_name) {
320                let ok = typ.fields.iter().all(|f| {
321                    if f.sanitized {
322                        sanitized_field_has_default(&f.ty, &default_type_names)
323                    } else if field_has_path_mismatch(f, &enum_paths, &type_paths) {
324                        false
325                    } else {
326                        is_field_convertible(&f.ty, &convertible_enums, &known)
327                    }
328                });
329                if !ok {
330                    to_remove.push(type_name.clone());
331                }
332            }
333        }
334        for name in to_remove {
335            if convertible.remove(&name) {
336                changed = true;
337            }
338        }
339    }
340    convertible
341}
342
343/// Check if a sanitized field's type can produce a valid `Default::default()` expression.
344/// Primitive types, strings, collections, Options, and Named types with `has_default` are fine.
345/// Named types without `has_default` are not — generating `Default::default()` for them would
346/// fail to compile.
347fn sanitized_field_has_default(ty: &TypeRef, default_types: &AHashSet<&str>) -> bool {
348    match ty {
349        TypeRef::Primitive(_)
350        | TypeRef::String
351        | TypeRef::Char
352        | TypeRef::Bytes
353        | TypeRef::Path
354        | TypeRef::Unit
355        | TypeRef::Duration
356        | TypeRef::Json => true,
357        // Option<T> defaults to None regardless of T
358        TypeRef::Optional(_) => true,
359        // Vec<T> defaults to empty vec regardless of T
360        TypeRef::Vec(_) => true,
361        // Map<K, V> defaults to empty map regardless of K/V
362        TypeRef::Map(_, _) => true,
363        TypeRef::Named(name) => {
364            if is_tuple_type_name(name) {
365                // Tuple types are always passthrough
366                true
367            } else {
368                // Named type must have has_default to be safely used via Default::default()
369                default_types.contains(name.as_str())
370            }
371        }
372    }
373}
374
375/// Check if a specific type is in the convertible set.
376pub fn can_generate_conversion(typ: &TypeDef, convertible: &AHashSet<String>) -> bool {
377    convertible.contains(&typ.name)
378}
379
380pub(crate) fn is_field_convertible(
381    ty: &TypeRef,
382    convertible_enums: &AHashSet<&str>,
383    known_types: &AHashSet<&str>,
384) -> bool {
385    match ty {
386        TypeRef::Primitive(_)
387        | TypeRef::String
388        | TypeRef::Char
389        | TypeRef::Bytes
390        | TypeRef::Path
391        | TypeRef::Unit
392        | TypeRef::Duration => true,
393        TypeRef::Json => true,
394        TypeRef::Optional(inner) | TypeRef::Vec(inner) => is_field_convertible(inner, convertible_enums, known_types),
395        TypeRef::Map(k, v) => {
396            is_field_convertible(k, convertible_enums, known_types)
397                && is_field_convertible(v, convertible_enums, known_types)
398        }
399        // Tuple types are passthrough — always convertible
400        TypeRef::Named(name) if is_tuple_type_name(name) => true,
401        // Unit-variant enums and known types (including opaques, which use Arc wrap/unwrap) are convertible.
402        TypeRef::Named(name) => convertible_enums.contains(name.as_str()) || known_types.contains(name.as_str()),
403    }
404}
405
406/// Check if a field's `type_rust_path` is compatible with the known type/enum rust_paths.
407///
408/// When a struct field has a `type_rust_path` that differs from the `rust_path` of the
409/// enum or type with the same short name, the `.into()` conversion will fail because
410/// the `From` impl targets a different type. This detects such mismatches.
411fn field_has_path_mismatch(
412    field: &FieldDef,
413    enum_rust_paths: &AHashMap<&str, &str>,
414    type_rust_paths: &AHashMap<&str, &str>,
415) -> bool {
416    let name = match &field.ty {
417        TypeRef::Named(n) => n.as_str(),
418        TypeRef::Optional(inner) | TypeRef::Vec(inner) => match inner.as_ref() {
419            TypeRef::Named(n) => n.as_str(),
420            _ => return false,
421        },
422        _ => return false,
423    };
424
425    if let Some(field_path) = &field.type_rust_path {
426        if let Some(enum_path) = enum_rust_paths.get(name) {
427            if !paths_compatible(field_path, enum_path) {
428                return true;
429            }
430        }
431        if let Some(type_path) = type_rust_paths.get(name) {
432            if !paths_compatible(field_path, type_path) {
433                return true;
434            }
435        }
436    }
437    false
438}
439
440/// Check if two rust paths refer to the same type.
441///
442/// Handles re-exports: `crate::module::Type` and `crate::Type` are compatible
443/// when they share the same crate root and type name (the type is re-exported).
444fn paths_compatible(a: &str, b: &str) -> bool {
445    if a == b {
446        return true;
447    }
448    // Normalize dashes to underscores for crate name comparison
449    // (Cargo uses dashes in package names, Rust uses underscores in crate names)
450    let a_norm = a.replace('-', "_");
451    let b_norm = b.replace('-', "_");
452    if a_norm == b_norm {
453        return true;
454    }
455    // Direct suffix match (e.g., "foo::Bar" ends_with "Bar")
456    if a_norm.ends_with(&b_norm) || b_norm.ends_with(&a_norm) {
457        return true;
458    }
459    // Same crate root + same short name → likely a re-export
460    let a_root = a_norm.split("::").next().unwrap_or("");
461    let b_root = b_norm.split("::").next().unwrap_or("");
462    let a_name = a_norm.rsplit("::").next().unwrap_or("");
463    let b_name = b_norm.rsplit("::").next().unwrap_or("");
464    a_root == b_root && a_name == b_name
465}
466
467/// Build maps of name -> rust_path for enums and types in the API surface.
468fn build_rust_path_maps(surface: &ApiSurface) -> (AHashMap<&str, &str>, AHashMap<&str, &str>) {
469    let enum_paths: AHashMap<&str, &str> = surface
470        .enums
471        .iter()
472        .map(|e| (e.name.as_str(), e.rust_path.as_str()))
473        .collect();
474    let type_paths: AHashMap<&str, &str> = surface
475        .types
476        .iter()
477        .map(|t| (t.name.as_str(), t.rust_path.as_str()))
478        .collect();
479    (enum_paths, type_paths)
480}
481
482/// Check if an enum can have From/Into safely generated (both directions).
483/// All enums are allowed — data variants use Default::default() for non-simple fields
484/// in the binding→core direction.
485pub fn can_generate_enum_conversion(enum_def: &EnumDef) -> bool {
486    !enum_def.variants.is_empty()
487}
488
489/// Check if an enum can have core→binding From safely generated.
490/// This is always possible: unit variants map 1:1, data variants discard data with `..`.
491pub fn can_generate_enum_conversion_from_core(enum_def: &EnumDef) -> bool {
492    // Always possible — data variants are handled by pattern matching with `..`
493    !enum_def.variants.is_empty()
494}
495
496/// Returns true if fields represent a tuple variant (positional: _0, _1, ...).
497pub fn is_tuple_variant(fields: &[FieldDef]) -> bool {
498    !fields.is_empty()
499        && fields[0]
500            .name
501            .strip_prefix('_')
502            .is_some_and(|rest: &str| rest.chars().all(|c: char| c.is_ascii_digit()))
503}
504
505/// Returns true if a TypeDef represents a newtype struct (single unnamed field `_0`).
506pub fn is_newtype(typ: &TypeDef) -> bool {
507    typ.fields.len() == 1 && typ.fields[0].name == "_0"
508}
509
510/// Returns true if a type name looks like a tuple (starts with `(`).
511/// Tuple types are passthrough — no conversion needed.
512pub(crate) fn is_tuple_type_name(name: &str) -> bool {
513    name.starts_with('(')
514}
515
516/// Derive the Rust import path from rust_path, replacing hyphens with underscores.
517///
518/// Prefers `original_rust_path` (the path before auto_path_mappings rewriting)
519/// so that `From` impls reference the actual defining crate, avoiding orphan
520/// rule violations when `core_import` is a re-export facade.
521pub fn core_type_path(typ: &TypeDef, core_import: &str) -> String {
522    // Always use rust_path (post path-mapping) — this is the import name that
523    // binding crates can actually resolve. The original_rust_path preserves the
524    // pre-mapping crate name (e.g. "html_to_markdown") which may not be importable
525    // when the dependency is renamed (e.g. "html-to-markdown-rs" in Cargo.toml).
526    let path = typ.rust_path.replace('-', "_");
527    if path.contains("::") {
528        path
529    } else {
530        format!("{core_import}::{}", typ.name)
531    }
532}
533
534/// Check if a type has any sanitized fields (binding→core conversion is lossy).
535pub fn has_sanitized_fields(typ: &TypeDef) -> bool {
536    typ.fields.iter().any(|f| f.sanitized)
537}
538
539/// Derive the Rust import path for an enum, replacing hyphens with underscores.
540pub fn core_enum_path(enum_def: &EnumDef, core_import: &str) -> String {
541    let path = enum_def.rust_path.replace('-', "_");
542    if path.starts_with(core_import) || path.contains("::") {
543        // Path is already fully-qualified — use it verbatim.
544        path
545    } else {
546        // Bare unqualified name: prefix with the facade crate.
547        format!("{core_import}::{}", enum_def.name)
548    }
549}
550
551/// Build a map from type/enum short name to full rust_path.
552///
553/// Used by backends to resolve `TypeRef::Named(name)` to the correct qualified path
554/// instead of assuming `core_import::name` (which fails for types not re-exported at crate root).
555pub fn build_type_path_map(surface: &ApiSurface, core_import: &str) -> AHashMap<String, String> {
556    let mut map = AHashMap::new();
557    for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
558        let path = typ.rust_path.replace('-', "_");
559        let resolved = if path.starts_with(core_import) {
560            path
561        } else {
562            format!("{core_import}::{}", typ.name)
563        };
564        map.insert(typ.name.clone(), resolved);
565    }
566    for en in &surface.enums {
567        let path = en.rust_path.replace('-', "_");
568        let resolved = if path.starts_with(core_import) {
569            path
570        } else {
571            format!("{core_import}::{}", en.name)
572        };
573        map.insert(en.name.clone(), resolved);
574    }
575    map
576}
577
578/// Resolve a `TypeRef::Named` short name to its full qualified path.
579///
580/// If the name is in the path map, returns the full path; otherwise falls back
581/// to `core_import::name`.
582pub fn resolve_named_path(name: &str, core_import: &str, path_map: &AHashMap<String, String>) -> String {
583    if let Some(path) = path_map.get(name) {
584        path.clone()
585    } else {
586        format!("{core_import}::{name}")
587    }
588}
589
590/// Generate a match arm for binding -> core direction.
591/// Binding enums are always unit-variant-only. Core enums may have data variants.
592/// For data variants: `BindingEnum::Variant => CoreEnum::Variant(Default::default(), ...)`
593pub fn binding_to_core_match_arm(binding_prefix: &str, variant_name: &str, fields: &[FieldDef]) -> String {
594    binding_to_core_match_arm_ext(binding_prefix, variant_name, fields, false)
595}
596
597/// Like `binding_to_core_match_arm` but `binding_has_data` controls whether the binding
598/// enum has the variant's fields (true) or is unit-only (false, e.g. Rustler/Elixir).
599/// Generate match arm for binding->core conversion with config (handles type conversions).
600pub fn binding_to_core_match_arm_ext_cfg(
601    binding_prefix: &str,
602    variant_name: &str,
603    fields: &[FieldDef],
604    binding_has_data: bool,
605    config: &ConversionConfig,
606) -> String {
607    use super::binding_to_core::field_conversion_to_core_cfg;
608
609    if fields.is_empty() {
610        format!("{binding_prefix}::{variant_name} => Self::{variant_name},")
611    } else if !binding_has_data {
612        // Binding is unit-only: use Default for core fields
613        if is_tuple_variant(fields) {
614            let defaults: Vec<&str> = fields.iter().map(|_| "Default::default()").collect();
615            format!(
616                "{binding_prefix}::{variant_name} => Self::{variant_name}({}),",
617                defaults.join(", ")
618            )
619        } else {
620            let defaults: Vec<String> = fields
621                .iter()
622                .map(|f| format!("{}: Default::default()", f.name))
623                .collect();
624            format!(
625                "{binding_prefix}::{variant_name} => Self::{variant_name} {{ {} }},",
626                defaults.join(", ")
627            )
628        }
629    } else if is_tuple_variant(fields) {
630        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
631        let binding_pattern = field_names.join(", ");
632        let core_args: Vec<String> = fields
633            .iter()
634            .map(|f| {
635                let name = &f.name;
636                // Sanitized fields: binding uses String, use serde_json to deserialize back.
637                if f.sanitized {
638                    let expr = format!("serde_json::from_str(&{name}).unwrap_or_default()");
639                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
640                }
641                // Fields referencing excluded types: they appear as String in the binding.
642                // Use serde_json deserialization to convert back to the core type.
643                if !config.exclude_types.is_empty() && field_references_excluded_type(&f.ty, config.exclude_types) {
644                    let expr = format!("serde_json::from_str(&{name}).unwrap_or_default()");
645                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
646                }
647                // Use the conversion logic from field_conversion_to_core_cfg.
648                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
649                // so replace `val.{name}` with just `{name}` in the generated expression.
650                let conv = field_conversion_to_core_cfg(name, &f.ty, f.optional, config);
651                // Extract the RHS from "name: expr" format
652                let expr = if let Some(expr) = conv.strip_prefix(&format!("{name}: ")) {
653                    let expr = expr.replace(&format!("val.{name}"), name);
654                    expr.to_string()
655                } else {
656                    conv
657                };
658                if f.is_boxed { format!("Box::new({expr})") } else { expr }
659            })
660            .collect();
661        format!(
662            "{binding_prefix}::{variant_name} {{ {binding_pattern} }} => Self::{variant_name}({}),",
663            core_args.join(", ")
664        )
665    } else {
666        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
667        let pattern = field_names.join(", ");
668        let core_fields: Vec<String> = fields
669            .iter()
670            .map(|f| {
671                // Sanitized fields: the binding stores a simplified type (String for any complex type
672                // like Vec<(String,String)>, Vec<Named>, etc.). Use serde_json to deserialize back.
673                if f.sanitized {
674                    return format!("{}: serde_json::from_str(&{}).unwrap_or_default()", f.name, f.name);
675                }
676                // Use the conversion logic from field_conversion_to_core_cfg.
677                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
678                // so replace `val.{name}` with just `{name}` in the generated expression.
679                let conv = field_conversion_to_core_cfg(&f.name, &f.ty, f.optional, config);
680                // Extract the RHS from "name: expr" format
681                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
682                    let expr = expr.replace(&format!("val.{}", f.name), &f.name);
683                    format!("{}: {}", f.name, expr)
684                } else {
685                    conv
686                }
687            })
688            .collect();
689        format!(
690            "{binding_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
691            core_fields.join(", ")
692        )
693    }
694}
695
696pub fn binding_to_core_match_arm_ext(
697    binding_prefix: &str,
698    variant_name: &str,
699    fields: &[FieldDef],
700    binding_has_data: bool,
701) -> String {
702    if fields.is_empty() {
703        format!("{binding_prefix}::{variant_name} => Self::{variant_name},")
704    } else if !binding_has_data {
705        // Binding is unit-only: use Default for core fields
706        if is_tuple_variant(fields) {
707            let defaults: Vec<&str> = fields.iter().map(|_| "Default::default()").collect();
708            format!(
709                "{binding_prefix}::{variant_name} => Self::{variant_name}({}),",
710                defaults.join(", ")
711            )
712        } else {
713            let defaults: Vec<String> = fields
714                .iter()
715                .map(|f| format!("{}: Default::default()", f.name))
716                .collect();
717            format!(
718                "{binding_prefix}::{variant_name} => Self::{variant_name} {{ {} }},",
719                defaults.join(", ")
720            )
721        }
722    } else if is_tuple_variant(fields) {
723        // Binding uses struct syntax with _0, _1 etc., core uses tuple syntax
724        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
725        let binding_pattern = field_names.join(", ");
726        // Wrap boxed fields with Box::new() and convert Named types with .into()
727        let core_args: Vec<String> = fields
728            .iter()
729            .map(|f| {
730                let name = &f.name;
731                let expr = if matches!(&f.ty, TypeRef::Named(_)) {
732                    format!("{name}.into()")
733                } else if f.sanitized {
734                    format!("serde_json::from_str(&{name}).unwrap_or_default()")
735                } else {
736                    name.clone()
737                };
738                if f.is_boxed { format!("Box::new({expr})") } else { expr }
739            })
740            .collect();
741        format!(
742            "{binding_prefix}::{variant_name} {{ {binding_pattern} }} => Self::{variant_name}({}),",
743            core_args.join(", ")
744        )
745    } else {
746        // Destructure binding named fields and pass to core, with .into() for Named types
747        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
748        let pattern = field_names.join(", ");
749        let core_fields: Vec<String> = fields
750            .iter()
751            .map(|f| {
752                if matches!(&f.ty, TypeRef::Named(_)) {
753                    format!("{}: {}.into()", f.name, f.name)
754                } else if f.sanitized {
755                    // Sanitized fields have a simplified type in the binding (e.g. String)
756                    // but the core type is complex (e.g. Vec<(String,String)>).
757                    // Deserialize from JSON string for the binding→core conversion.
758                    format!("{}: serde_json::from_str(&{}).unwrap_or_default()", f.name, f.name)
759                } else {
760                    format!("{0}: {0}", f.name)
761                }
762            })
763            .collect();
764        format!(
765            "{binding_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
766            core_fields.join(", ")
767        )
768    }
769}
770
771/// Generate a match arm for core -> binding direction.
772/// When the binding also has data variants, destructure and forward fields.
773/// When the binding is unit-variant-only, discard core data with `..`.
774pub fn core_to_binding_match_arm(core_prefix: &str, variant_name: &str, fields: &[FieldDef]) -> String {
775    core_to_binding_match_arm_ext(core_prefix, variant_name, fields, false)
776}
777
778/// Like `core_to_binding_match_arm` but `binding_has_data` controls whether the binding
779/// enum has the variant's fields (true) or is unit-only (false).
780/// Generate match arm for core->binding conversion with config (handles type conversions).
781pub fn core_to_binding_match_arm_ext_cfg(
782    core_prefix: &str,
783    variant_name: &str,
784    fields: &[FieldDef],
785    binding_has_data: bool,
786    config: &ConversionConfig,
787) -> String {
788    use super::core_to_binding::field_conversion_from_core_cfg;
789    use ahash::AHashSet;
790
791    if fields.is_empty() {
792        format!("{core_prefix}::{variant_name} => Self::{variant_name},")
793    } else if !binding_has_data {
794        // Binding is unit-only: discard core data
795        if is_tuple_variant(fields) {
796            format!("{core_prefix}::{variant_name}(..) => Self::{variant_name},")
797        } else {
798            format!("{core_prefix}::{variant_name} {{ .. }} => Self::{variant_name},")
799        }
800    } else if is_tuple_variant(fields) {
801        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
802        let core_pattern = field_names.join(", ");
803        let binding_fields: Vec<String> = fields
804            .iter()
805            .map(|f| {
806                // Use the conversion logic from field_conversion_from_core_cfg.
807                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
808                // so replace `val.{name}` with just `{name}` in the generated expression.
809                let conv =
810                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
811                // Extract the RHS from "name: expr" format
812                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
813                    let mut expr = expr.replace(&format!("val.{}", f.name), &f.name);
814                    // Boxed fields in core tuple variants need dereferencing before conversion
815                    if f.is_boxed {
816                        expr = expr.replace(&format!("{}.into()", f.name), &format!("(*{}).into()", f.name));
817                    }
818                    format!("{}: {}", f.name, expr)
819                } else {
820                    conv
821                }
822            })
823            .collect();
824        format!(
825            "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name} {{ {} }},",
826            binding_fields.join(", ")
827        )
828    } else {
829        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
830        let pattern = field_names.join(", ");
831        let binding_fields: Vec<String> = fields
832            .iter()
833            .map(|f| {
834                // Use the conversion logic from field_conversion_from_core_cfg.
835                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
836                // so replace `val.{name}` with just `{name}` in the generated expression.
837                let conv =
838                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
839                // Extract the RHS from "name: expr" format
840                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
841                    let expr = expr.replace(&format!("val.{}", f.name), &f.name);
842                    format!("{}: {}", f.name, expr)
843                } else {
844                    conv
845                }
846            })
847            .collect();
848        format!(
849            "{core_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
850            binding_fields.join(", ")
851        )
852    }
853}
854
855pub fn core_to_binding_match_arm_ext(
856    core_prefix: &str,
857    variant_name: &str,
858    fields: &[FieldDef],
859    binding_has_data: bool,
860) -> String {
861    if fields.is_empty() {
862        format!("{core_prefix}::{variant_name} => Self::{variant_name},")
863    } else if !binding_has_data {
864        // Binding is unit-only: discard core data
865        if is_tuple_variant(fields) {
866            format!("{core_prefix}::{variant_name}(..) => Self::{variant_name},")
867        } else {
868            format!("{core_prefix}::{variant_name} {{ .. }} => Self::{variant_name},")
869        }
870    } else if is_tuple_variant(fields) {
871        // Core uses tuple syntax, binding uses struct syntax with _0, _1 etc.
872        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
873        let core_pattern = field_names.join(", ");
874        // Unbox and convert Named types with .into()
875        let binding_fields: Vec<String> = fields
876            .iter()
877            .map(|f| {
878                let name = &f.name;
879                let expr = if f.is_boxed && matches!(&f.ty, TypeRef::Named(_)) {
880                    format!("(*{name}).into()")
881                } else if f.is_boxed {
882                    format!("*{name}")
883                } else if matches!(&f.ty, TypeRef::Named(_)) {
884                    format!("{name}.into()")
885                } else if f.sanitized {
886                    format!("serde_json::to_string(&{name}).unwrap_or_default()")
887                } else {
888                    name.clone()
889                };
890                format!("{name}: {expr}")
891            })
892            .collect();
893        format!(
894            "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name} {{ {} }},",
895            binding_fields.join(", ")
896        )
897    } else {
898        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
899        let pattern = field_names.join(", ");
900        let binding_fields: Vec<String> = fields
901            .iter()
902            .map(|f| {
903                if matches!(&f.ty, TypeRef::Named(_)) {
904                    format!("{}: {}.into()", f.name, f.name)
905                } else if f.sanitized {
906                    // Sanitized fields have a simplified type in the binding (e.g. String)
907                    // but the core type is complex (e.g. Vec<(String,String)>).
908                    // Serialize to JSON string for the conversion.
909                    format!("{}: serde_json::to_string(&{}).unwrap_or_default()", f.name, f.name)
910                } else {
911                    format!("{0}: {0}", f.name)
912                }
913            })
914            .collect();
915        format!(
916            "{core_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
917            binding_fields.join(", ")
918        )
919    }
920}