Attribute Macro command

Source
#[command]
Expand description

Conditionally adds the macro binding or tauri::command to a struct

By using this macro, when compiling to wasm, a version that invokes the current function is generated.

§Collecting commands

When this macro is compiled to the host target, additionally to adding the tauri::command macro, the option to auto collect the command via collect_commands and combine_handlers is provided.

§Binding generation

All parameter arguments with tauri in their name (case-insensitive) are removed as argument in a defined command. That includes tauri::* usages and Tauri named types.

The type returned is evaluated automatically and is most of the time 1:1 to the defined type. When using a wrapped Result<T, E> type, it should include the phrase “Result” in the type name. Otherwise, the returned type can’t be successfully interpreted as a result and by that will result in wrong type/error handling/serialization.

§Example - Definition

#[tauri_interop_macro::command]
fn trigger_something(name: &str) {
    print!("triggers something, but doesn't need to wait for it")
}

#[tauri_interop_macro::command]
fn wait_for_sync_execution(value: &str) -> String {
    format!("Has to wait that the backend completes the computation and returns the {value}")
}

#[tauri_interop_macro::command]
async fn asynchronous_execution(change: bool) -> Result<String, String> {
    if change {
        Ok("asynchronous execution returning result, need Result in their type name".into())
    } else {
        Err("if they don't it, the error will be not be parsed/handled".into())
    }
}

#[tauri_interop_macro::command]
async fn heavy_computation() {
  std::thread::sleep(std::time::Duration::from_millis(5000))
}

§Example - Usage

fn main() {
    trigger_something();

    wasm_bindgen_futures::spawn_local(async move {
        wait_for_sync_execution("value").await;
        asynchronous_execution(true).await.expect("returns ok");
        heavy_computation().await;
    });
}