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::Vec(inner) => {
87                        // Vec<T>: check if elements need conversion
88                        match inner.as_ref() {
89                            TypeRef::Named(_) => {
90                                // Vec<Named>: convert each element using Into::into
91                                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                        // Use explicit type with ::from() to avoid E0283 type inference issues in async context
105                        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                // Async serde path: wrap everything in future_into_py
120                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                    // When wrapped contains type conversions like .into() or ::from(),
129                    // bind to a variable to help type inference for the generic future_into_py.
130                    // This avoids E0283 "type annotations needed".
131                    if wrapped.contains(".into()") || wrapped.contains("::from(") {
132                        // Add explicit type annotation to help type inference
133                        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                // Async serde path for other backends (NAPI, etc.): use gen_async_body
143                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                // Unit return with error: avoid let_unit_value
158                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            // Async function that can't be auto-delegated — wrap unimplemented body in future_into_py
172            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            // Function can't be auto-delegated — return a default/error based on return type
184            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        // MARKER_DELEGATE_ASYNC
195        let core_call = format!("{core_fn_path}({call_args})");
196        // In async contexts (future_into_py, etc.), the compiler often can't infer the
197        // target type for .into(). Use explicit From::from() / collect::<Vec<T>>() instead.
198        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        // Determine return wrapping strategy
241        let returns_ref = func.returns_ref;
242        let wrap_return = |expr: &str| -> String {
243            match &func.return_type {
244                // Opaque type return: wrap in Arc
245                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                // Non-opaque Named: use .into() if From impl exists
253                TypeRef::Named(_name) => {
254                    if returns_ref {
255                        format!("{expr}.clone().into()")
256                    } else {
257                        format!("{expr}.into()")
258                    }
259                }
260                // String/Bytes: .into() handles &str→String etc.
261                TypeRef::String | TypeRef::Bytes => format!("{expr}.into()"),
262                // Path: PathBuf→String needs to_string_lossy
263                TypeRef::Path => format!("{expr}.to_string_lossy().to_string()"),
264                // Json: serde_json::Value to string
265                TypeRef::Json => format!("{expr}.to_string()"),
266                // Optional with opaque inner
267                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                // Vec<Named>: map each element through Into
297                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            // Backend-specific error conversion
323            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    // Prepend let bindings for non-opaque Named params (sync delegate case).
345    // Only prepend when can_delegate is true — the !can_delegate serde path does its own bindings.
346    // However, always prepend Vec<String> ref bindings (names_refs) since serde path doesn't handle them.
347    let body = if !let_bindings.is_empty() && !func.is_async {
348        if can_delegate {
349            format!("{let_bindings}{body}")
350        } else {
351            // For the !can_delegate path, only prepend Vec<String>+is_ref bindings (names_refs)
352            // since serde bindings handle Named type conversions.
353            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                // Handle both Vec<String> and Option<Vec<String>> parameters.
357                // When p.optional=true, p.ty is the inner type (Vec<String>), so we need to unwrap first.
358                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    // Wrap long signature if necessary
375    let async_kw = if func.is_async { "async " } else { "" };
376    let func_needs_py = func.is_async && cfg.async_pattern == AsyncPattern::Pyo3FutureIntoPy;
377
378    // For async PyO3 free functions, override return type and add lifetime generic.
379    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        // When formatting for long signatures, promote optional params like function_params() does
388        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        // For async PyO3, we need special signature handling
407        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    // Per-item clippy suppression: too_many_arguments when >7 params (including py)
442    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    // Per-item clippy suppression: missing_errors_doc for Result-returning functions
447    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
464/// Collect all unique trait import paths from types' methods.
465///
466/// Returns a deduplicated, sorted list of trait paths (e.g. `["liter_llm::LlmClient"]`)
467/// that need to be imported in generated binding code so that trait methods can be called.
468/// Both opaque and non-opaque types are scanned because non-opaque wrapper types also
469/// delegate trait method calls to their inner core type.
470pub fn collect_trait_imports(api: &ApiSurface) -> Vec<String> {
471    // Collect all trait paths, then deduplicate by last segment (trait name).
472    // When two paths resolve to the same trait name (e.g. `spikard_core::Dependency`
473    // and `spikard_core::di::Dependency`), only one import is needed. Keep the
474    // shorter (public re-export) path to avoid E0252 duplicate-import errors.
475    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    // Deduplicate by last path segment: keep the shortest path for each trait name.
485    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        // Prefer shorter paths (public re-exports are shorter than internal paths)
490        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
500/// Check if any type has methods from trait impls whose trait_source could not be resolved.
501///
502/// When true, the binding crate should add a glob import of the core crate (e.g.
503/// `use kreuzberg::*`) to bring all publicly exported traits into scope.
504/// This handles traits defined in private submodules that are re-exported.
505pub fn has_unresolved_trait_methods(api: &ApiSurface) -> bool {
506    // Count method names that appear on multiple non-trait types but lack trait_source.
507    // Such methods likely come from trait impls whose trait path could not be resolved
508    // (e.g. traits defined in private modules but re-exported via `pub use`).
509    let mut method_counts: AHashMap<&str, (usize, usize)> = AHashMap::new(); // (total, with_source)
510    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    // A method appearing on 3+ types without trait_source on any is almost certainly a trait method
523    method_counts
524        .values()
525        .any(|&(total, with_source)| total >= 3 && with_source == 0)
526}
527
528/// Collect explicit type and enum names from the API surface for named imports.
529///
530/// Returns a sorted, deduplicated list of type and enum names that should be
531/// imported from the core crate. This replaces glob imports (`use core::*`)
532/// which can cause name conflicts with local binding definitions (e.g. a
533/// `convert` function or `Result` type alias from the core crate shadowing
534/// the binding's own `convert` wrapper or `std::result::Result`).
535///
536/// Only struct/enum names are included — functions and type aliases are
537/// intentionally excluded because they are the source of conflicts.
538pub 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}