Skip to main content

alef_codegen/generators/
functions.rs

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
12/// Generate a free function.
13pub 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    // Use let-binding pattern for non-opaque Named params so core fns can take &CoreType
26    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    // Use the function's rust_path for correct module path resolution
40    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    // Backend-specific error conversion string for serde bindings
52    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    // Generate the body based on async pattern
60    let body = if !can_delegate {
61        // Check if an adapter provides the body
62        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            // MARKER_SERDE_PATH
66            // Serde-based param conversion: serialize binding types to JSON, deserialize to core types.
67            // This handles Named params (e.g., ProcessConfig) that lack binding→core From impls.
68            // For async functions with Pyo3FutureIntoPy, serde bindings use indented format.
69            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            // Determine return wrapping strategy for serde async (uses explicit types to avoid E0283)
83            let returns_ref = func.returns_ref;
84            let wrap_return = |expr: &str| -> String {
85                match &func.return_type {
86                    TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
87                        if returns_ref {
88                            format!("{name} {{ inner: Arc::new({expr}.clone()) }}")
89                        } else {
90                            format!("{name} {{ inner: Arc::new({expr}) }}")
91                        }
92                    }
93                    TypeRef::Named(_) => {
94                        // Use explicit type with ::from() to avoid E0283 type inference issues in async context
95                        if returns_ref {
96                            format!("{return_type}::from({expr}.clone())")
97                        } else {
98                            format!("{return_type}::from({expr})")
99                        }
100                    }
101                    TypeRef::String | TypeRef::Bytes => format!("{expr}.into()"),
102                    TypeRef::Path => format!("{expr}.to_string_lossy().to_string()"),
103                    TypeRef::Json => format!("{expr}.to_string()"),
104                    _ => expr.to_string(),
105                }
106            };
107
108            if is_async_pyo3 {
109                // Async serde path: wrap everything in future_into_py
110                let is_unit = matches!(func.return_type, TypeRef::Unit);
111                let wrapped = wrap_return("result");
112                let core_await = format!(
113                    "{core_call}.await\n            .map_err(|e| PyErr::new::<PyRuntimeError, _>(e.to_string()))?"
114                );
115                let inner_body = if is_unit {
116                    format!("{serde_bindings}{core_await};\n            Ok(())")
117                } else {
118                    // When wrapped contains type conversions like .into() or ::from(),
119                    // bind to a variable to help type inference for the generic future_into_py.
120                    // This avoids E0283 "type annotations needed".
121                    if wrapped.contains(".into()") || wrapped.contains("::from(") {
122                        // Add explicit type annotation to help type inference
123                        format!(
124                            "{serde_bindings}let result = {core_await};\n            let wrapped_result: {return_type} = {wrapped};\n            Ok(wrapped_result)"
125                        )
126                    } else {
127                        format!("{serde_bindings}let result = {core_await};\n            Ok({wrapped})")
128                    }
129                };
130                format!("pyo3_async_runtimes::tokio::future_into_py(py, async move {{\n{inner_body}\n        }})")
131            } else if func.is_async {
132                // Async serde path for other backends (NAPI, etc.): use gen_async_body
133                let is_unit = matches!(func.return_type, TypeRef::Unit);
134                let wrapped = wrap_return("result");
135                let async_body = gen_async_body(
136                    &core_call,
137                    cfg,
138                    func.error_type.is_some(),
139                    &wrapped,
140                    false,
141                    "",
142                    is_unit,
143                    Some(&return_type),
144                );
145                format!("{serde_bindings}{async_body}")
146            } else if matches!(func.return_type, TypeRef::Unit) {
147                // Unit return with error: avoid let_unit_value
148                let await_kw = if func.is_async { ".await" } else { "" };
149                let debug_marker = if func.is_async { "/*ASYNC_UNIT*/ " } else { "" };
150                format!("{serde_bindings}{debug_marker}{core_call}{await_kw}{serde_err_conv}?;\n    Ok(())")
151            } else {
152                let wrapped = wrap_return("val");
153                let await_kw = if func.is_async { ".await" } else { "" };
154                if wrapped == "val" {
155                    format!("{serde_bindings}{core_call}{await_kw}{serde_err_conv}")
156                } else {
157                    format!("{serde_bindings}{core_call}{await_kw}.map(|val| {wrapped}){serde_err_conv}")
158                }
159            }
160        } else if func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy {
161            // Async function that can't be auto-delegated — wrap unimplemented body in future_into_py
162            let suppress = if func.params.is_empty() {
163                String::new()
164            } else {
165                let names: Vec<&str> = func.params.iter().map(|p| p.name.as_str()).collect();
166                format!("let _ = ({});\n        ", names.join(", "))
167            };
168            format!(
169                "{suppress}Err(pyo3::exceptions::PyNotImplementedError::new_err(\"not implemented: {}\"))",
170                func.name
171            )
172        } else {
173            // Function can't be auto-delegated — return a default/error based on return type
174            gen_unimplemented_body(
175                &func.return_type,
176                &func.name,
177                func.error_type.is_some(),
178                cfg,
179                &func.params,
180                opaque_types,
181            )
182        }
183    } else if func.is_async {
184        // MARKER_DELEGATE_ASYNC
185        let core_call = format!("{core_fn_path}({call_args})");
186        // In async contexts (future_into_py, etc.), the compiler often can't infer the
187        // target type for .into(). Use explicit From::from() / collect::<Vec<T>>() instead.
188        let return_wrap = match &func.return_type {
189            TypeRef::Named(n) if opaque_types.contains(n.as_str()) => {
190                format!("{n} {{ inner: Arc::new(result) }}")
191            }
192            TypeRef::Named(_) => {
193                format!("{return_type}::from(result)")
194            }
195            TypeRef::Vec(inner) => match inner.as_ref() {
196                TypeRef::Named(n) if opaque_types.contains(n.as_str()) => {
197                    format!("result.into_iter().map(|v| {n} {{ inner: Arc::new(v) }}).collect::<Vec<_>>()")
198                }
199                TypeRef::Named(_) => {
200                    let inner_mapped = mapper.map_type(inner);
201                    format!("result.into_iter().map({inner_mapped}::from).collect::<Vec<_>>()")
202                }
203                _ => "result".to_string(),
204            },
205            TypeRef::Unit => "result".to_string(),
206            _ => super::binding_helpers::wrap_return(
207                "result",
208                &func.return_type,
209                "",
210                opaque_types,
211                false,
212                func.returns_ref,
213                false,
214            ),
215        };
216        let async_body = gen_async_body(
217            &core_call,
218            cfg,
219            func.error_type.is_some(),
220            &return_wrap,
221            false,
222            "",
223            matches!(func.return_type, TypeRef::Unit),
224            Some(&return_type),
225        );
226        format!("{let_bindings}{async_body}")
227    } else {
228        let core_call = format!("{core_fn_path}({call_args})");
229
230        // Determine return wrapping strategy
231        let returns_ref = func.returns_ref;
232        let wrap_return = |expr: &str| -> String {
233            match &func.return_type {
234                // Opaque type return: wrap in Arc
235                TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
236                    if returns_ref {
237                        format!("{name} {{ inner: Arc::new({expr}.clone()) }}")
238                    } else {
239                        format!("{name} {{ inner: Arc::new({expr}) }}")
240                    }
241                }
242                // Non-opaque Named: use .into() if From impl exists
243                TypeRef::Named(_name) => {
244                    if returns_ref {
245                        format!("{expr}.clone().into()")
246                    } else {
247                        format!("{expr}.into()")
248                    }
249                }
250                // String/Bytes: .into() handles &str→String etc.
251                TypeRef::String | TypeRef::Bytes => format!("{expr}.into()"),
252                // Path: PathBuf→String needs to_string_lossy
253                TypeRef::Path => format!("{expr}.to_string_lossy().to_string()"),
254                // Json: serde_json::Value to string
255                TypeRef::Json => format!("{expr}.to_string()"),
256                // Optional with opaque inner
257                TypeRef::Optional(inner) => match inner.as_ref() {
258                    TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
259                        if returns_ref {
260                            format!("{expr}.map(|v| {name} {{ inner: Arc::new(v.clone()) }})")
261                        } else {
262                            format!("{expr}.map(|v| {name} {{ inner: Arc::new(v) }})")
263                        }
264                    }
265                    TypeRef::Named(_) => {
266                        if returns_ref {
267                            format!("{expr}.map(|v| v.clone().into())")
268                        } else {
269                            format!("{expr}.map(Into::into)")
270                        }
271                    }
272                    TypeRef::String | TypeRef::Bytes | TypeRef::Path => {
273                        format!("{expr}.map(Into::into)")
274                    }
275                    TypeRef::Vec(vi) => match vi.as_ref() {
276                        TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
277                            format!("{expr}.map(|v| v.into_iter().map(|x| {name} {{ inner: Arc::new(x) }}).collect())")
278                        }
279                        TypeRef::Named(_) => {
280                            format!("{expr}.map(|v| v.into_iter().map(Into::into).collect())")
281                        }
282                        _ => expr.to_string(),
283                    },
284                    _ => expr.to_string(),
285                },
286                // Vec<Named>: map each element through Into
287                TypeRef::Vec(inner) => match inner.as_ref() {
288                    TypeRef::Named(name) if opaque_types.contains(name.as_str()) => {
289                        if returns_ref {
290                            format!("{expr}.into_iter().map(|v| {name} {{ inner: Arc::new(v.clone()) }}).collect()")
291                        } else {
292                            format!("{expr}.into_iter().map(|v| {name} {{ inner: Arc::new(v) }}).collect()")
293                        }
294                    }
295                    TypeRef::Named(_) => {
296                        if returns_ref {
297                            format!("{expr}.into_iter().map(|v| v.clone().into()).collect()")
298                        } else {
299                            format!("{expr}.into_iter().map(Into::into).collect()")
300                        }
301                    }
302                    TypeRef::String | TypeRef::Bytes | TypeRef::Path => {
303                        format!("{expr}.into_iter().map(Into::into).collect()")
304                    }
305                    _ => expr.to_string(),
306                },
307                _ => expr.to_string(),
308            }
309        };
310
311        if func.error_type.is_some() {
312            // Backend-specific error conversion
313            let err_conv = match cfg.async_pattern {
314                AsyncPattern::Pyo3FutureIntoPy => {
315                    ".map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))"
316                }
317                AsyncPattern::NapiNativeAsync => {
318                    ".map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))"
319                }
320                AsyncPattern::WasmNativeAsync => ".map_err(|e| JsValue::from_str(&e.to_string()))",
321                _ => ".map_err(|e| e.to_string())",
322            };
323            let wrapped = wrap_return("val");
324            if wrapped == "val" {
325                format!("{core_call}{err_conv}")
326            } else {
327                format!("{core_call}.map(|val| {wrapped}){err_conv}")
328            }
329        } else {
330            wrap_return(&core_call)
331        }
332    };
333
334    // Prepend let bindings for non-opaque Named params (sync delegate case).
335    // Only prepend when can_delegate is true — the !can_delegate serde path does its own bindings.
336    // However, always prepend Vec<String> ref bindings (names_refs) since serde path doesn't handle them.
337    let body = if !let_bindings.is_empty() && !func.is_async {
338        if can_delegate {
339            format!("{let_bindings}{body}")
340        } else {
341            // For the !can_delegate path, only prepend Vec<String>+is_ref bindings (names_refs)
342            // since serde bindings handle Named type conversions.
343            let vec_str_bindings: String = func.params.iter().filter(|p| {
344                p.is_ref && matches!(&p.ty, TypeRef::Vec(inner) if matches!(inner.as_ref(), TypeRef::String | TypeRef::Char))
345            }).map(|p| {
346                // Handle both Vec<String> and Option<Vec<String>> parameters.
347                // When p.optional=true, p.ty is the inner type (Vec<String>), so we need to unwrap first.
348                if p.optional {
349                    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)
350                } else {
351                    format!("let {}_refs: Vec<&str> = {}.iter().map(|s| s.as_str()).collect();\n    ", p.name, p.name)
352                }
353            }).collect();
354            if !vec_str_bindings.is_empty() {
355                format!("{vec_str_bindings}{body}")
356            } else {
357                body
358            }
359        }
360    } else {
361        body
362    };
363
364    // Wrap long signature if necessary
365    let async_kw = if func.is_async { "async " } else { "" };
366    let func_needs_py = func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy;
367
368    // For async PyO3 free functions, override return type and add lifetime generic.
369    let ret = if func_needs_py {
370        "PyResult<Bound<'py, PyAny>>".to_string()
371    } else {
372        ret
373    };
374    let func_lifetime = if func_needs_py { "<'py>" } else { "" };
375
376    let (func_sig, _params_formatted) = if params.len() > 100 {
377        // When formatting for long signatures, promote optional params like function_params() does
378        let mut seen_optional = false;
379        let wrapped_params = func
380            .params
381            .iter()
382            .map(|p| {
383                if p.optional {
384                    seen_optional = true;
385                }
386                let ty = if p.optional || seen_optional {
387                    format!("Option<{}>", mapper.map_type(&p.ty))
388                } else {
389                    mapper.map_type(&p.ty)
390                };
391                format!("{}: {}", p.name, ty)
392            })
393            .collect::<Vec<_>>()
394            .join(",\n    ");
395
396        // For async PyO3, we need special signature handling
397        if func_needs_py {
398            (
399                format!(
400                    "pub fn {}{func_lifetime}(py: Python<'py>,\n    {}\n) -> {ret}",
401                    func.name,
402                    wrapped_params,
403                    ret = ret
404                ),
405                "",
406            )
407        } else {
408            (
409                format!(
410                    "pub {async_kw}fn {}(\n    {}\n) -> {ret}",
411                    func.name,
412                    wrapped_params,
413                    ret = ret
414                ),
415                "",
416            )
417        }
418    } else if func_needs_py {
419        (
420            format!(
421                "pub fn {}{func_lifetime}(py: Python<'py>, {params}) -> {ret}",
422                func.name
423            ),
424            "",
425        )
426    } else {
427        (format!("pub {async_kw}fn {}({params}) -> {ret}", func.name), "")
428    };
429
430    let mut out = String::with_capacity(1024);
431    // Per-item clippy suppression: too_many_arguments when >7 params (including py)
432    let total_params = func.params.len() + if func_needs_py { 1 } else { 0 };
433    if total_params > 7 {
434        writeln!(out, "#[allow(clippy::too_many_arguments)]").ok();
435    }
436    // Per-item clippy suppression: missing_errors_doc for Result-returning functions
437    if func.error_type.is_some() {
438        writeln!(out, "#[allow(clippy::missing_errors_doc)]").ok();
439    }
440    let attr_inner = cfg
441        .function_attr
442        .trim_start_matches('#')
443        .trim_start_matches('[')
444        .trim_end_matches(']');
445    writeln!(out, "#[{attr_inner}]").ok();
446    if cfg.needs_signature {
447        let sig = function_sig_defaults(&func.params);
448        writeln!(out, "{}{}{}", cfg.signature_prefix, sig, cfg.signature_suffix).ok();
449    }
450    write!(out, "{} {{\n    {body}\n}}", func_sig,).ok();
451    out
452}
453
454/// Collect all unique trait import paths from types' methods.
455///
456/// Returns a deduplicated, sorted list of trait paths (e.g. `["liter_llm::LlmClient"]`)
457/// that need to be imported in generated binding code so that trait methods can be called.
458/// Both opaque and non-opaque types are scanned because non-opaque wrapper types also
459/// delegate trait method calls to their inner core type.
460pub fn collect_trait_imports(api: &ApiSurface) -> Vec<String> {
461    // Collect all trait paths, then deduplicate by last segment (trait name).
462    // When two paths resolve to the same trait name (e.g. `spikard_core::Dependency`
463    // and `spikard_core::di::Dependency`), only one import is needed. Keep the
464    // shorter (public re-export) path to avoid E0252 duplicate-import errors.
465    let mut traits: AHashSet<String> = AHashSet::new();
466    for typ in api.types.iter().filter(|typ| !typ.is_trait) {
467        for method in &typ.methods {
468            if let Some(ref trait_path) = method.trait_source {
469                traits.insert(trait_path.clone());
470            }
471        }
472    }
473
474    // Deduplicate by last path segment: keep the shortest path for each trait name.
475    let mut by_name: AHashMap<String, String> = AHashMap::new();
476    for path in traits {
477        let name = path.split("::").last().unwrap_or(&path).to_string();
478        let entry = by_name.entry(name).or_insert_with(|| path.clone());
479        // Prefer shorter paths (public re-exports are shorter than internal paths)
480        if path.len() < entry.len() {
481            *entry = path;
482        }
483    }
484
485    let mut sorted: Vec<String> = by_name.into_values().collect();
486    sorted.sort();
487    sorted
488}
489
490/// Check if any type has methods from trait impls whose trait_source could not be resolved.
491///
492/// When true, the binding crate should add a glob import of the core crate (e.g.
493/// `use kreuzberg::*`) to bring all publicly exported traits into scope.
494/// This handles traits defined in private submodules that are re-exported.
495pub fn has_unresolved_trait_methods(api: &ApiSurface) -> bool {
496    // Count method names that appear on multiple non-trait types but lack trait_source.
497    // Such methods likely come from trait impls whose trait path could not be resolved
498    // (e.g. traits defined in private modules but re-exported via `pub use`).
499    let mut method_counts: AHashMap<&str, (usize, usize)> = AHashMap::new(); // (total, with_source)
500    for typ in api.types.iter().filter(|typ| !typ.is_trait) {
501        if typ.is_trait {
502            continue;
503        }
504        for method in &typ.methods {
505            let entry = method_counts.entry(&method.name).or_insert((0, 0));
506            entry.0 += 1;
507            if method.trait_source.is_some() {
508                entry.1 += 1;
509            }
510        }
511    }
512    // A method appearing on 3+ types without trait_source on any is almost certainly a trait method
513    method_counts
514        .values()
515        .any(|&(total, with_source)| total >= 3 && with_source == 0)
516}
517
518/// Collect explicit type and enum names from the API surface for named imports.
519///
520/// Returns a sorted, deduplicated list of type and enum names that should be
521/// imported from the core crate. This replaces glob imports (`use core::*`)
522/// which can cause name conflicts with local binding definitions (e.g. a
523/// `convert` function or `Result` type alias from the core crate shadowing
524/// the binding's own `convert` wrapper or `std::result::Result`).
525///
526/// Only struct/enum names are included — functions and type aliases are
527/// intentionally excluded because they are the source of conflicts.
528pub fn collect_explicit_core_imports(api: &ApiSurface) -> Vec<String> {
529    let mut names = std::collections::BTreeSet::new();
530    for typ in api.types.iter().filter(|typ| !typ.is_trait) {
531        names.insert(typ.name.clone());
532    }
533    for e in &api.enums {
534        names.insert(e.name.clone());
535    }
536    names.into_iter().collect()
537}