Expand description
§wry_cmd
A lightweight, async-compatible command system for wry,
inspired by Tauri’s #[command] and invoke architecture.
§Features
#[command]proc-macro for registering sync or async Rust functions.- Automatically exposes commands via a
with_asynchronous_custom_protocolhook. - Simple message format:
POST mado://commandNamewith a JSON body. Note: for Windows, you may need to usehttp://{protocol}.{commandName}instead, due to wry limitations.
§Example
use serde::{Deserialize, Serialize};
use wry::{WebViewBuilder, application::event_loop::EventLoop, application::window::WindowBuilder};
use wry_cmd::{use_wry_cmd_protocol, command};
#[derive(Deserialize)]
struct GreetArgs { name: String }
#[derive(Serialize)]
struct GreetReply { message: String }
#[command]
fn greet(args: GreetArgs) -> GreetReply {
GreetReply { message: format!("Hello, {}!", args.name) }
}
fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop)?;
let webview = WebViewBuilder::new(window, event_loop)
.with_asynchronous_custom_protocol("mado".into(), use_wry_cmd_protocol!("mado"))
.with_url("data:text/html,...") // See README for working example
.build()?;
webview.run();
Ok(())
}§Frontend JavaScript
const res = await fetch("mado://greet", {
method: "POST",
body: JSON.stringify({ name: "Alice" }),
headers: { "Content-Type": "application/json" }
});
const reply = await res.json(); // { message: "Hello, Alice!" }Modules§
Macros§
Structs§
- Command
- A single registered command.
Functions§
- handle_
command - Dispatch an IPC command by name with JSON arguments.
Supports names like
"mycommands/greet"or even"/mycommands/greet"and percent-encoded paths (e.g.%2Fmycommands%2Fgreet).
Type Aliases§
- Command
Handler - Type alias for command handler functions.
Attribute Macros§
- command
- Marks a function as a Wry IPC command.
The function can take zero or one argument implementing
Deserializeand return a type implementingSerialize. If omitted, no args or no return are supported. Use#[command(name = "...")]or just#[command]. - commands
- Attribute macro to auto-generate and register IPC commands from a trait impl.