Crate tauri_wasm

Source
Expand description

tauri-wasm

The tauri framework library for pure rust frontend with wasm-bindgen

§About

Interact with a Tauri backend using the pure Rust library. You don’t need NPM or any other JavaScript tools to build a frontend, use Cargo instead.

This crate is designed for Tauri version 2.0 and above.

§Getting Started

Let’s write the frontend part:

use {
    gloo::console,
    wasm_bindgen::prelude::*,
};

#[wasm_bindgen]
pub async fn start() -> Result<(), JsError> {
    // first, check if we are running in a Tauri environment
    if !tauri_wasm::is_tauri() {
        console::error!("tauri was not detected!");
        return;
    }

    // invoke the "hello" command on the backend
    let message = tauri_wasm::invoke("hello").await?;

    // log the response from the backend
    console::log!("message: ", message);

    Ok(())
}

When wasm_bindgen attribute exports our start function to the JS runtime, we can call it from frontend.

On the backend part let’s write this:

// define the "hello" command
#[tauri::command]
fn hello() -> String {
    println!("frontend invoked hello");
    "message from backend".to_owned()
}

// configure the backend
pub fn run() {
    tauri::Builder::default()
        // register the "hello" command
        .invoke_handler(tauri::generate_handler![hello])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

For more details, see the example in the repository.

Modules§

eventserde
Types of tauri event system.
invoke
Types of tauri commands.

Structs§

Error
Common error type.

Traits§

ToStringValue
A value that can be represented as a JS string.

Functions§

argsserde
Arbitrary serializable data for with_args function.
emitserde
Sends an event to the backend.
invoke
Invokes a command on the backend.
is_tauri
Checks whether tauri environment is detected.