diff --git a/.changes/cmd-touch-bindings.md b/.changes/cmd-touch-bindings.md new file mode 100644 index 00000000000..4872790badb --- /dev/null +++ b/.changes/cmd-touch-bindings.md @@ -0,0 +1,5 @@ +--- +"tauri-macros": patch +--- + +Fixes a name collision when the command function is named `message` or `resolver`. diff --git a/core/tauri-macros/src/command/wrapper.rs b/core/tauri-macros/src/command/wrapper.rs index a23ba80b55d..88b465c7c76 100644 --- a/core/tauri-macros/src/command/wrapper.rs +++ b/core/tauri-macros/src/command/wrapper.rs @@ -74,7 +74,10 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream { // prevent warnings when the body is a `compile_error!` or if the command has no arguments #[allow(unused_variables)] - let ::tauri::Invoke { message, resolver } = $invoke; + let ::tauri::Invoke { + message: __tauri_message__, + resolver: __tauri_resolver__ + } = $invoke; #body }}; @@ -91,14 +94,14 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream { /// /// See the [`tauri::command`] module for all the items and traits that make this possible. /// -/// * Requires binding `message` and `resolver`. +/// * Requires binding `__tauri_message__` and `__tauri_resolver__`. /// * Requires all the traits from `tauri::command::private` to be in scope. /// /// [`tauri::command`]: https://docs.rs/tauri/*/tauri/runtime/index.html fn body_async(function: &ItemFn) -> syn::Result { parse_args(function).map(|args| { quote! { - resolver.respond_async_serialized(async move { + __tauri_resolver__.respond_async_serialized(async move { let result = $path(#(#args?),*); let kind = (&result).async_kind(); kind.future(result).await @@ -111,7 +114,7 @@ fn body_async(function: &ItemFn) -> syn::Result { /// /// See the [`tauri::command`] module for all the items and traits that make this possible. /// -/// * Requires binding `message` and `resolver`. +/// * Requires binding `__tauri_message__` and `__tauri_resolver__`. /// * Requires all the traits from `tauri::command::private` to be in scope. /// /// [`tauri::command`]: https://docs.rs/tauri/*/tauri/runtime/index.html @@ -121,13 +124,13 @@ fn body_blocking(function: &ItemFn) -> syn::Result { // the body of a `match` to early return any argument that wasn't successful in parsing. let match_body = quote!({ Ok(arg) => arg, - Err(err) => return resolver.invoke_error(err), + Err(err) => return __tauri_resolver__.invoke_error(err), }); Ok(quote! { let result = $path(#(match #args #match_body),*); let kind = (&result).blocking_kind(); - kind.block(result, resolver); + kind.block(result, __tauri_resolver__); }) } @@ -143,7 +146,7 @@ fn parse_args(function: &ItemFn) -> syn::Result> { /// Transform a [`FnArg`] into a command argument. /// -/// * Requires binding `message`. +/// * Requires binding `__tauri_message__`. fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result { // we have no use for self arguments let mut arg = match arg { @@ -187,7 +190,7 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result { ::tauri::command::CommandItem { name: stringify!(#command), key: #key, - message: &message, + message: &__tauri_message__, } ))) } diff --git a/examples/commands/src-tauri/src/commands.rs b/examples/commands/src-tauri/src/commands.rs index 59ba81ad7c2..b9c4c105582 100644 --- a/examples/commands/src-tauri/src/commands.rs +++ b/examples/commands/src-tauri/src/commands.rs @@ -10,6 +10,12 @@ pub fn cmd(_argument: String) {} #[command] pub fn invoke(_argument: String) {} +#[command] +pub fn message(_argument: String) {} + +#[command] +pub fn resolver(_argument: String) {} + #[command] pub fn simple_command(argument: String) { println!("{}", argument); diff --git a/examples/commands/src-tauri/src/main.rs b/examples/commands/src-tauri/src/main.rs index 89dc4a74f5f..451582def1b 100644 --- a/examples/commands/src-tauri/src/main.rs +++ b/examples/commands/src-tauri/src/main.rs @@ -9,7 +9,7 @@ // we move some basic commands to a separate module just to show it works mod commands; -use commands::{cmd, invoke}; +use commands::{cmd, invoke, message, resolver}; use serde::Deserialize; use tauri::{command, Params, State, Window}; @@ -170,6 +170,8 @@ fn main() { commands::stateful_command, cmd, invoke, + message, + resolver, async_simple_command, future_simple_command, async_stateful_command,