The Export Macro
The core of BridgeRust is the #[export] attribute macro. Placing this attribute on your Rust items instructs the framework to generate the necessary bindings for Python and Node.js.
#[bridge]
Section titled “#[bridge]”Marks a struct or module for export.
#[bridge]struct MyStruct;#[bridge_module]
Section titled “#[bridge_module]”Marks a module for export, automatically bridging all public items.
#[bridge_module]mod my_module { pub fn hello() {}}#[validate]
Section titled “#[validate]”Add validation to struct fields.
#[bridge]struct User { #[validate(email)] email: String,}Note:
#[validate(...)]now generates runtime validation checks on exported structs via BridgeRust’s validation runtime (RuntimeValidate).
use bridgerust::validation::RuntimeValidate;
let user = User { email: "a@b.com".to_string() };user.runtime_validate()?;Exporting Methods
Section titled “Exporting Methods”Structs
Section titled “Structs”Export structs to create classes in the target languages.
#[bridgerust::export]pub struct Point { pub x: f64, pub y: f64,}Export enums to represent shared state or options.
#[bridgerust::export]pub enum Status { Active, Inactive, Pending,}Configuration
Section titled “Configuration”The macro automatically detects which features (python, nodejs) are enabled in your Cargo.toml and generates code accordingly.
- Python: Generates
#[pyfunction],#[pyclass], and#[pymethods]usingpyo3. - Node.js: Generates
#[napi]attributes usingnapi-rs.
Limitations
Section titled “Limitations”- Items must be
pub. - Generic types are not directly supported (due to FFI limitations). Use concrete types or wrapper structs.
- Async functions are supported but require specific runtime configuration (Tokio is recommended).