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