Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make collect_commands a proc macro to support generic commands #59

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ fn has_error() -> Result<&'static str, i32> {
Err(32)
}

#[tauri::command]
#[specta::specta]
fn generic<T: tauri::Runtime>(app: tauri::AppHandle<T>) {}

mod nested {
use super::*;

Expand Down Expand Up @@ -57,7 +61,8 @@ fn main() {
hello_world,
goodbye_world,
has_error,
nested::some_struct
nested::some_struct,
generic::<tauri::Wry>
])
.events(tauri_specta::collect_events![DemoEvent, EmptyEvent])
.config(specta::ts::ExportConfig::default().formatter(specta::ts::prettier));
Expand Down
66 changes: 66 additions & 0 deletions macros/src/collect_commands.rs
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

View workflow job for this annotation

GitHub Actions / clippy

unused import: `TokenStream`

warning: unused import: `TokenStream` --> macros/src/collect_commands.rs:1:26 | 1 | use proc_macro2::{Ident, TokenStream}; | ^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use quote::quote;
use syn::{
bracketed,

Check warning on line 4 in macros/src/collect_commands.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `ItemStruct`, `bracketed`

warning: unused imports: `ItemStruct`, `bracketed` --> macros/src/collect_commands.rs:4:5 | 4 | bracketed, | ^^^^^^^^^ ... 8 | ItemStruct, Path, Token, | ^^^^^^^^^^
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()
}
7 changes: 7 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod collect_commands;

use heck::ToKebabCase;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
Expand All @@ -17,3 +19,8 @@ pub fn derive_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
}
.into()
}

#[proc_macro]
pub fn collect_commands(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
collect_commands::proc_macro(input)
}
14 changes: 1 addition & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,34 @@
//! // this example exports your types on startup when in debug mode or in a unit test. You can do whatever.
//! fn main() {
//! #[cfg(debug_assertions)]
//! ts::builder()

Check warning on line 71 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:71:4 | 71 | //! ts::builder() | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments note: the lint level is defined here --> src/lib.rs:111:9 | 111 | #![warn(clippy::all, clippy::unwrap_used, clippy::panic | ^^^^^^^^^^^ = note: `#[warn(clippy::tabs_in_doc_comments)]` implied by `#[warn(clippy::all)]`
//! .commands(collect_commands![greet, greet2, greet3])

Check warning on line 72 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:72:4 | 72 | //! .commands(collect_commands![greet, greet2, greet3]) | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .path("../src/bindings.ts")

Check warning on line 73 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:73:4 | 73 | //! .path("../src/bindings.ts") | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .export()

Check warning on line 74 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:74:4 | 74 | //! .export() | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .unwrap();

Check warning on line 75 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:75:4 | 75 | //! .unwrap(); | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//!
//! // or export to JS with JSDoc
//! #[cfg(debug_assertions)]
//! js::builder()

Check warning on line 79 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:79:4 | 79 | //! js::builder() | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .commands(collect_commands![greet, greet2, greet3])

Check warning on line 80 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:80:4 | 80 | //! .commands(collect_commands![greet, greet2, greet3]) | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .path("../src/bindings.js")

Check warning on line 81 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:81:4 | 81 | //! .path("../src/bindings.js") | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .export()

Check warning on line 82 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:82:4 | 82 | //! .export() | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .unwrap();

Check warning on line 83 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:83:4 | 83 | //! .unwrap(); | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! }
//!
//! #[test]
//! fn export_bindings() {
//! ts::builder()

Check warning on line 88 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:88:4 | 88 | //! ts::builder() | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .commands(collect_commands![greet, greet2, greet3])

Check warning on line 89 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:89:4 | 89 | //! .commands(collect_commands![greet, greet2, greet3]) | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .path("../src/bindings.ts")

Check warning on line 90 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:90:4 | 90 | //! .path("../src/bindings.ts") | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .export()

Check warning on line 91 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:91:4 | 91 | //! .export() | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .unwrap();

Check warning on line 92 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:92:4 | 92 | //! .unwrap(); | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//!
//! js::builder()

Check warning on line 94 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:94:4 | 94 | //! js::builder() | ^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .commands(collect_commands![greet, greet2, greet3])

Check warning on line 95 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:95:4 | 95 | //! .commands(collect_commands![greet, greet2, greet3]) | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .path("../src/bindings.js")

Check warning on line 96 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:96:4 | 96 | //! .path("../src/bindings.js") | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .export()

Check warning on line 97 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:97:4 | 97 | //! .export() | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! .unwrap();

Check warning on line 98 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using tabs in doc comments is not recommended

warning: using tabs in doc comments is not recommended --> src/lib.rs:98:4 | 98 | //! .unwrap(); | ^^^^^^^^^^^^ help: consider using four spaces per tab | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
//! }
//! ```
//!
Expand Down Expand Up @@ -150,19 +150,7 @@
pub type CollectCommandsTuple<TInvokeHandler> =
(specta::functions::CollectFunctionsResult, TInvokeHandler);

#[macro_export]
macro_rules! collect_commands {
(type_map: $type_map:ident, $($command:path),*) => {
(
specta::collect_functions![$type_map; $($command),*],
::tauri::generate_handler![$($command),*],
)
};
($($command:path),*) => {{
let mut type_map = specta::TypeMap::default();
$crate::collect_commands![type_map: type_map, $($command),*]
}};
}
pub use tauri_specta_macros::collect_commands;

// TODO
// #[cfg(doctest)]
Expand Down
Loading