Skip to main content

alef_codegen/conversions/
mod.rs

1mod binding_to_core;
2mod core_to_binding;
3mod enums;
4pub(crate) mod helpers;
5
6use ahash::AHashSet;
7
8/// Backend-specific configuration for From/field conversion generation.
9/// Enables shared code to handle all backend differences via parameters.
10#[derive(Default, Clone)]
11pub struct ConversionConfig<'a> {
12    /// Prefix for binding type names ("Js" for NAPI/WASM, "" for others).
13    pub type_name_prefix: &'a str,
14    /// U64/Usize/Isize need `as i64` casts (NAPI, PHP — JS/PHP lack native u64).
15    pub cast_large_ints_to_i64: bool,
16    /// Enum names mapped to String in the binding layer (PHP only).
17    /// Named fields referencing these use `format!("{:?}")` in core→binding.
18    pub enum_string_names: Option<&'a AHashSet<String>>,
19    /// Map types use JsValue in the binding layer (WASM only).
20    /// When true, Map fields use `serde_wasm_bindgen` for conversion instead of
21    /// iterator-based collect patterns (JsValue is not iterable).
22    pub map_uses_jsvalue: bool,
23    /// When true, f32 is mapped to f64 (NAPI only — JS has no f32).
24    pub cast_f32_to_f64: bool,
25    /// When true, non-optional fields on defaultable types are wrapped in Option<T>
26    /// in the binding struct and need `.unwrap_or_default()` in binding→core From.
27    /// Used by NAPI to make JS-facing structs fully optional.
28    pub optionalize_defaults: bool,
29    /// When true, Json (serde_json::Value) fields are mapped to String in the binding layer.
30    /// Core→binding uses `.to_string()`, binding→core uses `Default::default()` (lossy).
31    /// Used by PHP where serde_json::Value can't cross the extension boundary.
32    pub json_to_string: bool,
33    /// When true, add synthetic metadata field conversion for ConversionResult.
34    /// Only NAPI backend sets this (it adds metadata field to the struct).
35    pub include_cfg_metadata: bool,
36    /// When true, non-optional Duration fields on `has_default` types are stored as
37    /// `Option<u64>` in the binding struct.  The From conversion uses the builder
38    /// pattern so that `None` falls back to the core type's `Default` implementation
39    /// (giving the real default, e.g. `Duration::from_secs(30)`) instead of `Duration::ZERO`.
40    /// Used by PyO3 to prevent validation failures when `request_timeout` is unset.
41    pub option_duration_on_defaults: bool,
42}
43
44// Re-export all public items so callers continue to use `conversions::foo`.
45pub use binding_to_core::{
46    field_conversion_to_core, field_conversion_to_core_cfg, gen_from_binding_to_core, gen_from_binding_to_core_cfg,
47};
48pub use core_to_binding::{
49    field_conversion_from_core, field_conversion_from_core_cfg, gen_from_core_to_binding, gen_from_core_to_binding_cfg,
50};
51pub use enums::{
52    gen_enum_from_binding_to_core, gen_enum_from_binding_to_core_cfg, gen_enum_from_core_to_binding,
53    gen_enum_from_core_to_binding_cfg,
54};
55pub use helpers::{
56    binding_to_core_match_arm, can_generate_conversion, can_generate_enum_conversion,
57    can_generate_enum_conversion_from_core, convertible_types, core_enum_path, core_to_binding_convertible_types,
58    core_to_binding_match_arm, core_type_path, has_sanitized_fields, is_tuple_variant,
59};
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use alef_core::ir::*;
65
66    fn simple_type() -> TypeDef {
67        TypeDef {
68            name: "Config".to_string(),
69            rust_path: "my_crate::Config".to_string(),
70            fields: vec![
71                FieldDef {
72                    name: "name".into(),
73                    ty: TypeRef::String,
74                    optional: false,
75                    default: None,
76                    doc: String::new(),
77                    sanitized: false,
78                    is_boxed: false,
79                    type_rust_path: None,
80                    cfg: None,
81                    typed_default: None,
82                    core_wrapper: CoreWrapper::None,
83                    vec_inner_core_wrapper: CoreWrapper::None,
84                    newtype_wrapper: None,
85                },
86                FieldDef {
87                    name: "timeout".into(),
88                    ty: TypeRef::Primitive(PrimitiveType::U64),
89                    optional: true,
90                    default: None,
91                    doc: String::new(),
92                    sanitized: false,
93                    is_boxed: false,
94                    type_rust_path: None,
95                    cfg: None,
96                    typed_default: None,
97                    core_wrapper: CoreWrapper::None,
98                    vec_inner_core_wrapper: CoreWrapper::None,
99                    newtype_wrapper: None,
100                },
101                FieldDef {
102                    name: "backend".into(),
103                    ty: TypeRef::Named("Backend".into()),
104                    optional: true,
105                    default: None,
106                    doc: String::new(),
107                    sanitized: false,
108                    is_boxed: false,
109                    type_rust_path: None,
110                    cfg: None,
111                    typed_default: None,
112                    core_wrapper: CoreWrapper::None,
113                    vec_inner_core_wrapper: CoreWrapper::None,
114                    newtype_wrapper: None,
115                },
116            ],
117            methods: vec![],
118            is_opaque: false,
119            is_clone: true,
120            is_trait: false,
121            has_default: false,
122            has_stripped_cfg_fields: false,
123            is_return_type: false,
124            serde_rename_all: None,
125            doc: String::new(),
126            cfg: None,
127        }
128    }
129
130    fn simple_enum() -> EnumDef {
131        EnumDef {
132            name: "Backend".to_string(),
133            rust_path: "my_crate::Backend".to_string(),
134            variants: vec![
135                EnumVariant {
136                    name: "Cpu".into(),
137                    fields: vec![],
138                    doc: String::new(),
139                    is_default: false,
140                    serde_rename: None,
141                },
142                EnumVariant {
143                    name: "Gpu".into(),
144                    fields: vec![],
145                    doc: String::new(),
146                    is_default: false,
147                    serde_rename: None,
148                },
149            ],
150            doc: String::new(),
151            cfg: None,
152            serde_tag: None,
153            serde_rename_all: None,
154        }
155    }
156
157    #[test]
158    fn test_from_binding_to_core() {
159        let typ = simple_type();
160        let result = gen_from_binding_to_core(&typ, "my_crate");
161        assert!(result.contains("impl From<Config> for my_crate::Config"));
162        assert!(result.contains("name: val.name"));
163        assert!(result.contains("timeout: val.timeout"));
164        assert!(result.contains("backend: val.backend.map(Into::into)"));
165    }
166
167    #[test]
168    fn test_from_core_to_binding() {
169        let typ = simple_type();
170        let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
171        assert!(result.contains("impl From<my_crate::Config> for Config"));
172    }
173
174    #[test]
175    fn test_enum_from_binding_to_core() {
176        let enum_def = simple_enum();
177        let result = gen_enum_from_binding_to_core(&enum_def, "my_crate");
178        assert!(result.contains("impl From<Backend> for my_crate::Backend"));
179        assert!(result.contains("Backend::Cpu => Self::Cpu"));
180        assert!(result.contains("Backend::Gpu => Self::Gpu"));
181    }
182
183    #[test]
184    fn test_enum_from_core_to_binding() {
185        let enum_def = simple_enum();
186        let result = gen_enum_from_core_to_binding(&enum_def, "my_crate");
187        assert!(result.contains("impl From<my_crate::Backend> for Backend"));
188        assert!(result.contains("my_crate::Backend::Cpu => Self::Cpu"));
189        assert!(result.contains("my_crate::Backend::Gpu => Self::Gpu"));
190    }
191}