feat: 引入tauri-specta来保证类型安全

This commit is contained in:
lanyeeee
2025-07-09 05:28:01 +08:00
parent 81a98dc54a
commit 2b4b7b6862
5 changed files with 196 additions and 3 deletions
+19 -1
View File
@@ -1,14 +1,32 @@
use tauri::Wry;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
#[specta::specta]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let builder = tauri_specta::Builder::<Wry>::new()
.commands(tauri_specta::collect_commands![greet])
.events(tauri_specta::collect_events![]);
#[cfg(debug_assertions)]
builder
.export(
specta_typescript::Typescript::default()
.bigint(specta_typescript::BigIntExportBehavior::Number)
.formatter(specta_typescript::formatter::prettier)
.header("// @ts-nocheck"), // disable typescript checks
"../src/bindings.ts",
)
.expect("Failed to export typescript bindings");
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(builder.invoke_handler())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}