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    core_type_path_remapped(typ, core_import, &[])
523}
524
525/// Like [`core_type_path`] but rewrites the leading crate segment when it matches
526/// a known source→override mapping.
527///
528/// When `core_crate_override` is set for a language, IR `rust_path` values still
529/// contain the original source crate prefix (e.g. `spikard_core::Method`). The
530/// `remaps` slice contains `(original_crate, override_crate)` pairs; when the
531/// leading crate segment of `rust_path` matches `original_crate`, it is replaced
532/// with `override_crate`.
533pub fn core_type_path_remapped(typ: &TypeDef, core_import: &str, remaps: &[(&str, &str)]) -> String {
534    // Always use rust_path (post path-mapping) — this is the import name that
535    // binding crates can actually resolve. The original_rust_path preserves the
536    // pre-mapping crate name (e.g. "html_to_markdown") which may not be importable
537    // when the dependency is renamed (e.g. "html-to-markdown-rs" in Cargo.toml).
538    let path = typ.rust_path.replace('-', "_");
539    if path.contains("::") {
540        apply_crate_remaps(&path, remaps)
541    } else {
542        format!("{core_import}::{}", typ.name)
543    }
544}
545
546/// Apply source→override crate remaps to a fully-qualified Rust path.
547///
548/// If the leading crate segment of `path` (the part before the first `::`)
549/// matches any entry in `remaps`, that segment is replaced with the override.
550/// Returns `path` unchanged when no remap applies.
551pub fn apply_crate_remaps(path: &str, remaps: &[(&str, &str)]) -> String {
552    if remaps.is_empty() {
553        return path.to_string();
554    }
555    if let Some(sep) = path.find("::") {
556        let leading = &path[..sep];
557        if let Some(&(_, override_crate)) = remaps.iter().find(|(orig, _)| *orig == leading) {
558            return format!("{override_crate}{}", &path[sep..]);
559        }
560    }
561    path.to_string()
562}
563
564/// Check if a type has any sanitized fields (binding→core conversion is lossy).
565pub fn has_sanitized_fields(typ: &TypeDef) -> bool {
566    typ.fields.iter().any(|f| f.sanitized)
567}
568
569/// Derive the Rust import path for an enum, replacing hyphens with underscores.
570pub fn core_enum_path(enum_def: &EnumDef, core_import: &str) -> String {
571    core_enum_path_remapped(enum_def, core_import, &[])
572}
573
574/// Like [`core_enum_path`] but rewrites the leading crate segment when it matches
575/// a known source→override mapping. See [`core_type_path_remapped`] for details.
576pub fn core_enum_path_remapped(enum_def: &EnumDef, core_import: &str, remaps: &[(&str, &str)]) -> String {
577    let path = enum_def.rust_path.replace('-', "_");
578    if path.starts_with(core_import) || path.contains("::") {
579        // Path is already fully-qualified — apply remaps and return.
580        apply_crate_remaps(&path, remaps)
581    } else {
582        // Bare unqualified name: prefix with the facade crate.
583        format!("{core_import}::{}", enum_def.name)
584    }
585}
586
587/// Build a map from type/enum short name to full rust_path.
588///
589/// Used by backends to resolve `TypeRef::Named(name)` to the correct qualified path
590/// instead of assuming `core_import::name` (which fails for types not re-exported at crate root).
591pub fn build_type_path_map(surface: &ApiSurface, core_import: &str) -> AHashMap<String, String> {
592    let mut map = AHashMap::new();
593    for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
594        let path = typ.rust_path.replace('-', "_");
595        let resolved = if path.starts_with(core_import) {
596            path
597        } else {
598            format!("{core_import}::{}", typ.name)
599        };
600        map.insert(typ.name.clone(), resolved);
601    }
602    for en in &surface.enums {
603        let path = en.rust_path.replace('-', "_");
604        let resolved = if path.starts_with(core_import) {
605            path
606        } else {
607            format!("{core_import}::{}", en.name)
608        };
609        map.insert(en.name.clone(), resolved);
610    }
611    map
612}
613
614/// Resolve a `TypeRef::Named` short name to its full qualified path.
615///
616/// If the name is in the path map, returns the full path; otherwise falls back
617/// to `core_import::name`.
618pub fn resolve_named_path(name: &str, core_import: &str, path_map: &AHashMap<String, String>) -> String {
619    if let Some(path) = path_map.get(name) {
620        path.clone()
621    } else {
622        format!("{core_import}::{name}")
623    }
624}
625
626/// Generate a match arm for binding -> core direction.
627/// Binding enums are always unit-variant-only. Core enums may have data variants.
628/// For data variants: `BindingEnum::Variant => CoreEnum::Variant(Default::default(), ...)`
629pub fn binding_to_core_match_arm(binding_prefix: &str, variant_name: &str, fields: &[FieldDef]) -> String {
630    binding_to_core_match_arm_ext(binding_prefix, variant_name, fields, false)
631}
632
633/// Like `binding_to_core_match_arm` but `binding_has_data` controls whether the binding
634/// enum has the variant's fields (true) or is unit-only (false, e.g. Rustler/Elixir).
635/// Generate match arm for binding->core conversion with config (handles type conversions).
636pub fn binding_to_core_match_arm_ext_cfg(
637    binding_prefix: &str,
638    variant_name: &str,
639    fields: &[FieldDef],
640    binding_has_data: bool,
641    config: &ConversionConfig,
642) -> String {
643    use super::binding_to_core::field_conversion_to_core_cfg;
644
645    if fields.is_empty() {
646        format!("{binding_prefix}::{variant_name} => Self::{variant_name},")
647    } else if !binding_has_data {
648        // Binding is unit-only: use Default for core fields
649        if is_tuple_variant(fields) {
650            let defaults: Vec<&str> = fields.iter().map(|_| "Default::default()").collect();
651            format!(
652                "{binding_prefix}::{variant_name} => Self::{variant_name}({}),",
653                defaults.join(", ")
654            )
655        } else {
656            let defaults: Vec<String> = fields
657                .iter()
658                .map(|f| format!("{}: Default::default()", f.name))
659                .collect();
660            format!(
661                "{binding_prefix}::{variant_name} => Self::{variant_name} {{ {} }},",
662                defaults.join(", ")
663            )
664        }
665    } else if is_tuple_variant(fields) {
666        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
667        let binding_pattern = field_names.join(", ");
668        let core_args: Vec<String> = fields
669            .iter()
670            .map(|f| {
671                let name = &f.name;
672                // Sanitized fields: binding uses String, use serde_json to deserialize back.
673                if f.sanitized {
674                    let expr = if let TypeRef::Vec(_) = &f.ty {
675                        // Vec<String> sanitized: each element is a debug-formatted value,
676                        // parse each one individually.
677                        format!("{name}.iter().filter_map(|s| serde_json::from_str(s).ok()).collect()")
678                    } else {
679                        format!("serde_json::from_str(&{name}).unwrap_or_default()")
680                    };
681                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
682                }
683                // Fields referencing excluded types: they appear as String in the binding.
684                // Use serde_json deserialization to convert back to the core type.
685                if !config.exclude_types.is_empty() && field_references_excluded_type(&f.ty, config.exclude_types) {
686                    let expr = format!("serde_json::from_str(&{name}).unwrap_or_default()");
687                    return if f.is_boxed { format!("Box::new({expr})") } else { expr };
688                }
689                // Use the conversion logic from field_conversion_to_core_cfg.
690                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
691                // so replace `val.{name}` with just `{name}` in the generated expression.
692                let conv = field_conversion_to_core_cfg(name, &f.ty, f.optional, config);
693                // Extract the RHS from "name: expr" format
694                let expr = if let Some(expr) = conv.strip_prefix(&format!("{name}: ")) {
695                    let expr = expr.replace(&format!("val.{name}"), name);
696                    expr.to_string()
697                } else {
698                    conv
699                };
700                if f.is_boxed { format!("Box::new({expr})") } else { expr }
701            })
702            .collect();
703        format!(
704            "{binding_prefix}::{variant_name} {{ {binding_pattern} }} => Self::{variant_name}({}),",
705            core_args.join(", ")
706        )
707    } else {
708        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
709        let pattern = field_names.join(", ");
710        let core_fields: Vec<String> = fields
711            .iter()
712            .map(|f| {
713                // Sanitized fields: the binding stores a simplified type (String for any complex type
714                // like Vec<(String,String)>, Vec<Named>, etc.). Use serde_json to deserialize back.
715                if f.sanitized {
716                    // For Vec<String> sanitized (representing Vec<Tuple> like Vec<(String, String)>),
717                    // each element is a debug-formatted string. Cannot safely use serde_json::from_str
718                    // on Vec<String>. Default to empty collection.
719                    if let TypeRef::Vec(_) = &f.ty {
720                        return format!(
721                            "{}: {}.iter().filter_map(|s| serde_json::from_str(s).ok()).collect()",
722                            f.name, f.name
723                        );
724                    }
725                    return format!("{}: serde_json::from_str(&{}).unwrap_or_default()", f.name, f.name);
726                }
727                // Use the conversion logic from field_conversion_to_core_cfg.
728                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
729                // so replace `val.{name}` with just `{name}` in the generated expression.
730                let conv = field_conversion_to_core_cfg(&f.name, &f.ty, f.optional, config);
731                // Extract the RHS from "name: expr" format
732                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
733                    let expr = expr.replace(&format!("val.{}", f.name), &f.name);
734                    format!("{}: {}", f.name, expr)
735                } else {
736                    conv
737                }
738            })
739            .collect();
740        format!(
741            "{binding_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
742            core_fields.join(", ")
743        )
744    }
745}
746
747pub fn binding_to_core_match_arm_ext(
748    binding_prefix: &str,
749    variant_name: &str,
750    fields: &[FieldDef],
751    binding_has_data: bool,
752) -> String {
753    if fields.is_empty() {
754        format!("{binding_prefix}::{variant_name} => Self::{variant_name},")
755    } else if !binding_has_data {
756        // Binding is unit-only: use Default for core fields
757        if is_tuple_variant(fields) {
758            let defaults: Vec<&str> = fields.iter().map(|_| "Default::default()").collect();
759            format!(
760                "{binding_prefix}::{variant_name} => Self::{variant_name}({}),",
761                defaults.join(", ")
762            )
763        } else {
764            let defaults: Vec<String> = fields
765                .iter()
766                .map(|f| format!("{}: Default::default()", f.name))
767                .collect();
768            format!(
769                "{binding_prefix}::{variant_name} => Self::{variant_name} {{ {} }},",
770                defaults.join(", ")
771            )
772        }
773    } else if is_tuple_variant(fields) {
774        // Binding uses struct syntax with _0, _1 etc., core uses tuple syntax
775        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
776        let binding_pattern = field_names.join(", ");
777        // Wrap boxed fields with Box::new() and convert Named types with .into()
778        let core_args: Vec<String> = fields
779            .iter()
780            .map(|f| {
781                let name = &f.name;
782                let expr = if matches!(&f.ty, TypeRef::Named(_)) {
783                    format!("{name}.into()")
784                } else if f.sanitized {
785                    format!("serde_json::from_str(&{name}).unwrap_or_default()")
786                } else {
787                    name.clone()
788                };
789                if f.is_boxed { format!("Box::new({expr})") } else { expr }
790            })
791            .collect();
792        format!(
793            "{binding_prefix}::{variant_name} {{ {binding_pattern} }} => Self::{variant_name}({}),",
794            core_args.join(", ")
795        )
796    } else {
797        // Destructure binding named fields and pass to core, with .into() for Named types
798        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
799        let pattern = field_names.join(", ");
800        let core_fields: Vec<String> = fields
801            .iter()
802            .map(|f| {
803                if matches!(&f.ty, TypeRef::Named(_)) {
804                    format!("{}: {}.into()", f.name, f.name)
805                } else if f.sanitized {
806                    // Sanitized fields have a simplified type in the binding (e.g. String)
807                    // but the core type is complex (e.g. Vec<(String,String)>).
808                    // Deserialize from JSON string for the binding→core conversion.
809                    format!("{}: serde_json::from_str(&{}).unwrap_or_default()", f.name, f.name)
810                } else {
811                    format!("{0}: {0}", f.name)
812                }
813            })
814            .collect();
815        format!(
816            "{binding_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
817            core_fields.join(", ")
818        )
819    }
820}
821
822/// Generate a match arm for core -> binding direction.
823/// When the binding also has data variants, destructure and forward fields.
824/// When the binding is unit-variant-only, discard core data with `..`.
825pub fn core_to_binding_match_arm(core_prefix: &str, variant_name: &str, fields: &[FieldDef]) -> String {
826    core_to_binding_match_arm_ext(core_prefix, variant_name, fields, false)
827}
828
829/// Like `core_to_binding_match_arm` but `binding_has_data` controls whether the binding
830/// enum has the variant's fields (true) or is unit-only (false).
831/// Generate match arm for core->binding conversion with config (handles type conversions).
832pub fn core_to_binding_match_arm_ext_cfg(
833    core_prefix: &str,
834    variant_name: &str,
835    fields: &[FieldDef],
836    binding_has_data: bool,
837    config: &ConversionConfig,
838) -> String {
839    use super::core_to_binding::field_conversion_from_core_cfg;
840    use ahash::AHashSet;
841
842    if fields.is_empty() {
843        format!("{core_prefix}::{variant_name} => Self::{variant_name},")
844    } else if !binding_has_data {
845        // Binding is unit-only: discard core data
846        if is_tuple_variant(fields) {
847            format!("{core_prefix}::{variant_name}(..) => Self::{variant_name},")
848        } else {
849            format!("{core_prefix}::{variant_name} {{ .. }} => Self::{variant_name},")
850        }
851    } else if is_tuple_variant(fields) {
852        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
853        let core_pattern = field_names.join(", ");
854        let binding_fields: Vec<String> = fields
855            .iter()
856            .map(|f| {
857                // Use the conversion logic from field_conversion_from_core_cfg.
858                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
859                // so replace `val.{name}` with just `{name}` in the generated expression.
860                let conv =
861                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
862                // Extract the RHS from "name: expr" format
863                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
864                    let mut expr = expr.replace(&format!("val.{}", f.name), &f.name);
865                    // Boxed fields in core tuple variants need dereferencing before conversion
866                    if f.is_boxed {
867                        expr = expr.replace(&format!("{}.into()", f.name), &format!("(*{}).into()", f.name));
868                    }
869                    format!("{}: {}", f.name, expr)
870                } else {
871                    conv
872                }
873            })
874            .collect();
875        format!(
876            "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name} {{ {} }},",
877            binding_fields.join(", ")
878        )
879    } else {
880        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
881        let pattern = field_names.join(", ");
882        let binding_fields: Vec<String> = fields
883            .iter()
884            .map(|f| {
885                // Use the conversion logic from field_conversion_from_core_cfg.
886                // In an enum match arm, fields are bound by destructuring (not via `val.field`),
887                // so replace `val.{name}` with just `{name}` in the generated expression.
888                let conv =
889                    field_conversion_from_core_cfg(&f.name, &f.ty, f.optional, f.sanitized, &AHashSet::new(), config);
890                // Extract the RHS from "name: expr" format
891                if let Some(expr) = conv.strip_prefix(&format!("{}: ", f.name)) {
892                    let expr = expr.replace(&format!("val.{}", f.name), &f.name);
893                    format!("{}: {}", f.name, expr)
894                } else {
895                    conv
896                }
897            })
898            .collect();
899        format!(
900            "{core_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
901            binding_fields.join(", ")
902        )
903    }
904}
905
906pub fn core_to_binding_match_arm_ext(
907    core_prefix: &str,
908    variant_name: &str,
909    fields: &[FieldDef],
910    binding_has_data: bool,
911) -> String {
912    if fields.is_empty() {
913        format!("{core_prefix}::{variant_name} => Self::{variant_name},")
914    } else if !binding_has_data {
915        // Binding is unit-only: discard core data
916        if is_tuple_variant(fields) {
917            format!("{core_prefix}::{variant_name}(..) => Self::{variant_name},")
918        } else {
919            format!("{core_prefix}::{variant_name} {{ .. }} => Self::{variant_name},")
920        }
921    } else if is_tuple_variant(fields) {
922        // Core uses tuple syntax, binding uses struct syntax with _0, _1 etc.
923        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
924        let core_pattern = field_names.join(", ");
925        // Unbox and convert Named types with .into()
926        let binding_fields: Vec<String> = fields
927            .iter()
928            .map(|f| {
929                let name = &f.name;
930                let expr = if f.is_boxed && matches!(&f.ty, TypeRef::Named(_)) {
931                    format!("(*{name}).into()")
932                } else if f.is_boxed {
933                    format!("*{name}")
934                } else if matches!(&f.ty, TypeRef::Named(_)) {
935                    format!("{name}.into()")
936                } else if f.sanitized {
937                    format!("serde_json::to_string(&{name}).unwrap_or_default()")
938                } else {
939                    name.clone()
940                };
941                format!("{name}: {expr}")
942            })
943            .collect();
944        format!(
945            "{core_prefix}::{variant_name}({core_pattern}) => Self::{variant_name} {{ {} }},",
946            binding_fields.join(", ")
947        )
948    } else {
949        let field_names: Vec<&str> = fields.iter().map(|f| f.name.as_str()).collect();
950        let pattern = field_names.join(", ");
951        let binding_fields: Vec<String> = fields
952            .iter()
953            .map(|f| {
954                if matches!(&f.ty, TypeRef::Named(_)) {
955                    format!("{}: {}.into()", f.name, f.name)
956                } else if f.sanitized {
957                    // Sanitized fields have a simplified type in the binding (e.g. String)
958                    // but the core type is complex (e.g. Vec<(String,String)>).
959                    // Serialize to JSON string for the conversion.
960                    format!("{}: serde_json::to_string(&{}).unwrap_or_default()", f.name, f.name)
961                } else {
962                    format!("{0}: {0}", f.name)
963                }
964            })
965            .collect();
966        format!(
967            "{core_prefix}::{variant_name} {{ {pattern} }} => Self::{variant_name} {{ {} }},",
968            binding_fields.join(", ")
969        )
970    }
971}