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