Skip to content

Commit

Permalink
fix collect_commands macro
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Aug 1, 2024
1 parent 8cf9659 commit d3741d7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ fn main() {
hello_world,
goodbye_world,
has_error,
// nested::some_struct,
// generic::<tauri::Wry>,
nested::some_struct,
generic::<tauri::Wry>,
deprecated,
typesafe_errors_using_thiserror,
typesafe_errors_using_thiserror_with_value
Expand Down
30 changes: 23 additions & 7 deletions examples/app/src/bindings-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/** user-defined commands **/

export const commands = {
/**
/**
* HELLO
* WORLD
* !!!!
Expand All @@ -31,6 +31,18 @@ async hasError() {
else return { status: "error", error: e };
}
},
/**
* @returns { Promise<MyStruct> }
*/
async someStruct() {
return await TAURI_INVOKE("some_struct");
},
/**
* @returns { Promise<void> }
*/
async generic() {
await TAURI_INVOKE("generic");
},
/**
* @deprecated This is a deprecated function
* @returns { Promise<void> }
Expand Down Expand Up @@ -60,24 +72,24 @@ async typesafeErrorsUsingThiserrorWithValue() {
else return { status: "error", error: e };
}
}
}
}

/** user-defined events **/


/**
/**
* @type {typeof __makeEvents__<{
* demoEvent: DemoEvent
* emptyEvent: EmptyEvent
* }>}
*/

const __typedMakeEvents__ = __makeEvents__;
const __typedMakeEvents__ = __makeEvents__;

export const events = __typedMakeEvents__({
demoEvent: "demo-event",
export const events = __typedMakeEvents__({
demoEvent: "demo-event",
emptyEvent: "empty-event"
})
})

/** user-defined constants **/

Expand Down Expand Up @@ -105,6 +117,10 @@ export const universalConstant = 42;
* @typedef { { type: "IoError"; data: string } } MyError2
*/

/**
* @typedef { { some_field: string } } MyStruct
*/


/** tauri-specta globals **/

Expand Down
7 changes: 7 additions & 0 deletions examples/app/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ async hasError() : Promise<Result<string, number>> {
else return { status: "error", error: e as any };
}
},
async someStruct() : Promise<MyStruct> {
return await TAURI_INVOKE("some_struct");
},
async generic() : Promise<void> {
await TAURI_INVOKE("generic");
},
/**
* @deprecated This is a deprecated function
*/
Expand Down Expand Up @@ -70,6 +76,7 @@ export type DemoEvent = string
export type EmptyEvent = null
export type MyError = { type: "IoError" } | { type: "AnotherError"; data: string }
export type MyError2 = { type: "IoError"; data: string }
export type MyStruct = { some_field: string }

/** tauri-specta globals **/

Expand Down
2 changes: 1 addition & 1 deletion src/lang/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn render_commands(ts: &Typescript, cfg: &ExportContext) -> Result<String, Expor
&js_ts::command_body(&cfg.plugin_name, function, false),
))
})
.collect::<Result<Vec<_>, _>>()?
.collect::<Result<Vec<_>, ExportError>>()?
.join(",\n");

Ok(format!(
Expand Down
59 changes: 35 additions & 24 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,51 @@
/// collect_commands![hello_world, some::path::function, generic_fn::<String>];
/// ```
///
// TODO: Hide it's implementation details from the generated rustdoc.
#[macro_export]
macro_rules! collect_commands {
() => {
$crate::internal::command(::tauri::generate_handler![], ::specta::function::collect_functions![])
};
($i:ident) => {
$crate::internal::command(::tauri::generate_handler![$i], ::specta::function::collect_functions![$i])
// Hide distracting implementation details from the generated rustdoc.
($($t:tt)*) => {
$crate::collect_commands_internal!([] [] $($t)*)
};
($i:ident, $($rest:tt)*) => {
$crate::internal::command($crate::collect_commands!(@internal; $i; $($rest)*), ::specta::function::collect_functions![$i, $($rest)*])
}

#[doc(hidden)]
#[macro_export]
macro_rules! collect_commands_internal {
() => {
$crate::internal::command(
::tauri::generate_handler![],
::specta::function::collect_functions![],
)
};
($i:ident::<$g:path>) => {
$crate::internal::command(::tauri::generate_handler![$i], ::specta::function::collect_functions![$i<$g>])
// Alternate parsing mode between `<` and `>` where all chars are not put into `stripped` accumulator
([$($stripped:tt)*] [$($raw:tt)*] []) => {
compile_error!("Unexpected end of input. Did you forget to close a generic argument?");
};
($i:ident::<$g:path>, $($rest:tt)*) => {
$crate::internal::command($crate::collect_commands!(@internal; $i; $($rest)*), ::specta::function::collect_functions![$i<$g>, $($rest)*])
([$($stripped:tt)*] [$($raw:tt)*] [] > $($rest:tt)*) => {
// Switch back to regular parsing mode
$crate::collect_commands_internal!([$($stripped)*] [$($raw)* >] $($rest)*)
};
//
(@internal; $($b:path),*;) => {
::tauri::generate_handler![$($b),*]
([$($stripped:tt)*] [$($raw:tt)*] [] $a:tt $($rest:tt)*) => {
$crate::collect_commands_internal!([$($stripped)*] [$($raw)* $a] [] $($rest)*)
};
(@internal; $($b:path),*; $i:ident) => {
::tauri::generate_handler![$($b),*, $i]
// Regular parsing mode
([$($stripped:tt)*] [$($raw:tt)*]) => {
$crate::internal::command(
::tauri::generate_handler![$($stripped)*],
::specta::function::collect_functions![$($raw)*],
)
};
(@internal; $($b:path),*; $i:ident, $($rest:tt)*) => {
$crate::collect_commands!(@internal; $($b),*, $i; $($rest)*)
([$($stripped:tt)*] [$($raw:tt)*] ::< $($rest:tt)*) => {
// Switch to alternate parsing mode
$crate::collect_commands_internal!([$($stripped)*] [$($raw)* ::<] [] $($rest)*)
};
(@internal; $($b:path),*; $i:ident::<$g:path>) => {
::tauri::generate_handler![$($b),*, $i]
([$($stripped:tt)*] [$($raw:tt)*] $a:tt $($rest:tt)*) => {
$crate::collect_commands_internal!([$($stripped)* $a] [$($raw)* $a] $($rest)*)
};
(@internal; $($b:path),*; $i:ident::<$g:ident>, $($rest:tt)*) => {
$crate::collect_commands!(@internal; $($b),*, $i; $($rest)*)
// Input
($($rest:tt)*) => {
$crate::collect_commands_internal!([] [] $($rest)*);
};
}

Expand All @@ -58,7 +70,6 @@ macro_rules! collect_commands {
/// collect_events![MyEvent, module::MyOtherEvent];
/// ```
///
// TODO: Hide it's implementation details from the generated rustdoc.
#[macro_export]
macro_rules! collect_events {
($($event:path),* $(,)?) => {{
Expand Down

0 comments on commit d3741d7

Please sign in to comment.