Skip to main content

alef_core/
backend.rs

1use crate::config::{AlefConfig, Language};
2use crate::ir::ApiSurface;
3use std::path::PathBuf;
4
5/// Build configuration for a language backend.
6#[derive(Debug, Clone)]
7pub struct BuildConfig {
8    /// Build tool name (e.g., "napi", "maturin", "wasm-pack", "cargo", "mvn", "dotnet", "mix").
9    pub tool: &'static str,
10    /// Crate suffix for Rust binding crate (e.g., "-node", "-py", "-wasm", "-ffi").
11    pub crate_suffix: &'static str,
12    /// Whether this language depends on the FFI crate being built first (Go, Java, C#).
13    pub depends_on_ffi: bool,
14    /// Post-processing steps to run after build.
15    pub post_build: Vec<PostBuildStep>,
16}
17
18/// A post-build processing step.
19#[derive(Debug, Clone)]
20pub enum PostBuildStep {
21    /// Replace all occurrences of `find` with `replace` in `path` (relative to crate dir).
22    PatchFile {
23        /// File path relative to the binding crate directory.
24        path: &'static str,
25        /// Text to find.
26        find: &'static str,
27        /// Text to replace with.
28        replace: &'static str,
29    },
30}
31
32/// A generated file to write to disk.
33#[derive(Debug, Clone)]
34pub struct GeneratedFile {
35    /// Path relative to the output root.
36    pub path: PathBuf,
37    /// File content.
38    pub content: String,
39    /// Whether to prepend a "DO NOT EDIT" header.
40    pub generated_header: bool,
41}
42
43/// Capabilities supported by a backend.
44#[derive(Debug, Clone, Default)]
45pub struct Capabilities {
46    pub supports_async: bool,
47    pub supports_classes: bool,
48    pub supports_enums: bool,
49    pub supports_option: bool,
50    pub supports_result: bool,
51    pub supports_callbacks: bool,
52    pub supports_streaming: bool,
53}
54
55/// Trait that all language backends implement.
56pub trait Backend: Send + Sync {
57    /// Backend identifier (e.g., "pyo3", "napi", "ffi").
58    fn name(&self) -> &str;
59
60    /// Target language.
61    fn language(&self) -> Language;
62
63    /// What this backend supports.
64    fn capabilities(&self) -> Capabilities;
65
66    /// Generate binding source code.
67    fn generate_bindings(&self, api: &ApiSurface, config: &AlefConfig) -> anyhow::Result<Vec<GeneratedFile>>;
68
69    /// Generate type stubs (.pyi, .rbs, .d.ts). Optional — default returns empty.
70    fn generate_type_stubs(&self, _api: &ApiSurface, _config: &AlefConfig) -> anyhow::Result<Vec<GeneratedFile>> {
71        Ok(vec![])
72    }
73
74    /// Generate package scaffolding. Optional — default returns empty.
75    fn generate_scaffold(&self, _api: &ApiSurface, _config: &AlefConfig) -> anyhow::Result<Vec<GeneratedFile>> {
76        Ok(vec![])
77    }
78
79    /// Generate language-native public API wrappers. Optional — default returns empty.
80    fn generate_public_api(&self, _api: &ApiSurface, _config: &AlefConfig) -> anyhow::Result<Vec<GeneratedFile>> {
81        Ok(vec![])
82    }
83
84    /// Build configuration for this backend. Returns `None` if build is not supported.
85    fn build_config(&self) -> Option<BuildConfig> {
86        None
87    }
88}