From 5539fcc729f1e57cf7d6e18dea5743eec38dab48 Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Fri, 31 Jul 2026 00:55:43 +0800 Subject: [PATCH] added tauri specta --- Cargo.lock | 88 +++++++++++++++++++++++++++++++++++++++++++ src-server/Cargo.toml | 2 +- src-tauri/Cargo.toml | 5 ++- src-tauri/src/lib.rs | 15 +++++++- src/lib/bindings.ts | 81 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 src/lib/bindings.ts diff --git a/Cargo.lock b/Cargo.lock index 9e1faef..fb4068b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" + [[package]] name = "adler2" version = "2.0.1" @@ -2578,6 +2584,12 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pathdiff" version = "0.2.3" @@ -3394,6 +3406,50 @@ dependencies = [ "system-deps", ] +[[package]] +name = "specta" +version = "2.0.0-rc.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab7f01e9310a820edd31c80fde3cae445295adde21a3f9416517d7d65015b971" +dependencies = [ + "paste", + "specta-macros", + "thiserror 1.0.69", +] + +[[package]] +name = "specta-macros" +version = "2.0.0-rc.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0074b9e30ed84c6924eb63ad8d2fe71cdc82628525d84b1fcb1f2fd40676517" +dependencies = [ + "Inflector", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "specta-serde" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77216504061374659e7245eac53d30c7b3e5fe64b88da97c753e7184b0781e63" +dependencies = [ + "specta", + "thiserror 1.0.69", +] + +[[package]] +name = "specta-typescript" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3220a0c365e51e248ac98eab5a6a32f544ff6f961906f09d3ee10903a4f52b2d" +dependencies = [ + "specta", + "specta-serde", + "thiserror 1.0.69", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -3593,6 +3649,7 @@ dependencies = [ "serde_json", "serde_repr", "serialize-to-javascript", + "specta", "swift-rs", "tauri-build", "tauri-macros", @@ -3760,6 +3817,34 @@ dependencies = [ "wry", ] +[[package]] +name = "tauri-specta" +version = "2.0.0-rc.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b23c0132dd3cf6064e5cd919b82b3f47780e9280e7b5910babfe139829b76655" +dependencies = [ + "heck 0.5.0", + "serde", + "serde_json", + "specta", + "specta-typescript", + "tauri", + "tauri-specta-macros", + "thiserror 2.0.18", +] + +[[package]] +name = "tauri-specta-macros" +version = "2.0.0-rc.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a4aa93823e07859546aa796b8a5d608190cd8037a3a5dce3eb63d491c34bda8" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "tauri-utils" version = "2.9.2" @@ -5153,9 +5238,12 @@ dependencies = [ "objc2-foundation", "serde", "serde_json", + "specta", + "specta-typescript", "tauri", "tauri-build", "tauri-plugin-opener", + "tauri-specta", "tokio", "windows 0.58.0", ] diff --git a/src-server/Cargo.toml b/src-server/Cargo.toml index 4c2e39e..da8d0e5 100644 --- a/src-server/Cargo.toml +++ b/src-server/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "wyd-server" version = "0.1.0" -edition = "2021" +edition = "2024" publish = false [dependencies] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3c1fbfc..e7e21bd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -3,7 +3,7 @@ name = "wyd" version = "0.1.0" description = "A Tauri App" authors = ["you"] -edition = "2021" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -28,6 +28,9 @@ ed25519-dalek = "3.0.0" getrandom = "0.4.3" device_query = "4.0.1" tokio = "1.53.1" +tauri-specta = { version = "2.0.0-rc.21", features = ["derive", "typescript"] } +specta = "2.0.0-rc.21" +specta-typescript = { version = "0.0.9" } [target.'cfg(target_os = "windows")'.dependencies] windows = "0.58" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a8d8c72..7818866 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -9,9 +9,22 @@ fn launch_app(app: &tauri::App) { #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + let specta_builder = tauri_specta::Builder::::new() + .error_handling(tauri_specta::ErrorHandlingMode::Throw) + .commands(tauri_specta::collect_commands![]) + .events(tauri_specta::collect_events![]); + + #[cfg(debug_assertions)] + specta_builder + .export( + specta_typescript::Typescript::default(), + "../src/lib/bindings.ts", + ) + .expect("Failed to export TypeScript bindings"); + tauri::Builder::default() .plugin(tauri_plugin_opener::init()) - .invoke_handler(tauri::generate_handler![]) + .invoke_handler(specta_builder.invoke_handler()) .setup(|app| { launch_app(app); Ok(()) diff --git a/src/lib/bindings.ts b/src/lib/bindings.ts new file mode 100644 index 0000000..7d34a9c --- /dev/null +++ b/src/lib/bindings.ts @@ -0,0 +1,81 @@ + +// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually. + +/** user-defined commands **/ + + +export const commands = { + +} + +/** user-defined events **/ + + + +/** user-defined constants **/ + + + +/** user-defined types **/ + + + +/** tauri-specta globals **/ + +import { + invoke as TAURI_INVOKE, + Channel as TAURI_CHANNEL, +} from "@tauri-apps/api/core"; +import * as TAURI_API_EVENT from "@tauri-apps/api/event"; +import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow"; + +type __EventObj__ = { + listen: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + once: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + emit: null extends T + ? (payload?: T) => ReturnType + : (payload: T) => ReturnType; +}; + +export type Result = + | { status: "ok"; data: T } + | { status: "error"; error: E }; + +function __makeEvents__>( + mappings: Record, +) { + return new Proxy( + {} as unknown as { + [K in keyof T]: __EventObj__ & { + (handle: __WebviewWindow__): __EventObj__; + }; + }, + { + get: (_, event) => { + const name = mappings[event as keyof T]; + + return new Proxy((() => {}) as any, { + apply: (_, __, [window]: [__WebviewWindow__]) => ({ + listen: (arg: any) => window.listen(name, arg), + once: (arg: any) => window.once(name, arg), + emit: (arg: any) => window.emit(name, arg), + }), + get: (_, command: keyof __EventObj__) => { + switch (command) { + case "listen": + return (arg: any) => TAURI_API_EVENT.listen(name, arg); + case "once": + return (arg: any) => TAURI_API_EVENT.once(name, arg); + case "emit": + return (arg: any) => TAURI_API_EVENT.emit(name, arg); + } + }, + }); + }, + }, + ); +}