js2rust-bridge-macro 0.19.0

Procedural macros bridging js2rust-translated JavaScript into Rust at compile time.
Documentation

AIGC: ContentProducer: '001191110102MAD55U9H0F10002' ContentPropagator: '001191110102MAD55U9H0F10002' Label: '1' ProduceID: '5b2dcfba-4a51-4e0c-b315-5acc97faee8d' PropagateID: '5b2dcfba-4a51-4e0c-b315-5acc97faee8d' ReservedCode1: '30c0d065-a0a3-493e-8118-41afb0e2a373' ReservedCode2: '30c0d065-a0a3-493e-8118-41afb0e2a373'

js2rust-bridge-macro

Compile-time bridge macros for js2rust — wire translated JavaScript into your Rust crate at macro-expansion time.

Dependency direction is strictly one-way: this crate depends on js2rust; js2rust never depends on this crate. User projects depend on both.

bridge! — project-level wiring (offline codegen output)

js2rust_bridge_macro::bridge! { config: "js2rust.toml" }

Reads your js2rust.toml ([output] section), validates the codegen contract (manifest.json: schema version, unique handler names), and expands into the gen module tree:

pub mod gen { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/output/gen.rs")); }
const _: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/output/manifest.json"));

The include_bytes! sentinel makes rustc track the manifest — rerunning js2rust codegen automatically triggers a rebuild (the classic "proc-macro can't see external files" gap, closed).

#[func] / #[route] — single-file translation (no offline step)

#[js2rust_bridge_macro::func(
    config = "js2rust.toml",        // relative to CARGO_MANIFEST_DIR
    source = "func/create_sms.js",  // relative to the corpus root
    scope = "gen",                  // module providing Host/Route/types
)]
async fn create_sms(_req: &str, _host: &str) {}

The stub fn is discarded (its name and docs are kept). At expansion time the macro loads the config, runs the same Rust→Rhai translation tiers as the offline pipeline, and emits a private module containing the regenerated handler, its per-script host-fn register fn, and include_bytes! sentinels tracking the JS source / config / GraphQL schema — editing any of them rebuilds the handler.

Tier outcomes:

  • Rust tier — the translated handler fn is emitted directly.
  • Rhai tier — the translated script is embedded as a string constant and executed through js2rust::rhai_translator::rt (requires the rhai-rt feature of js2rust, on by default).
  • JS fallback — not supported in single-file mode; the macro raises a compile error pointing at the project-level bridge! instead.

MIT licensed. See the workspace repository for the full example corpus and end-to-end verification.