Skip to content

Commit

Permalink
fix(macros): fix rest of command collisons (#1805)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
chippers and lucasfernog committed May 12, 2021
1 parent 7862ec5 commit 0b87532
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .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`.
19 changes: 11 additions & 8 deletions core/tauri-macros/src/command/wrapper.rs
Expand Up @@ -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
}};
Expand All @@ -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<TokenStream2> {
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
Expand All @@ -111,7 +114,7 @@ fn body_async(function: &ItemFn) -> syn::Result<TokenStream2> {
///
/// 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
Expand All @@ -121,13 +124,13 @@ fn body_blocking(function: &ItemFn) -> syn::Result<TokenStream2> {
// 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__);
})
}

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

/// Transform a [`FnArg`] into a command argument.
///
/// * Requires binding `message`.
/// * Requires binding `__tauri_message__`.
fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
// we have no use for self arguments
let mut arg = match arg {
Expand Down Expand Up @@ -187,7 +190,7 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
::tauri::command::CommandItem {
name: stringify!(#command),
key: #key,
message: &message,
message: &__tauri_message__,
}
)))
}
Expand Down
6 changes: 6 additions & 0 deletions examples/commands/src-tauri/src/commands.rs
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion examples/commands/src-tauri/src/main.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -170,6 +170,8 @@ fn main() {
commands::stateful_command,
cmd,
invoke,
message,
resolver,
async_simple_command,
future_simple_command,
async_stateful_command,
Expand Down

0 comments on commit 0b87532

Please sign in to comment.