Crate wry_cmd

Crate wry_cmd 

Source
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_protocol hook.
  • Simple message format: POST mado://commandName with a JSON body. Note: for Windows, you may need to use http://{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§

futures
Abstractions for asynchronous programming.
inventory
githubcrates-iodocs-rs

Macros§

use_wry_cmd_protocol

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§

CommandHandler
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 Deserialize and return a type implementing Serialize. 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.