1use crate::generators::binding_helpers::{
2 gen_async_body, gen_call_args, gen_call_args_with_let_bindings, gen_named_let_bindings, gen_serde_let_bindings,
3 gen_unimplemented_body, has_named_params,
4};
5use crate::generators::{AdapterBodies, AsyncPattern, RustBindingConfig};
6use crate::shared::{function_params, function_sig_defaults};
7use crate::type_mapper::TypeMapper;
8use ahash::{AHashMap, AHashSet};
9use alef_core::ir::{ApiSurface, FunctionDef, TypeRef};
10use std::fmt::Write;
11
12pub fn gen_function(
14 func: &FunctionDef,
15 mapper: &dyn TypeMapper,
16 cfg: &RustBindingConfig,
17 adapter_bodies: &AdapterBodies,
18 opaque_types: &AHashSet<String>,
19) -> String {
20 let map_fn = |ty: &alef_core::ir::TypeRef| mapper.map_type(ty);
21 let params = function_params(&func.params, &map_fn);
22 let return_type = mapper.map_type(&func.return_type);
23 let ret = mapper.wrap_return(&return_type, func.error_type.is_some());
24
25 let use_let_bindings = has_named_params(&func.params, opaque_types);
27 let call_args = if use_let_bindings {
28 gen_call_args_with_let_bindings(&func.params, opaque_types)
29 } else {
30 gen_call_args(&func.params, opaque_types)
31 };
32 let core_import = cfg.core_import;
33 let let_bindings = if use_let_bindings {
34 gen_named_let_bindings(&func.params, opaque_types, core_import)
35 } else {
36 String::new()
37 };
38
39 let core_fn_path = {
41 let path = func.rust_path.replace('-', "_");
42 if path.starts_with(core_import) {
43 path
44 } else {
45 format!("{core_import}::{}", func.name)
46 }
47 };
48
49 let can_delegate = crate::shared::can_auto_delegate_function(func, opaque_types);
50
51 let serde_err_conv = match cfg.async_pattern {
53 AsyncPattern::Pyo3FutureIntoPy => ".map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))",
54 AsyncPattern::NapiNativeAsync => ".map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))",
55 AsyncPattern::WasmNativeAsync => ".map_err(|e| JsValue::from_str(&e.to_string()))",
56 _ => ".map_err(|e| e.to_string())",
57 };
58
59 let body = if !can_delegate {
61 if let Some(adapter_body) = adapter_bodies.get(&func.name) {
63 adapter_body.clone()
64 } else if cfg.has_serde && use_let_bindings && func.error_type.is_some() {
65 let is_async_pyo3 = func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy;
70 let (serde_indent, serde_err_async) = if is_async_pyo3 {
71 (
72 " ",
73 ".map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))",
74 )
75 } else {
76 (" ", serde_err_conv)
77 };
78 let serde_bindings =
79 gen_serde_let_bindings(&func.params, opaque_types, core_import, serde_err_async, serde_indent);
80 let core_call = format!("{core_fn_path}({call_args})");
81
82 let returns_ref = func.returns_ref;
84 let wrap_return = |expr: &str| -> String {
85 match &func.return_type {
86 TypeRef::Vec(inner) => {
87 match inner.as_ref() {
89 TypeRef::Named(_) => {
90 format!("{expr}.into_iter().map(Into::into).collect()")
92 }
93 _ => expr.to_string(),
94 }
95 }
96 TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
97 if returns_ref {
98 format!("{name} {{ inner: Arc::new({expr}.clone()) }}")
99 } else {
100 format!("{name} {{ inner: Arc::new({expr}) }}")
101 }
102 }
103 TypeRef::Named(_) => {
104 if returns_ref {
106 format!("{return_type}::from({expr}.clone())")
107 } else {
108 format!("{return_type}::from({expr})")
109 }
110 }
111 TypeRef::String | TypeRef::Bytes => format!("{expr}.into()"),
112 TypeRef::Path => format!("{expr}.to_string_lossy().to_string()"),
113 TypeRef::Json => format!("{expr}.to_string()"),
114 _ => expr.to_string(),
115 }
116 };
117
118 if is_async_pyo3 {
119 let is_unit = matches!(func.return_type, TypeRef::Unit);
121 let wrapped = wrap_return("result");
122 let core_await = format!(
123 "{core_call}.await\n .map_err(|e| PyErr::new::<PyRuntimeError, _>(e.to_string()))?"
124 );
125 let inner_body = if is_unit {
126 format!("{serde_bindings}{core_await};\n Ok(())")
127 } else {
128 if wrapped.contains(".into()") || wrapped.contains("::from(") {
132 format!(
134 "{serde_bindings}let result = {core_await};\n let wrapped_result: {return_type} = {wrapped};\n Ok(wrapped_result)"
135 )
136 } else {
137 format!("{serde_bindings}let result = {core_await};\n Ok({wrapped})")
138 }
139 };
140 format!("pyo3_async_runtimes::tokio::future_into_py(py, async move {{\n{inner_body}\n }})")
141 } else if func.is_async {
142 let is_unit = matches!(func.return_type, TypeRef::Unit);
144 let wrapped = wrap_return("result");
145 let async_body = gen_async_body(
146 &core_call,
147 cfg,
148 func.error_type.is_some(),
149 &wrapped,
150 false,
151 "",
152 is_unit,
153 Some(&return_type),
154 );
155 format!("{serde_bindings}{async_body}")
156 } else if matches!(func.return_type, TypeRef::Unit) {
157 let await_kw = if func.is_async { ".await" } else { "" };
159 let debug_marker = if func.is_async { "/*ASYNC_UNIT*/ " } else { "" };
160 format!("{serde_bindings}{debug_marker}{core_call}{await_kw}{serde_err_conv}?;\n Ok(())")
161 } else {
162 let wrapped = wrap_return("val");
163 let await_kw = if func.is_async { ".await" } else { "" };
164 if wrapped == "val" {
165 format!("{serde_bindings}{core_call}{await_kw}{serde_err_conv}")
166 } else {
167 format!("{serde_bindings}{core_call}{await_kw}.map(|val| {wrapped}){serde_err_conv}")
168 }
169 }
170 } else if func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy {
171 let suppress = if func.params.is_empty() {
173 String::new()
174 } else {
175 let names: Vec<&str> = func.params.iter().map(|p| p.name.as_str()).collect();
176 format!("let _ = ({});\n ", names.join(", "))
177 };
178 format!(
179 "{suppress}Err(pyo3::exceptions::PyNotImplementedError::new_err(\"not implemented: {}\"))",
180 func.name
181 )
182 } else {
183 gen_unimplemented_body(
185 &func.return_type,
186 &func.name,
187 func.error_type.is_some(),
188 cfg,
189 &func.params,
190 opaque_types,
191 )
192 }
193 } else if func.is_async {
194 let core_call = format!("{core_fn_path}({call_args})");
196 let return_wrap = match &func.return_type {
199 TypeRef::Named(n) if opaque_types.contains(n.as_str()) => {
200 format!("{n} {{ inner: Arc::new(result) }}")
201 }
202 TypeRef::Named(_) => {
203 format!("{return_type}::from(result)")
204 }
205 TypeRef::Vec(inner) => match inner.as_ref() {
206 TypeRef::Named(n) if opaque_types.contains(n.as_str()) => {
207 format!("result.into_iter().map(|v| {n} {{ inner: Arc::new(v) }}).collect::<Vec<_>>()")
208 }
209 TypeRef::Named(_) => {
210 let inner_mapped = mapper.map_type(inner);
211 format!("result.into_iter().map({inner_mapped}::from).collect::<Vec<_>>()")
212 }
213 _ => "result".to_string(),
214 },
215 TypeRef::Unit => "result".to_string(),
216 _ => super::binding_helpers::wrap_return(
217 "result",
218 &func.return_type,
219 "",
220 opaque_types,
221 false,
222 func.returns_ref,
223 false,
224 ),
225 };
226 let async_body = gen_async_body(
227 &core_call,
228 cfg,
229 func.error_type.is_some(),
230 &return_wrap,
231 false,
232 "",
233 matches!(func.return_type, TypeRef::Unit),
234 Some(&return_type),
235 );
236 format!("{let_bindings}{async_body}")
237 } else {
238 let core_call = format!("{core_fn_path}({call_args})");
239
240 let returns_ref = func.returns_ref;
242 let wrap_return = |expr: &str| -> String {
243 match &func.return_type {
244 TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
246 if returns_ref {
247 format!("{name} {{ inner: Arc::new({expr}.clone()) }}")
248 } else {
249 format!("{name} {{ inner: Arc::new({expr}) }}")
250 }
251 }
252 TypeRef::Named(_name) => {
254 if returns_ref {
255 format!("{expr}.clone().into()")
256 } else {
257 format!("{expr}.into()")
258 }
259 }
260 TypeRef::String | TypeRef::Bytes => format!("{expr}.into()"),
262 TypeRef::Path => format!("{expr}.to_string_lossy().to_string()"),
264 TypeRef::Json => format!("{expr}.to_string()"),
266 TypeRef::Optional(inner) => match inner.as_ref() {
268 TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
269 if returns_ref {
270 format!("{expr}.map(|v| {name} {{ inner: Arc::new(v.clone()) }})")
271 } else {
272 format!("{expr}.map(|v| {name} {{ inner: Arc::new(v) }})")
273 }
274 }
275 TypeRef::Named(_) => {
276 if returns_ref {
277 format!("{expr}.map(|v| v.clone().into())")
278 } else {
279 format!("{expr}.map(Into::into)")
280 }
281 }
282 TypeRef::String | TypeRef::Bytes | TypeRef::Path => {
283 format!("{expr}.map(Into::into)")
284 }
285 TypeRef::Vec(vi) => match vi.as_ref() {
286 TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
287 format!("{expr}.map(|v| v.into_iter().map(|x| {name} {{ inner: Arc::new(x) }}).collect())")
288 }
289 TypeRef::Named(_) => {
290 format!("{expr}.map(|v| v.into_iter().map(Into::into).collect())")
291 }
292 _ => expr.to_string(),
293 },
294 _ => expr.to_string(),
295 },
296 TypeRef::Vec(inner) => match inner.as_ref() {
298 TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
299 if returns_ref {
300 format!("{expr}.into_iter().map(|v| {name} {{ inner: Arc::new(v.clone()) }}).collect()")
301 } else {
302 format!("{expr}.into_iter().map(|v| {name} {{ inner: Arc::new(v) }}).collect()")
303 }
304 }
305 TypeRef::Named(_) => {
306 if returns_ref {
307 format!("{expr}.into_iter().map(|v| v.clone().into()).collect()")
308 } else {
309 format!("{expr}.into_iter().map(Into::into).collect()")
310 }
311 }
312 TypeRef::String | TypeRef::Bytes | TypeRef::Path => {
313 format!("{expr}.into_iter().map(Into::into).collect()")
314 }
315 _ => expr.to_string(),
316 },
317 _ => expr.to_string(),
318 }
319 };
320
321 if func.error_type.is_some() {
322 let err_conv = match cfg.async_pattern {
324 AsyncPattern::Pyo3FutureIntoPy => {
325 ".map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))"
326 }
327 AsyncPattern::NapiNativeAsync => {
328 ".map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))"
329 }
330 AsyncPattern::WasmNativeAsync => ".map_err(|e| JsValue::from_str(&e.to_string()))",
331 _ => ".map_err(|e| e.to_string())",
332 };
333 let wrapped = wrap_return("val");
334 if wrapped == "val" {
335 format!("{core_call}{err_conv}")
336 } else {
337 format!("{core_call}.map(|val| {wrapped}){err_conv}")
338 }
339 } else {
340 wrap_return(&core_call)
341 }
342 };
343
344 let body = if !let_bindings.is_empty() && !func.is_async {
348 if can_delegate {
349 format!("{let_bindings}{body}")
350 } else {
351 let vec_str_bindings: String = func.params.iter().filter(|p| {
354 p.is_ref && matches!(&p.ty, TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::String | TypeRef::Char))
355 }).map(|p| {
356 if p.optional {
359 format!("let {}_refs: Vec<&str> = {}.as_ref().map(|v| v.iter().map(|s| s.as_str()).collect()).unwrap_or_default();\n ", p.name, p.name)
360 } else {
361 format!("let {}_refs: Vec<&str> = {}.iter().map(|s| s.as_str()).collect();\n ", p.name, p.name)
362 }
363 }).collect();
364 if !vec_str_bindings.is_empty() {
365 format!("{vec_str_bindings}{body}")
366 } else {
367 body
368 }
369 }
370 } else {
371 body
372 };
373
374 let async_kw = if func.is_async { "async " } else { "" };
376 let func_needs_py = func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy;
377
378 let ret = if func_needs_py {
380 "PyResult<Bound<'py, PyAny>>".to_string()
381 } else {
382 ret
383 };
384 let func_lifetime = if func_needs_py { "<'py>" } else { "" };
385
386 let (func_sig, _params_formatted) = if params.len() > 100 {
387 let mut seen_optional = false;
389 let wrapped_params = func
390 .params
391 .iter()
392 .map(|p| {
393 if p.optional {
394 seen_optional = true;
395 }
396 let ty = if p.optional || seen_optional {
397 format!("Option<{}>", mapper.map_type(&p.ty))
398 } else {
399 mapper.map_type(&p.ty)
400 };
401 format!("{}: {}", p.name, ty)
402 })
403 .collect::<Vec<_>>()
404 .join(",\n ");
405
406 if func_needs_py {
408 (
409 format!(
410 "pub fn {}{func_lifetime}(py: Python<'py>,\n {}\n) -> {ret}",
411 func.name,
412 wrapped_params,
413 ret = ret
414 ),
415 "",
416 )
417 } else {
418 (
419 format!(
420 "pub {async_kw}fn {}(\n {}\n) -> {ret}",
421 func.name,
422 wrapped_params,
423 ret = ret
424 ),
425 "",
426 )
427 }
428 } else if func_needs_py {
429 (
430 format!(
431 "pub fn {}{func_lifetime}(py: Python<'py>, {params}) -> {ret}",
432 func.name
433 ),
434 "",
435 )
436 } else {
437 (format!("pub {async_kw}fn {}({params}) -> {ret}", func.name), "")
438 };
439
440 let mut out = String::with_capacity(1024);
441 let total_params = func.params.len() + if func_needs_py { 1 } else { 0 };
443 if total_params > 7 {
444 writeln!(out, "#[allow(clippy::too_many_arguments)]").ok();
445 }
446 if func.error_type.is_some() {
448 writeln!(out, "#[allow(clippy::missing_errors_doc)]").ok();
449 }
450 let attr_inner = cfg
451 .function_attr
452 .trim_start_matches('#')
453 .trim_start_matches('[')
454 .trim_end_matches(']');
455 writeln!(out, "#[{attr_inner}]").ok();
456 if cfg.needs_signature {
457 let sig = function_sig_defaults(&func.params);
458 writeln!(out, "{}{}{}", cfg.signature_prefix, sig, cfg.signature_suffix).ok();
459 }
460 write!(out, "{} {{\n {body}\n}}", func_sig,).ok();
461 out
462}
463
464pub fn collect_trait_imports(api: &ApiSurface) -> Vec<String> {
471 let mut traits: AHashSet<String> = AHashSet::new();
476 for typ in api.types.iter().filter(|typ| !typ.is_trait) {
477 for method in &typ.methods {
478 if let Some(ref trait_path) = method.trait_source {
479 traits.insert(trait_path.clone());
480 }
481 }
482 }
483
484 let mut by_name: AHashMap<String, String> = AHashMap::new();
486 for path in traits {
487 let name = path.split("::").last().unwrap_or(&path).to_string();
488 let entry = by_name.entry(name).or_insert_with(|| path.clone());
489 if path.len() < entry.len() {
491 *entry = path;
492 }
493 }
494
495 let mut sorted: Vec<String> = by_name.into_values().collect();
496 sorted.sort();
497 sorted
498}
499
500pub fn has_unresolved_trait_methods(api: &ApiSurface) -> bool {
506 let mut method_counts: AHashMap<&str, (usize, usize)> = AHashMap::new(); for typ in api.types.iter().filter(|typ| !typ.is_trait) {
511 if typ.is_trait {
512 continue;
513 }
514 for method in &typ.methods {
515 let entry = method_counts.entry(&method.name).or_insert((0, 0));
516 entry.0 += 1;
517 if method.trait_source.is_some() {
518 entry.1 += 1;
519 }
520 }
521 }
522 method_counts
524 .values()
525 .any(|&(total, with_source)| total >= 3 && with_source == 0)
526}
527
528pub fn collect_explicit_core_imports(api: &ApiSurface) -> Vec<String> {
539 let mut names = std::collections::BTreeSet::new();
540 for typ in api.types.iter().filter(|typ| !typ.is_trait) {
541 names.insert(typ.name.clone());
542 }
543 for e in &api.enums {
544 names.insert(e.name.clone());
545 }
546 names.into_iter().collect()
547}