Typesafe language bindings generator for the Tauri IPC bridge
https://github.com/tauri-apps/tauri-bindgen.git
Note: This project is still under heavy development and may not support all features yet. Feel free to try it out though, create issues, and open PRs!
Generate type-safe bindings for Tauri's IPC bridge. Currently supports Rust Host and Webview frontends (called Guests) in JavaScript, TypeScript, and Rust. Bindings are declared using *.wit files that describe exposed functions and shared types.
Under the current IPC system, Tauri can make no compile-time guarantees on the correctness of your invoke calls and any mistakes will result in nasty runtime errors. For example you might have misspelled a command or parameter name which you will only notice when actually running the app!
tauri-bindgen will generate traits and invoke-handlers for the Host and Guest bindings for JavaScript, TypeScript, Rust and ReScript from a single, shared source of truth using the *.wit format as a Interface Definition Language (IDL). The generated bindings automatically take care of the heavy lifting, such as correctly calling the invoke function and serializing/deserializing parameters and results.
Here are a few reasons why that is cool:
tauri-bindgen can also generate easy to read markdown and html descriptions of your interfaces for documentation purposes.
tauri-bindgen has been designed with some of the early design-work in mind. A big rework of course means breaking changes, but using tauri bindgen isolates from these changes, just update Tauri, update tauri-bindgen, and re-generate your bindings and your code will continue to work!
HostHost refers to the Tauri rust core, so the place where your commands are implemented.
GuestGuest refers to the code running in the Webview, this will usually be written in JavaScript/TypeScript, but can also be written in any other language that runs on the Web (through WASM) like Rust, C, or Swift.
invoke_handler from the interface. This generator can also be used through the tauri-bindgen-host crate (located at crates/host) and, has a generate! macro for generating code.wasm_bindgen that can be used in Rust compile-to-wasm frontend frameworks such as sycamore. You probably want to depend on the tauri-bindgen-guest-rust crate (located at crates/guest-rust) and use the generate! macro to generate code.Declare your interface in a *.wit file:
interface greet {
func greet(name: string) -> string
}
then you can generate the host bindings:
use tauri_bindgen_host::ipc_router_wip::{BuilderExt, Caller, Router};
tauri_bindgen_host::generate!({
path: "../greet.wit",
async: false,
tracing: true
});
#[derive(Clone, Copy)]
struct GreetCtx;
impl greet::Greet for GreetCtx {
fn greet(&self, name: String) -> String {
format!(
"Hello, {}! You've been greeted from code-generated Rust!",
name
)
}
}
fn main() {
let mut router: Router<GreetCtx> = Router::new(GreetCtx {});
greet::add_to_router(&mut router, |ctx| ctx).unwrap();
tauri::Builder::default()
.ipc_router(router)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
and lastly generate client bindings, this can be done for JavaScript, Typescript or ReScript using the following commands:
tauri-bindgen guest javascript greet.wit --prettier // we can run prettier on the generated code. requires a global prettier install
tauri-bindgen guest typescript greet.wit --prettier
tauri-bindgen guest rescript greet.wit --fmt // run `rescript format` on the generated code. requires a global rescript install
or for rust using the provided generate! macro:
tauri_bindgen_guest_rust::generate!({
path: "greet.wit"
});
// now we can call greet
let greeting = greet::greet("Jonas").await;
see also the example.
PRs are welcome!
This project has been inspired by and based off of the awesome wit-bindgen tool for WebAssembly by the Bytecode Alliance.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.