From 3c5d9e4312a156a27f94c8a031f9fa692d959bda Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Thu, 9 May 2024 10:56:43 +0800 Subject: [PATCH] :tada: Release 2.0.0-rc.9 with Specta 2.0.0-rc.12 --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- docs/v2.md | 4 ++-- examples/app/src-tauri/Cargo.toml | 2 +- examples/app/src-tauri/src/main.rs | 25 +++++++++++++++++++++++- examples/app/src/bindings.ts | 11 ++++++++++- examples/custom-plugin/plugin/Cargo.toml | 2 +- 7 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca5960f..8ed1ab9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2752,9 +2752,9 @@ dependencies = [ [[package]] name = "specta" -version = "2.0.0-rc.11" +version = "2.0.0-rc.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ed13baa3131e5b627ca78dca1097aa84dd83509b18ac9f876a0db5e70413636" +checksum = "3624a07cbde326fdf1ec37cbd39d06a224660fa0199b7db7316f2349583df981" dependencies = [ "once_cell", "paste", @@ -3144,7 +3144,7 @@ dependencies = [ [[package]] name = "tauri-specta" -version = "2.0.0-rc.8" +version = "2.0.0-rc.9" dependencies = [ "heck 0.5.0", "indoc", diff --git a/Cargo.toml b/Cargo.toml index 1968653..0f201dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tauri-specta" description = "Completely typesafe Tauri commands" -version = "2.0.0-rc.8" +version = "2.0.0-rc.9" authors = ["Oscar Beaumont "] edition = "2021" license = "MIT" @@ -23,7 +23,7 @@ typescript = ["specta/typescript", "specta/js_doc"] [dependencies] # Public -specta = { version = "=2.0.0-rc.11", features = ["function"] } +specta = { version = "=2.0.0-rc.12", features = ["function"] } tauri-specta-macros = { version = "=2.0.0-rc.5", path = "./macros" } serde = "1" serde_json = "1" diff --git a/docs/v2.md b/docs/v2.md index 929dfef..c5be014 100644 --- a/docs/v2.md +++ b/docs/v2.md @@ -9,7 +9,7 @@ ```bash cargo add tauri@=2.0.0-beta.17 -cargo add specta@=2.0.0-rc.11 +cargo add specta@=2.0.0-rc.12 cargo add tauri-specta@=2.0.0-rc.5 --features javascript,typescript ``` @@ -186,7 +186,7 @@ let builder = ts::builder() .types(TypeCollection::default().register::()); // < call `register` as much as you want. ``` -`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.11/specta/type/trait.NamedType.html) as otherwise you would run into the following problem: +`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.12/specta/type/trait.NamedType.html) as otherwise you would run into the following problem: ```rust // vvv - What would this be without a name? export ... = {}; diff --git a/examples/app/src-tauri/Cargo.toml b/examples/app/src-tauri/Cargo.toml index f18e6f2..86aed08 100644 --- a/examples/app/src-tauri/Cargo.toml +++ b/examples/app/src-tauri/Cargo.toml @@ -14,7 +14,7 @@ tauri-build = { version = "2.0.0-beta.13", features = [] } [dependencies] serde_json = "1.0" -specta = "=2.0.0-rc.11" +specta = "=2.0.0-rc.12" serde = { version = "1.0", features = ["derive"] } tauri = { version = "2.0.0-beta", features = [] } tauri-specta = { path = "../../../", features = ["typescript", "javascript"] } diff --git a/examples/app/src-tauri/src/main.rs b/examples/app/src-tauri/src/main.rs index 9f475a6..6a7ca74 100644 --- a/examples/app/src-tauri/src/main.rs +++ b/examples/app/src-tauri/src/main.rs @@ -56,6 +56,7 @@ mod nested { } #[derive(Error, Debug, Serialize, Type)] +#[serde(tag = "type", content = "data")] pub enum MyError { // On the frontend this variant will be "IoError" with no data. #[error("io error: {0}")] @@ -78,6 +79,27 @@ fn typesafe_errors_using_thiserror() -> Result<(), MyError> { ))) } +#[derive(Error, Debug, Serialize, Type)] +#[serde(tag = "type", content = "data")] +pub enum MyError2 { + #[error("io error: {0}")] + IoError(String), +} + +impl From for MyError2 { + fn from(error: std::io::Error) -> Self { + Self::IoError(error.to_string()) + } +} + +#[tauri::command] +#[specta::specta] +fn typesafe_errors_using_thiserror_with_value() -> Result<(), MyError2> { + // some_method()?; // This will work because `?` does `From` conversion. + + Err(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into()) // We use `into` here to do the `From` conversion. +} + #[derive(Serialize, Deserialize, Debug, Clone, specta::Type, tauri_specta::Event)] pub struct DemoEvent(String); @@ -97,7 +119,8 @@ fn main() { nested::some_struct, generic::, deprecated, - typesafe_errors_using_thiserror + typesafe_errors_using_thiserror, + typesafe_errors_using_thiserror_with_value ]) .events(tauri_specta::collect_events![DemoEvent, EmptyEvent]) .types(TypeCollection::default().register::()) diff --git a/examples/app/src/bindings.ts b/examples/app/src/bindings.ts index 045fbe6..12fd6b6 100644 --- a/examples/app/src/bindings.ts +++ b/examples/app/src/bindings.ts @@ -39,6 +39,14 @@ try { if(e instanceof Error) throw e; else return { status: "error", error: e as any }; } +}, +async typesafeErrorsUsingThiserrorWithValue() : Promise> { +try { + return { status: "ok", data: await TAURI_INVOKE("typesafe_errors_using_thiserror_with_value") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} } } @@ -55,7 +63,8 @@ emptyEvent: "empty-event" export type Custom = string export type DemoEvent = string export type EmptyEvent = null -export type MyError = "IoError" | { AnotherError: string } +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 **/ diff --git a/examples/custom-plugin/plugin/Cargo.toml b/examples/custom-plugin/plugin/Cargo.toml index 52b16e9..df21412 100644 --- a/examples/custom-plugin/plugin/Cargo.toml +++ b/examples/custom-plugin/plugin/Cargo.toml @@ -10,6 +10,6 @@ tauri-plugin = { version = "=2.0.0-beta.13", features = ["build"] } [dependencies] rand = "0.8.5" serde = "1" -specta = "=2.0.0-rc.11" +specta = "=2.0.0-rc.12" tauri = "=2.0.0-beta.17" tauri-specta = { path = "../../../", features = ["typescript"] }