Skip to main content

alef_core/config/
trait_bridge.rs

1use serde::{Deserialize, Serialize};
2
3/// Configuration for generating trait bridge code that allows foreign language
4/// objects to implement Rust traits via FFI.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct TraitBridgeConfig {
7    /// Name of the Rust trait to bridge (e.g., `"OcrBackend"`).
8    pub trait_name: String,
9    /// Super-trait that requires forwarding (e.g., `"Plugin"`).
10    /// When set, the bridge generates an `impl SuperTrait for Wrapper` block.
11    #[serde(default)]
12    pub super_trait: Option<String>,
13    /// Rust path to the registry getter function
14    /// (e.g., `"kreuzberg::plugins::registry::get_ocr_backend_registry"`).
15    /// Optional — when set, the generated registration function inserts the bridge into a registry.
16    #[serde(default)]
17    pub registry_getter: Option<String>,
18    /// Name of the registration function to generate
19    /// (e.g., `"register_ocr_backend"`).
20    /// Optional — when set, a `#[pyfunction]` registration function is generated.
21    /// When absent, only the wrapper struct and trait impl are emitted (per-call bridge pattern).
22    #[serde(default)]
23    pub register_fn: Option<String>,
24    /// Named type alias in the IR that maps to this bridge (e.g., `"VisitorHandle"`).
25    ///
26    /// When a function parameter has a `TypeRef::Named` matching this alias, code
27    /// generators replace the parameter type with the language-native callback object
28    /// (e.g., `Py<PyAny>` for Python) and emit wrapping code to construct the bridge.
29    #[serde(default)]
30    pub type_alias: Option<String>,
31    /// Parameter name override — when the extractor sanitizes the type (e.g., `VisitorHandle`
32    /// becomes `String` because it is a type alias over `Rc<RefCell<dyn Trait>>`), use the
33    /// parameter name instead of the IR type to detect which parameter to bridge.
34    ///
35    /// For example, `param_name = "visitor"` ensures that a sanitized `visitor: Option<String>`
36    /// parameter is still treated as a bridge param for this trait.
37    #[serde(default)]
38    pub param_name: Option<String>,
39    /// Extra arguments to append to the `registry.register(arc, ...)` call.
40    /// Example: `"0"` produces `registry.register(arc, 0)`.
41    #[serde(default)]
42    pub register_extra_args: Option<String>,
43    /// Language backends that should NOT generate this trait bridge.
44    /// Use backend names as they appear in `Backend::name()`, e.g. `["elixir", "wasm"]`.
45    /// When a backend's name is listed here, the bridge struct and all related code are
46    /// omitted from that backend's output.
47    #[serde(default)]
48    pub exclude_languages: Vec<String>,
49}