Skip to content

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.

Marks a struct or module for export.

#[bridge]
struct MyStruct;

Marks a module for export, automatically bridging all public items.

#[bridge_module]
mod my_module {
pub fn hello() {}
}

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()?;

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,
}

The macro automatically detects which features (python, nodejs) are enabled in your Cargo.toml and generates code accordingly.

  • Python: Generates #[pyfunction], #[pyclass], and #[pymethods] using pyo3.
  • Node.js: Generates #[napi] attributes using napi-rs.
  • 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).