Skip to main content

alef_codegen/generators/
mod.rs

1use ahash::AHashMap;
2
3pub mod binding_helpers;
4pub mod enums;
5pub mod functions;
6pub mod methods;
7pub mod structs;
8
9/// Map of adapter-generated method/function bodies.
10/// Key: "TypeName.method_name" for methods, "function_name" for free functions.
11pub type AdapterBodies = AHashMap<String, String>;
12
13/// Async support pattern for the backend.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum AsyncPattern {
16    /// No async support
17    None,
18    /// PyO3: pyo3_async_runtimes::tokio::future_into_py
19    Pyo3FutureIntoPy,
20    /// NAPI-RS: native async fn → auto-Promise
21    NapiNativeAsync,
22    /// wasm-bindgen: native async fn → auto-Promise
23    WasmNativeAsync,
24    /// Block on Tokio runtime (Ruby, PHP)
25    TokioBlockOn,
26}
27
28/// Configuration for Rust binding code generation.
29pub struct RustBindingConfig<'a> {
30    /// Attrs applied to generated structs, e.g. `["pyclass(frozen)"]`.
31    pub struct_attrs: &'a [&'a str],
32    /// Attrs applied to each field, e.g. `["pyo3(get)"]`.
33    pub field_attrs: &'a [&'a str],
34    /// Derives applied to generated structs, e.g. `["Clone"]`.
35    pub struct_derives: &'a [&'a str],
36    /// Attr wrapping the impl block, e.g. `Some("pymethods")`.
37    pub method_block_attr: Option<&'a str>,
38    /// Attr placed on the constructor, e.g. `"#[new]"`.
39    pub constructor_attr: &'a str,
40    /// Attr placed on static methods, e.g. `Some("staticmethod")`.
41    pub static_attr: Option<&'a str>,
42    /// Attr placed on free functions, e.g. `"#[pyfunction]"`.
43    pub function_attr: &'a str,
44    /// Attrs applied to generated enums, e.g. `["pyclass(eq, eq_int)"]`.
45    pub enum_attrs: &'a [&'a str],
46    /// Derives applied to generated enums, e.g. `["Clone", "PartialEq"]`.
47    pub enum_derives: &'a [&'a str],
48    /// Whether the backend requires `#[pyo3(signature = (...))]`-style annotations.
49    pub needs_signature: bool,
50    /// Prefix for the signature annotation, e.g. `"#[pyo3(signature = ("`.
51    pub signature_prefix: &'a str,
52    /// Suffix for the signature annotation, e.g. `"))]"`.
53    pub signature_suffix: &'a str,
54    /// Core crate import path, e.g. `"liter_llm"`. Used to generate calls into core.
55    pub core_import: &'a str,
56    /// Async pattern supported by this backend.
57    pub async_pattern: AsyncPattern,
58    /// Whether serde/serde_json are available in the output crate's dependencies.
59    /// When true, the generator can use serde-based param conversion and add `serde::Serialize` derives.
60    /// When false, non-convertible Named params fall back to `gen_unimplemented_body`.
61    pub has_serde: bool,
62    /// Prefix for binding type names (e.g. "Js" for NAPI/WASM, "" for PyO3/PHP).
63    /// Used in impl block targets: `impl {prefix}{TypeName}`.
64    pub type_name_prefix: &'a str,
65    /// When true, non-optional Duration fields on `has_default` types are emitted as
66    /// `Option<u64>` in the binding struct so that unset fields fall back to the core
67    /// type's `Default` implementation rather than `Duration::ZERO`.
68    /// Used by PyO3 to prevent validation failures when `request_timeout` is unset.
69    pub option_duration_on_defaults: bool,
70}
71
72/// Method names that conflict with standard trait methods.
73/// When a generated method has one of these names, we add
74/// `#[allow(clippy::should_implement_trait)]` to suppress the lint.
75pub(super) const TRAIT_METHOD_NAMES: &[&str] = &[
76    "default", "from", "from_str", "into", "eq", "ne", "lt", "le", "gt", "ge", "add", "sub", "mul", "div", "rem",
77    "neg", "not", "index", "deref",
78];
79
80// Re-exports for backwards compatibility — callers use `crate::generators::*`.
81pub use binding_helpers::{
82    gen_async_body, gen_call_args, gen_call_args_with_let_bindings, gen_lossy_binding_to_core_fields,
83    gen_named_let_bindings_pub, gen_serde_let_bindings, gen_unimplemented_body, has_named_params,
84    is_simple_non_opaque_param, wrap_return,
85};
86pub use enums::{enum_has_data_variants, gen_enum, gen_pyo3_data_enum};
87pub use functions::{collect_trait_imports, gen_function};
88pub use methods::{
89    gen_constructor, gen_impl_block, gen_method, gen_opaque_impl_block, gen_static_method, is_trait_method_name,
90};
91pub use structs::{
92    gen_opaque_struct, gen_opaque_struct_prefixed, gen_struct, gen_struct_default_impl, gen_struct_with_per_field_attrs,
93};