alef_codegen/conversions/
mod.rs1mod binding_to_core;
2mod core_to_binding;
3mod enums;
4pub(crate) mod helpers;
5
6use ahash::AHashSet;
7
8#[derive(Default, Clone)]
11pub struct ConversionConfig<'a> {
12 pub type_name_prefix: &'a str,
14 pub cast_large_ints_to_i64: bool,
16 pub enum_string_names: Option<&'a AHashSet<String>>,
19 pub map_uses_jsvalue: bool,
23 pub cast_f32_to_f64: bool,
25 pub optionalize_defaults: bool,
29 pub json_to_string: bool,
33 pub include_cfg_metadata: bool,
36 pub option_duration_on_defaults: bool,
42 pub binding_enums_have_data: bool,
45}
46
47pub use binding_to_core::{
49 field_conversion_to_core, field_conversion_to_core_cfg, gen_from_binding_to_core, gen_from_binding_to_core_cfg,
50};
51pub use core_to_binding::{
52 field_conversion_from_core, field_conversion_from_core_cfg, gen_from_core_to_binding, gen_from_core_to_binding_cfg,
53};
54pub use enums::{
55 gen_enum_from_binding_to_core, gen_enum_from_binding_to_core_cfg, gen_enum_from_core_to_binding,
56 gen_enum_from_core_to_binding_cfg,
57};
58pub use helpers::{
59 binding_to_core_match_arm, can_generate_conversion, can_generate_enum_conversion,
60 can_generate_enum_conversion_from_core, convertible_types, core_enum_path, core_to_binding_convertible_types,
61 core_to_binding_match_arm, core_type_path, has_sanitized_fields, is_tuple_variant,
62};
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67 use alef_core::ir::*;
68
69 fn simple_type() -> TypeDef {
70 TypeDef {
71 name: "Config".to_string(),
72 rust_path: "my_crate::Config".to_string(),
73 fields: vec![
74 FieldDef {
75 name: "name".into(),
76 ty: TypeRef::String,
77 optional: false,
78 default: None,
79 doc: String::new(),
80 sanitized: false,
81 is_boxed: false,
82 type_rust_path: None,
83 cfg: None,
84 typed_default: None,
85 core_wrapper: CoreWrapper::None,
86 vec_inner_core_wrapper: CoreWrapper::None,
87 newtype_wrapper: None,
88 },
89 FieldDef {
90 name: "timeout".into(),
91 ty: TypeRef::Primitive(PrimitiveType::U64),
92 optional: true,
93 default: None,
94 doc: String::new(),
95 sanitized: false,
96 is_boxed: false,
97 type_rust_path: None,
98 cfg: None,
99 typed_default: None,
100 core_wrapper: CoreWrapper::None,
101 vec_inner_core_wrapper: CoreWrapper::None,
102 newtype_wrapper: None,
103 },
104 FieldDef {
105 name: "backend".into(),
106 ty: TypeRef::Named("Backend".into()),
107 optional: true,
108 default: None,
109 doc: String::new(),
110 sanitized: false,
111 is_boxed: false,
112 type_rust_path: None,
113 cfg: None,
114 typed_default: None,
115 core_wrapper: CoreWrapper::None,
116 vec_inner_core_wrapper: CoreWrapper::None,
117 newtype_wrapper: None,
118 },
119 ],
120 methods: vec![],
121 is_opaque: false,
122 is_clone: true,
123 is_trait: false,
124 has_default: false,
125 has_stripped_cfg_fields: false,
126 is_return_type: false,
127 serde_rename_all: None,
128 doc: String::new(),
129 cfg: None,
130 }
131 }
132
133 fn simple_enum() -> EnumDef {
134 EnumDef {
135 name: "Backend".to_string(),
136 rust_path: "my_crate::Backend".to_string(),
137 variants: vec![
138 EnumVariant {
139 name: "Cpu".into(),
140 fields: vec![],
141 doc: String::new(),
142 is_default: false,
143 serde_rename: None,
144 },
145 EnumVariant {
146 name: "Gpu".into(),
147 fields: vec![],
148 doc: String::new(),
149 is_default: false,
150 serde_rename: None,
151 },
152 ],
153 doc: String::new(),
154 cfg: None,
155 serde_tag: None,
156 serde_rename_all: None,
157 }
158 }
159
160 #[test]
161 fn test_from_binding_to_core() {
162 let typ = simple_type();
163 let result = gen_from_binding_to_core(&typ, "my_crate");
164 assert!(result.contains("impl From<Config> for my_crate::Config"));
165 assert!(result.contains("name: val.name"));
166 assert!(result.contains("timeout: val.timeout"));
167 assert!(result.contains("backend: val.backend.map(Into::into)"));
168 }
169
170 #[test]
171 fn test_from_core_to_binding() {
172 let typ = simple_type();
173 let result = gen_from_core_to_binding(&typ, "my_crate", &AHashSet::new());
174 assert!(result.contains("impl From<my_crate::Config> for Config"));
175 }
176
177 #[test]
178 fn test_enum_from_binding_to_core() {
179 let enum_def = simple_enum();
180 let result = gen_enum_from_binding_to_core(&enum_def, "my_crate");
181 assert!(result.contains("impl From<Backend> for my_crate::Backend"));
182 assert!(result.contains("Backend::Cpu => Self::Cpu"));
183 assert!(result.contains("Backend::Gpu => Self::Gpu"));
184 }
185
186 #[test]
187 fn test_enum_from_core_to_binding() {
188 let enum_def = simple_enum();
189 let result = gen_enum_from_core_to_binding(&enum_def, "my_crate");
190 assert!(result.contains("impl From<my_crate::Backend> for Backend"));
191 assert!(result.contains("my_crate::Backend::Cpu => Self::Cpu"));
192 assert!(result.contains("my_crate::Backend::Gpu => Self::Gpu"));
193 }
194}