Skip to content

Commit 0b87532

Browse files
fix(macros): fix rest of command collisons (#1805)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 7862ec5 commit 0b87532

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎.changes/cmd-touch-bindings.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-macros": patch
3+
---
4+
5+
Fixes a name collision when the command function is named `message` or `resolver`.

‎core/tauri-macros/src/command/wrapper.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream {
7474

7575
// prevent warnings when the body is a `compile_error!` or if the command has no arguments
7676
#[allow(unused_variables)]
77-
let ::tauri::Invoke { message, resolver } = $invoke;
77+
let ::tauri::Invoke {
78+
message: __tauri_message__,
79+
resolver: __tauri_resolver__
80+
} = $invoke;
7881

7982
#body
8083
}};
@@ -91,14 +94,14 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream {
9194
///
9295
/// See the [`tauri::command`] module for all the items and traits that make this possible.
9396
///
94-
/// * Requires binding `message` and `resolver`.
97+
/// * Requires binding `__tauri_message__` and `__tauri_resolver__`.
9598
/// * Requires all the traits from `tauri::command::private` to be in scope.
9699
///
97100
/// [`tauri::command`]: https://docs.rs/tauri/*/tauri/runtime/index.html
98101
fn body_async(function: &ItemFn) -> syn::Result<TokenStream2> {
99102
parse_args(function).map(|args| {
100103
quote! {
101-
resolver.respond_async_serialized(async move {
104+
__tauri_resolver__.respond_async_serialized(async move {
102105
let result = $path(#(#args?),*);
103106
let kind = (&result).async_kind();
104107
kind.future(result).await
@@ -111,7 +114,7 @@ fn body_async(function: &ItemFn) -> syn::Result<TokenStream2> {
111114
///
112115
/// See the [`tauri::command`] module for all the items and traits that make this possible.
113116
///
114-
/// * Requires binding `message` and `resolver`.
117+
/// * Requires binding `__tauri_message__` and `__tauri_resolver__`.
115118
/// * Requires all the traits from `tauri::command::private` to be in scope.
116119
///
117120
/// [`tauri::command`]: https://docs.rs/tauri/*/tauri/runtime/index.html
@@ -121,13 +124,13 @@ fn body_blocking(function: &ItemFn) -> syn::Result<TokenStream2> {
121124
// the body of a `match` to early return any argument that wasn't successful in parsing.
122125
let match_body = quote!({
123126
Ok(arg) => arg,
124-
Err(err) => return resolver.invoke_error(err),
127+
Err(err) => return __tauri_resolver__.invoke_error(err),
125128
});
126129

127130
Ok(quote! {
128131
let result = $path(#(match #args #match_body),*);
129132
let kind = (&result).blocking_kind();
130-
kind.block(result, resolver);
133+
kind.block(result, __tauri_resolver__);
131134
})
132135
}
133136

@@ -143,7 +146,7 @@ fn parse_args(function: &ItemFn) -> syn::Result<Vec<TokenStream2>> {
143146

144147
/// Transform a [`FnArg`] into a command argument.
145148
///
146-
/// * Requires binding `message`.
149+
/// * Requires binding `__tauri_message__`.
147150
fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
148151
// we have no use for self arguments
149152
let mut arg = match arg {
@@ -187,7 +190,7 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
187190
::tauri::command::CommandItem {
188191
name: stringify!(#command),
189192
key: #key,
190-
message: &message,
193+
message: &__tauri_message__,
191194
}
192195
)))
193196
}

‎examples/commands/src-tauri/src/commands.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ pub fn cmd(_argument: String) {}
1010
#[command]
1111
pub fn invoke(_argument: String) {}
1212

13+
#[command]
14+
pub fn message(_argument: String) {}
15+
16+
#[command]
17+
pub fn resolver(_argument: String) {}
18+
1319
#[command]
1420
pub fn simple_command(argument: String) {
1521
println!("{}", argument);

‎examples/commands/src-tauri/src/main.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// we move some basic commands to a separate module just to show it works
1111
mod commands;
12-
use commands::{cmd, invoke};
12+
use commands::{cmd, invoke, message, resolver};
1313

1414
use serde::Deserialize;
1515
use tauri::{command, Params, State, Window};
@@ -170,6 +170,8 @@ fn main() {
170170
commands::stateful_command,
171171
cmd,
172172
invoke,
173+
message,
174+
resolver,
173175
async_simple_command,
174176
future_simple_command,
175177
async_stateful_command,

0 commit comments

Comments
 (0)