Skip to content

Commit ec9eecd

Browse files
committed
reuse Commands type and collect_commands! macro
1 parent d0ebcf1 commit ec9eecd

7 files changed

Lines changed: 36 additions & 168 deletions

File tree

examples/tanstack-query/src-tauri/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ fn delete_todo(state: tauri::State<AppStateMutex>, id: u32) -> Result<(), ApiErr
147147

148148
fn main() {
149149
let builder = Builder::<tauri::Wry>::new()
150-
.queries(tauri_specta::collect_queries![
150+
.queries(tauri_specta::collect_commands![
151151
get_user, list_users, list_todos,
152152
])
153-
.mutations(tauri_specta::collect_mutations![
153+
.mutations(tauri_specta::collect_commands![
154154
create_user,
155155
create_todo,
156156
delete_user,

src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::{
66
};
77

88
use crate::{
9-
Commands, EventRegistry, Events, LanguageExt, Mutations, Queries, TanstackFramework,
9+
Commands, EventRegistry, Events, LanguageExt, TanstackFramework,
10+
commands::{Mutations, Queries},
1011
event::EventRegistryMeta,
1112
};
1213
use serde::Serialize;

src/commands.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub struct Commands<R: Runtime>(
1212
pub(crate) fn(&mut Types) -> Vec<datatype::Function>,
1313
);
1414

15+
/// A tanstack query type alias for `Commands`.
16+
pub type Queries<R> = Commands<R>;
17+
18+
/// A tanstack mutation type alias for `Commands`.
19+
pub type Mutations<R> = Commands<R>;
20+
1521
impl<R: Runtime> fmt::Debug for Commands<R> {
1622
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1723
f.debug_struct("Commands").finish()

src/lib.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ mod commands;
186186
mod event;
187187
mod lang;
188188
mod macros;
189-
mod queries;
189+
mod tanstack;
190190

191191
pub use builder::{Builder, BuilderConfiguration, ErrorHandlingMode};
192192
pub use commands::Commands;
193193
pub use event::{Event, Events, TypedEvent};
194194
pub use lang::LanguageExt;
195-
pub use queries::{Mutations, Queries, TanstackFramework};
195+
pub use tanstack::TanstackFramework;
196196

197197
/// Implements the [`Event`](trait@crate::Event) trait for a struct.
198198
///
@@ -231,28 +231,6 @@ pub mod internal {
231231
Commands(Arc::new(f), types)
232232
}
233233

234-
/// called by `collect_queries` to construct `Queries`
235-
pub fn query<R: Runtime, F>(
236-
f: F,
237-
types: fn(&mut Types) -> Vec<datatype::Function>,
238-
) -> Queries<R>
239-
where
240-
F: Fn(Invoke<R>) -> bool + Send + Sync + 'static,
241-
{
242-
Queries(Arc::new(f), types)
243-
}
244-
245-
/// called by `collect_mutations` to construct `Mutations`
246-
pub fn mutation<R: Runtime, F>(
247-
f: F,
248-
types: fn(&mut Types) -> Vec<datatype::Function>,
249-
) -> Mutations<R>
250-
where
251-
F: Fn(Invoke<R>) -> bool + Send + Sync + 'static,
252-
{
253-
Mutations(Arc::new(f), types)
254-
}
255-
256234
/// called by `collect_events` to register events to an `Events`
257235
#[allow(clippy::panic)]
258236
pub fn register_event<E: Event>(Events(events): &mut Events) {

src/macros.rs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,58 +49,6 @@ macro_rules! collect_commands {
4949
};
5050
}
5151

52-
/// Collect queries for TanStack Query integration.
53-
///
54-
/// Like [`collect_commands!`] but marks functions as queries, generating
55-
/// `queryOptions` wrappers in TypeScript output.
56-
///
57-
/// # Usage
58-
/// ```rust,ignore
59-
/// use tauri_specta::{collect_queries, Builder};
60-
///
61-
/// #[tauri::command]
62-
/// #[specta::specta]
63-
/// fn get_user(id: u32) -> User { /* ... */ }
64-
///
65-
/// let builder = Builder::<tauri::Wry>::new()
66-
/// .queries(collect_queries![get_user]);
67-
/// ```
68-
#[macro_export]
69-
macro_rules! collect_queries {
70-
($($b:ident $(:: $($p:ident)? $(<$($g:path),*>)? )* ),* $(,)?) => {
71-
$crate::internal::query(
72-
::tauri::generate_handler![$($b $($(::$p)? )* ),*],
73-
::specta::function::collect_functions![$($b $($(::$p)? $(::<$($g),*>)? )* ),*],
74-
)
75-
};
76-
}
77-
78-
/// Collect mutations for TanStack Query integration.
79-
///
80-
/// Like [`collect_commands!`] but marks functions as mutations, generating
81-
/// `mutationOptions` wrappers in TypeScript output.
82-
///
83-
/// # Usage
84-
/// ```rust,ignore
85-
/// use tauri_specta::{collect_mutations, Builder};
86-
///
87-
/// #[tauri::command]
88-
/// #[specta::specta]
89-
/// fn create_user(name: String) -> User { /* ... */ }
90-
///
91-
/// let builder = Builder::<tauri::Wry>::new()
92-
/// .mutations(collect_mutations![create_user]);
93-
/// ```
94-
#[macro_export]
95-
macro_rules! collect_mutations {
96-
($($b:ident $(:: $($p:ident)? $(<$($g:path),*>)? )* ),* $(,)?) => {
97-
$crate::internal::mutation(
98-
::tauri::generate_handler![$($b $($(::$p)? )* ),*],
99-
::specta::function::collect_functions![$($b $($(::$p)? $(::<$($g),*>)? )* ),*],
100-
)
101-
};
102-
}
103-
10452
/// Collect events and their types.
10553
///
10654
/// This returns a [`Events`](crate::Events) struct that can be passed to [`Builder::events`](crate::Builder::events).

src/queries.rs

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/tanstack.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// Which TanStack Query framework to target for imports.
2+
#[derive(Debug, Clone, Copy)]
3+
pub enum TanstackFramework {
4+
/// `@tanstack/react-query`
5+
React,
6+
/// `@tanstack/solid-query`
7+
Solid,
8+
/// `@tanstack/vue-query`
9+
Vue,
10+
/// `@tanstack/svelte-query`
11+
Svelte,
12+
}
13+
14+
impl TanstackFramework {
15+
/// Returns the npm package name for this framework.
16+
pub fn package_name(&self) -> &'static str {
17+
match self {
18+
Self::React => "@tanstack/react-query",
19+
Self::Solid => "@tanstack/solid-query",
20+
Self::Vue => "@tanstack/vue-query",
21+
Self::Svelte => "@tanstack/svelte-query",
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)