From 092a561ca69a631d2a03777e29debeba37b197a7 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 21 Sep 2023 15:55:42 +0300 Subject: [PATCH] refactor!: remove `tauri::api` module (#7874) * refactor!: remove `tauri::api` module ref: https://github.com/tauri-apps/tauri/issues/7756 * change file * fix builds --- .changes/tauri-api-removal.md | 5 +++++ core/tauri/src/api/error.rs | 15 --------------- core/tauri/src/api/mod.rs | 21 --------------------- core/tauri/src/api/os.rs | 10 ---------- core/tauri/src/error.rs | 3 --- core/tauri/src/ipc/format_callback.rs | 8 ++++---- core/tauri/src/ipc/protocol.rs | 2 +- core/tauri/src/lib.rs | 1 - examples/web/core/tauri/Cargo.toml | 3 ++- examples/web/core/tauri/src/main.rs | 5 ++++- 10 files changed, 16 insertions(+), 57 deletions(-) create mode 100644 .changes/tauri-api-removal.md delete mode 100644 core/tauri/src/api/error.rs delete mode 100644 core/tauri/src/api/mod.rs delete mode 100644 core/tauri/src/api/os.rs diff --git a/.changes/tauri-api-removal.md b/.changes/tauri-api-removal.md new file mode 100644 index 000000000000..b6c728d03faf --- /dev/null +++ b/.changes/tauri-api-removal.md @@ -0,0 +1,5 @@ +--- +'tauri': 'major:breaking' +--- + +Removed `tauri::api` module as most apis have been moved to either a plugin or we recommend using other crates. diff --git a/core/tauri/src/api/error.rs b/core/tauri/src/api/error.rs deleted file mode 100644 index fe9f3d1a4bfe..000000000000 --- a/core/tauri/src/api/error.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2019-2023 Tauri Programme within The Commons Conservancy -// SPDX-License-Identifier: Apache-2.0 -// SPDX-License-Identifier: MIT - -/// The result type of Tauri API module. -pub type Result = std::result::Result; - -/// The error type of Tauri API module. -#[derive(thiserror::Error, Debug)] -#[non_exhaustive] -pub enum Error { - /// JSON error. - #[error(transparent)] - Json(#[from] serde_json::Error), -} diff --git a/core/tauri/src/api/mod.rs b/core/tauri/src/api/mod.rs deleted file mode 100644 index 10742b7baeb6..000000000000 --- a/core/tauri/src/api/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2019-2023 Tauri Programme within The Commons Conservancy -// SPDX-License-Identifier: Apache-2.0 -// SPDX-License-Identifier: MIT - -//! The Tauri API interface. - -mod error; - -pub use error::{Error, Result}; -// Not public API -#[doc(hidden)] -pub mod private { - pub use once_cell::sync::OnceCell; - - pub trait AsTauriContext { - fn config() -> &'static crate::Config; - fn assets() -> &'static crate::utils::assets::EmbeddedAssets; - fn default_window_icon() -> Option<&'static [u8]>; - fn package_info() -> crate::PackageInfo; - } -} diff --git a/core/tauri/src/api/os.rs b/core/tauri/src/api/os.rs deleted file mode 100644 index c5ea565a8d0e..000000000000 --- a/core/tauri/src/api/os.rs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2019-2023 Tauri Programme within The Commons Conservancy -// SPDX-License-Identifier: Apache-2.0 -// SPDX-License-Identifier: MIT - -//! Types and functions related to operating system operations. - -/// Returns `Some(String)` with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `None` is returned instead. -pub fn locale() -> Option { - sys_locale::get_locale() -} diff --git a/core/tauri/src/error.rs b/core/tauri/src/error.rs index 8de7cec0d01b..97b41f03b0ed 100644 --- a/core/tauri/src/error.rs +++ b/core/tauri/src/error.rs @@ -43,9 +43,6 @@ pub enum Error { /// Failed to serialize/deserialize. #[error("JSON error: {0}")] Json(#[from] serde_json::Error), - /// Failed to execute tauri API. - #[error("failed to execute API: {0}")] - FailedToExecuteApi(#[from] crate::api::Error), /// IO error. #[error("{0}")] Io(#[from] std::io::Error), diff --git a/core/tauri/src/ipc/format_callback.rs b/core/tauri/src/ipc/format_callback.rs index 31d7cbc599d1..ea118c74ba6b 100644 --- a/core/tauri/src/ipc/format_callback.rs +++ b/core/tauri/src/ipc/format_callback.rs @@ -44,7 +44,7 @@ fn serialize_js_with String>( value: &T, options: serialize_to_javascript::Options, cb: F, -) -> crate::api::Result { +) -> crate::Result { // get a raw &str representation of a serialized json value. let string = serde_json::to_string(value)?; let raw = RawValue::from_string(string)?; @@ -83,7 +83,7 @@ fn serialize_js_with String>( /// but will serialize arrays and objects whose serialized JSON string is smaller than 1 GB and larger /// than 10 KiB with `JSON.parse('...')`. /// See [json-parse-benchmark](https://github.com/GoogleChromeLabs/json-parse-benchmark). -pub fn format(function_name: CallbackFn, arg: &T) -> crate::api::Result { +pub fn format(function_name: CallbackFn, arg: &T) -> crate::Result { serialize_js_with(arg, Default::default(), |arg| { format!( r#" @@ -111,7 +111,7 @@ pub fn format_result( result: Result, success_callback: CallbackFn, error_callback: CallbackFn, -) -> crate::api::Result { +) -> crate::Result { match result { Ok(res) => format(success_callback, &res), Err(err) => format(error_callback, &err), @@ -130,7 +130,7 @@ mod test { } } - fn serialize_js(value: &T) -> crate::api::Result { + fn serialize_js(value: &T) -> crate::Result { serialize_js_with(value, Default::default(), |v| v.into()) } diff --git a/core/tauri/src/ipc/protocol.rs b/core/tauri/src/ipc/protocol.rs index 181e8c97d5c2..9e25c1dd1de3 100644 --- a/core/tauri/src/ipc/protocol.rs +++ b/core/tauri/src/ipc/protocol.rs @@ -228,7 +228,7 @@ fn handle_ipc_message(message: String, manager: &WindowManager, l { fn responder_eval( window: &crate::Window, - js: crate::api::Result, + js: crate::Result, error: CallbackFn, ) { let eval_js = match js { diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index eabcd02d1e22..3b5b2e2e1f2d 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -74,7 +74,6 @@ pub use swift_rs; pub use tauri_macros::mobile_entry_point; pub use tauri_macros::{command, generate_handler}; -pub mod api; pub(crate) mod app; pub mod async_runtime; pub mod command; diff --git a/examples/web/core/tauri/Cargo.toml b/examples/web/core/tauri/Cargo.toml index 2f9cf59782c1..ebf0e357cd57 100644 --- a/examples/web/core/tauri/Cargo.toml +++ b/examples/web/core/tauri/Cargo.toml @@ -15,6 +15,7 @@ tauri-build = { path = "../../../../core/tauri-build", features = [] } [dependencies] api = { path = "../api" } tauri = { path = "../../../../core/tauri" } +tauri-plugin-dialog = "2.0.0-alpha.2" [features] -custom-protocol = [ "tauri/custom-protocol" ] +custom-protocol = ["tauri/custom-protocol"] diff --git a/examples/web/core/tauri/src/main.rs b/examples/web/core/tauri/src/main.rs index 5ec45c597eed..76755fda04da 100644 --- a/examples/web/core/tauri/src/main.rs +++ b/examples/web/core/tauri/src/main.rs @@ -3,10 +3,13 @@ // SPDX-License-Identifier: MIT #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use tauri_plugin_dialog::DialogExt; #[tauri::command] fn greet(window: tauri::Window, name: String) { - tauri::api::dialog::message(Some(&window), "Tauri Example", api::greet(&name)); + MessageDialogBuilder::new(window.dialog(), "Tauri Example") + .parent(&window) + .show(); } fn main() {