tauri-apps/tauri
tauri-macros
Active contributors: Lucas Fernandes Nogueira, Amr Bashir
Purpose
crates/tauri-macros is the home of every proc macro Tauri ships. Concretely:
#[tauri::command]— turn a Rust function into a Tauri command callable from JS.tauri::generate_handler!— register a slice of commands with the IPC dispatcher.tauri::generate_context!— readtauri.conf.jsonand embed everythingBuilder::buildneeds.tauri::include_image!— embed an image asset by path.tauri::mobile_entry_point— emit the platform-specific entry symbol on Android/iOS.
Directory layout
crates/tauri-macros/
├── Cargo.toml
├── src/
│ ├── lib.rs # the proc-macro front door (#[command], generate_handler!, …)
│ ├── command/ # parsing for #[tauri::command]
│ ├── command_module.rs
│ ├── context.rs # generate_context!
│ ├── menu.rs
│ └── runtime.rs
└── build.rsKey macros
| Macro | Defined in | What it does |
|---|---|---|
#[command] |
crates/tauri-macros/src/command/mod.rs |
Wraps a function so it can be invoked from JS. Generates an internal type that decodes args and dispatches. |
generate_handler! |
crates/tauri-macros/src/command/handler.rs |
Produces a closure that maps a command name to its #[command]-generated dispatcher. |
generate_context! |
crates/tauri-macros/src/context.rs |
Calls into tauri-codegen to embed tauri.conf.json, frontend assets, ACL, default window icon, etc. |
include_image! |
crates/tauri-macros/src/lib.rs |
Compile-time image::Image from a file path. |
mobile_entry_point |
crates/tauri-macros/src/lib.rs |
Emits the platform-specific entry symbol for Android/iOS apps. |
How it works
#[tauri::command] rewrites the user function into a wrapper that:
- Looks up the named arguments from the incoming
serde_json::Value. - Resolves Tauri-specific extractors via
tauri::ipc::CommandArg(e.g.State<T>,Window,AppHandle,Webview). - Calls the original function.
- Serialises the return value (or error) back into the IPC response.
generate_handler! macro-expands to a match over command names. Misnamed commands fail at compile time, not runtime.
graph LR
src["fn my_cmd(state: State<MyState>, name: String) -> ..."] -->|"#[command]"| Wrapped["__cmd__my_cmd"]
Wrapped -->|"generate_handler![my_cmd]"| Handler["dispatcher closure"]
Handler -->|"Builder::invoke_handler(...)"| App["tauri::App"]Cargo features
compression— enable compression intauri-codegenwhen generating the embedded asset bundle.tracing— emittracinginstrumentation in the generated wrappers.custom-protocol— flag passed through fromtauri-clito switch the embedded asset behaviour to production.config-json5/config-toml— accept JSON5 or TOML fortauri.conf.json.isolation— enable the isolation pattern code path ingenerate_context!.
Integration points
- Up:
crates/taurire-exports the macros (pub use tauri_macros::{command, generate_handler};incrates/tauri/src/lib.rs). Plugin authors usetauri::commandetc., nottauri_macros::command. - Down: depends on
tauri-codegenfor the heavy lifting ingenerate_context!and onsyn/quote/proc-macro2for parsing.
Entry points for modification
| Goal | Start here |
|---|---|
Add a new argument extractor (like State) |
crates/tauri-macros/src/command/wrapper.rs plus the CommandArg impl in crates/tauri/src/ipc/command.rs |
| Change command return-type handling | crates/tauri-macros/src/command/ |
Adjust how generate_context! embeds assets |
crates/tauri-codegen/src/context.rs (called from here) |
| Add a new convenience macro | crates/tauri-macros/src/lib.rs |
See tauri-codegen for the work that backs generate_context!, and systems/ipc-and-commands for the runtime side of #[command].
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.