Skip to content

Commit 7a4b1fb

Browse files
authored
feat(macros): add support to attributes on generate_handler (#6839)
1 parent 256c30c commit 7a4b1fb

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-macros": patch
3+
"tauri": patch
4+
---
5+
6+
Added support to attibutes for each command path in the `generate_handler` macro.

core/tauri-macros/src/command/handler.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@
44

55
use quote::format_ident;
66
use syn::{
7-
parse::{Parse, ParseBuffer},
8-
Ident, Path, Token,
7+
parse::{Parse, ParseBuffer, ParseStream},
8+
Attribute, Ident, Path, Token,
99
};
1010

11+
struct CommandDef {
12+
path: Path,
13+
attrs: Vec<Attribute>,
14+
}
15+
16+
impl Parse for CommandDef {
17+
fn parse(input: ParseStream) -> syn::Result<Self> {
18+
let attrs = input.call(Attribute::parse_outer)?;
19+
let path = input.parse()?;
20+
21+
Ok(CommandDef { path, attrs })
22+
}
23+
}
24+
1125
/// The items parsed from [`generate_handle!`](crate::generate_handle).
1226
pub struct Handler {
13-
paths: Vec<Path>,
27+
command_defs: Vec<CommandDef>,
1428
commands: Vec<Ident>,
1529
wrappers: Vec<Path>,
1630
}
1731

1832
impl Parse for Handler {
1933
fn parse(input: &ParseBuffer<'_>) -> syn::Result<Self> {
20-
let paths = input.parse_terminated::<Path, Token![,]>(Path::parse)?;
34+
let command_defs = input.parse_terminated::<CommandDef, Token![,]>(CommandDef::parse)?;
2135

2236
// parse the command names and wrappers from the passed paths
23-
let (commands, wrappers) = paths
37+
let (commands, wrappers) = command_defs
2438
.iter()
25-
.map(|path| {
26-
let mut wrapper = path.clone();
39+
.map(|command_def| {
40+
let mut wrapper = command_def.path.clone();
2741
let last = super::path_to_command(&mut wrapper);
2842

2943
// the name of the actual command function
@@ -37,7 +51,7 @@ impl Parse for Handler {
3751
.unzip();
3852

3953
Ok(Self {
40-
paths: paths.into_iter().collect(), // remove punctuation separators
54+
command_defs: command_defs.into_iter().collect(), // remove punctuation separators
4155
commands,
4256
wrappers,
4357
})
@@ -47,17 +61,21 @@ impl Parse for Handler {
4761
impl From<Handler> for proc_macro::TokenStream {
4862
fn from(
4963
Handler {
50-
paths,
64+
command_defs,
5165
commands,
5266
wrappers,
5367
}: Handler,
5468
) -> Self {
5569
let cmd = format_ident!("__tauri_cmd__");
5670
let invoke = format_ident!("__tauri_invoke__");
71+
let (paths, attrs): (Vec<Path>, Vec<Vec<Attribute>>) = command_defs
72+
.into_iter()
73+
.map(|def| (def.path, def.attrs))
74+
.unzip();
5775
quote::quote!(move |#invoke| {
5876
let #cmd = #invoke.message.command();
5977
match #cmd {
60-
#(stringify!(#commands) => #wrappers!(#paths, #invoke),)*
78+
#(#(#attrs)* stringify!(#commands) => #wrappers!(#paths, #invoke),)*
6179
_ => {
6280
return false;
6381
},

0 commit comments

Comments
 (0)