Add Tauri Command Skill
When to use this skill
- •When the user asks to "add a command", "expose a function", or "connect frontend to backend".
Workflow
1. Backend Implementation (src-tauri/src/lib.rs)
- •Define the function with
#[tauri::command]. - •Use
Result<T, String>for return types to handle errors gracefully. - •Add the function name to the
invoke_handler!macro in therun()function.
rust
/// Doc comment explaining the command
#[tauri::command]
fn my_new_command(arg: String) -> Result<String, String> {
// Implementation
Ok("success".to_string())
}
2. Frontend API Wrapper (src/lib/emulator-api.ts)
- •Add a static method to the
EmulatorAPIclass. - •Use the
invokefunction to call the backend command. - •Ensure inputs and outputs are typed (do not use
any).
typescript
static async myNewCommand(arg: string): Promise<string> {
return await invoke('my_new_command', { arg });
}
3. Type Definitions (src/lib/emulator-types.ts)
- •If the command returns a custom struct, define the interface here.
- •Ensure the interface matches the Rust struct (which should derive
Serialize).
4. Verification
- •Run the app:
pnpm tauri dev - •Test the command from the frontend.