-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make collect_commands a proc macro to support generic commands
- Loading branch information
1 parent
5ae9eef
commit 9574da6
Showing
4 changed files
with
80 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use proc_macro2::{Ident, TokenStream}; | ||
Check warning on line 1 in macros/src/collect_commands.rs GitHub Actions / clippyunused import: `TokenStream`
|
||
use quote::quote; | ||
use syn::{ | ||
bracketed, | ||
Check warning on line 4 in macros/src/collect_commands.rs GitHub Actions / clippyunused imports: `ItemStruct`, `bracketed`
|
||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
punctuated::Punctuated, | ||
ItemStruct, Path, Token, | ||
}; | ||
|
||
pub struct Input { | ||
type_map: Option<Ident>, | ||
paths: Punctuated<Path, Token![,]>, | ||
} | ||
|
||
mod kw { | ||
syn::custom_keyword!(type_map); | ||
} | ||
|
||
impl Parse for Input { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
Ok(Self { | ||
type_map: { | ||
if input.peek(kw::type_map) && input.peek2(Token![:]) { | ||
input.parse::<kw::type_map>()?; | ||
input.parse::<Token![:]>()?; | ||
Some(input.parse()?) | ||
} else { | ||
None | ||
} | ||
}, | ||
paths: Punctuated::parse_terminated(input)?, | ||
}) | ||
} | ||
} | ||
|
||
pub fn proc_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let Input { type_map, paths } = parse_macro_input!(input as Input); | ||
|
||
let tauri_paths = paths.iter().map(|p| { | ||
let Path { | ||
leading_colon, | ||
segments, | ||
} = p; | ||
|
||
let segments = segments.iter().map(|s| &s.ident); | ||
|
||
quote!(#leading_colon #(#segments)::*) | ||
}); | ||
|
||
let type_map = type_map | ||
.map(|i| quote!(#i)) | ||
.unwrap_or_else(|| quote!(::specta::TypeMap::new())); | ||
|
||
let body = quote! {( | ||
::specta::collect_functions![type_map; #paths], | ||
::tauri::generate_handler![#(#tauri_paths),*], | ||
)}; | ||
|
||
quote! {{ | ||
let mut type_map = #type_map; | ||
|
||
#body | ||
}} | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters