From 5336dde8b45b7c100007243b3726980b4b1e6d8e Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Sat, 14 Dec 2024 01:13:39 -0500 Subject: [PATCH] ENH make all Tauri commands async (#218) * ENH make all Tauri commands async * make api commands async as well --------- Co-authored-by: Xinyu Li <78083150+Xinyu-Li-123@users.noreply.github.com> --- src-tauri/Cargo.lock | 395 ++++++++++++++++++++++++++++++++- src-tauri/Cargo.toml | 1 + src-tauri/src/apis/fs/apis.rs | 89 +++++--- src-tauri/src/apis/sys/apis.rs | 2 +- src-tauri/src/commands.rs | 39 ++-- 5 files changed, 464 insertions(+), 62 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 1842f97e..c3f15132 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -126,6 +126,159 @@ dependencies = [ "syn 2.0.55", ] +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "async-channel" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" +dependencies = [ + "concurrent-queue", + "event-listener 2.5.3", + "futures-core", +] + +[[package]] +name = "async-channel" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-executor" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7ebdfa2ebdab6b1760375fa7d6f382b9f486eac35fc994625a00e89280bdbb7" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand 2.0.2", + "futures-lite 2.3.0", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b1b633a2115cd122d73b955eadd9916c18c8f510ec9cd1686404c60ad1c29c" +dependencies = [ + "async-channel 2.3.1", + "async-executor", + "async-io 2.3.4", + "async-lock 3.4.0", + "blocking", + "futures-lite 2.3.0", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" +dependencies = [ + "async-lock 2.8.0", + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-lite 1.13.0", + "log", + "parking", + "polling 2.8.0", + "rustix 0.37.27", + "slab", + "socket2 0.4.10", + "waker-fn", +] + +[[package]] +name = "async-io" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "444b0228950ee6501b3568d3c93bf1176a1fdbc3b758dcd9475046d30f4dc7e8" +dependencies = [ + "async-lock 3.4.0", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite 2.3.0", + "parking", + "polling 3.7.3", + "rustix 0.38.32", + "slab", + "tracing", + "windows-sys 0.59.0", +] + +[[package]] +name = "async-lock" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" +dependencies = [ + "event-listener 2.5.3", +] + +[[package]] +name = "async-lock" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff6e472cdea888a4bd64f342f09b3f50e1886d32afe8df3d663c01140b811b18" +dependencies = [ + "event-listener 5.3.1", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-std" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" +dependencies = [ + "async-attributes", + "async-channel 1.9.0", + "async-global-executor", + "async-io 1.13.0", + "async-lock 2.8.0", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite 1.13.0", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "atk" version = "0.18.0" @@ -149,6 +302,12 @@ dependencies = [ "system-deps", ] +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "auto_impl" version = "1.2.0" @@ -268,6 +427,19 @@ dependencies = [ "objc2", ] +[[package]] +name = "blocking" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea" +dependencies = [ + "async-channel 2.3.1", + "async-task", + "futures-io", + "futures-lite 2.3.0", + "piper", +] + [[package]] name = "brotli" version = "6.0.0" @@ -581,6 +753,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -913,6 +1094,7 @@ name = "deskulpt" version = "0.0.0" dependencies = [ "anyhow", + "async-std", "cargo-llvm-cov", "copy_dir", "objc", @@ -1129,6 +1311,33 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0474425d51df81997e2f90a21591180b38eccf27292d755f3e30750225c175b" +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + +[[package]] +name = "event-listener" +version = "5.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" +dependencies = [ + "event-listener 5.3.1", + "pin-project-lite", +] + [[package]] name = "exr" version = "1.72.0" @@ -1145,6 +1354,15 @@ dependencies = [ "zune-inflate", ] +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + [[package]] name = "fastrand" version = "2.0.2" @@ -1342,6 +1560,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +[[package]] +name = "futures-lite" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" +dependencies = [ + "fastrand 1.9.0", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + +[[package]] +name = "futures-lite" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" +dependencies = [ + "fastrand 2.0.2", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + [[package]] name = "futures-macro" version = "0.3.30" @@ -1683,6 +1929,18 @@ dependencies = [ "x11-dl", ] +[[package]] +name = "gloo-timers" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "gobject-sys" version = "0.18.0" @@ -1790,6 +2048,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "hex" version = "0.4.3" @@ -1905,7 +2169,7 @@ dependencies = [ "http-body", "hyper", "pin-project-lite", - "socket2", + "socket2 0.5.6", "tokio", "tower", "tower-service", @@ -2038,6 +2302,17 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -2206,6 +2481,15 @@ dependencies = [ "selectors", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -2293,6 +2577,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd1bc4d24ad230d21fb898d1116b1801d7adfc449d42026475862ab48b11e70e" +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -2314,6 +2604,9 @@ name = "log" version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +dependencies = [ + "value-bag", +] [[package]] name = "loom" @@ -2571,7 +2864,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] @@ -2819,6 +3112,12 @@ dependencies = [ "system-deps", ] +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + [[package]] name = "parking_lot" version = "0.12.3" @@ -3036,6 +3335,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066" +dependencies = [ + "atomic-waker", + "fastrand 2.0.2", + "futures-io", +] + [[package]] name = "pkg-config" version = "0.3.30" @@ -3069,6 +3379,37 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "polling" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +dependencies = [ + "autocfg", + "bitflags 1.3.2", + "cfg-if", + "concurrent-queue", + "libc", + "log", + "pin-project-lite", + "windows-sys 0.48.0", +] + +[[package]] +name = "polling" +version = "3.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc2790cd301dec6cd3b7a025e4815cf825724a51c98dccfe6a3e55f05ffb6511" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi 0.4.0", + "pin-project-lite", + "rustix 0.38.32", + "tracing", + "windows-sys 0.59.0", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -3515,6 +3856,20 @@ dependencies = [ "semver 1.0.22", ] +[[package]] +name = "rustix" +version = "0.37.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2" +dependencies = [ + "bitflags 1.3.2", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", +] + [[package]] name = "rustix" version = "0.38.32" @@ -3524,7 +3879,7 @@ dependencies = [ "bitflags 2.5.0", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.13", "windows-sys 0.52.0", ] @@ -3903,6 +4258,16 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.6" @@ -4958,9 +5323,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" dependencies = [ "cfg-if", - "fastrand", + "fastrand 2.0.2", "once_cell", - "rustix", + "rustix 0.38.32", "windows-sys 0.59.0", ] @@ -5137,7 +5502,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", - "socket2", + "socket2 0.5.6", "windows-sys 0.48.0", ] @@ -5518,6 +5883,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "value-bag" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a84c137d37ab0142f0f2ddfe332651fdbf252e7b7dbb4e67b6c1f1b2e925101" + [[package]] name = "vergen" version = "9.0.0" @@ -5577,6 +5948,12 @@ dependencies = [ "libc", ] +[[package]] +name = "waker-fn" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" + [[package]] name = "walkdir" version = "2.5.0" @@ -6301,7 +6678,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12" dependencies = [ "gethostname", - "rustix", + "rustix 0.38.32", "x11rb-protocol", ] @@ -6318,8 +6695,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da84f1a25939b27f6820d92aed108f83ff920fdf11a7b19366c27c4cda81d4f" dependencies = [ "libc", - "linux-raw-sys", - "rustix", + "linux-raw-sys 0.4.13", + "rustix 0.38.32", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 510e741e..a236fb1f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" tauri-build = { version = "2.0.0-beta.13", features = ["codegen"] } [dev-dependencies] +async-std = { version = "1.12.0", features = ["attributes"] } cargo-llvm-cov = "0.6.13" copy_dir = "0.1.3" pretty_assertions = "1.4.1" diff --git a/src-tauri/src/apis/fs/apis.rs b/src-tauri/src/apis/fs/apis.rs index fecd5970..246cdf07 100644 --- a/src-tauri/src/apis/fs/apis.rs +++ b/src-tauri/src/apis/fs/apis.rs @@ -7,7 +7,7 @@ use std::io::Write; use tauri::{command, AppHandle, Runtime}; #[command] -pub(crate) fn exists( +pub(crate) async fn exists( app_handle: AppHandle, widget_id: String, path: String, @@ -17,7 +17,7 @@ pub(crate) fn exists( } #[command] -pub(crate) fn is_file( +pub(crate) async fn is_file( app_handle: AppHandle, widget_id: String, path: String, @@ -27,7 +27,7 @@ pub(crate) fn is_file( } #[command] -pub(crate) fn is_dir( +pub(crate) async fn is_dir( app_handle: AppHandle, widget_id: String, path: String, @@ -37,7 +37,7 @@ pub(crate) fn is_dir( } #[command] -pub(crate) fn read_file( +pub(crate) async fn read_file( app_handle: AppHandle, widget_id: String, path: String, @@ -49,7 +49,7 @@ pub(crate) fn read_file( } #[command] -pub(crate) fn write_file( +pub(crate) async fn write_file( app_handle: AppHandle, widget_id: String, path: String, @@ -62,7 +62,7 @@ pub(crate) fn write_file( } #[command] -pub(crate) fn append_file( +pub(crate) async fn append_file( app_handle: AppHandle, widget_id: String, path: String, @@ -79,7 +79,7 @@ pub(crate) fn append_file( } #[command] -pub(crate) fn remove_file( +pub(crate) async fn remove_file( app_handle: AppHandle, widget_id: String, path: String, @@ -91,7 +91,7 @@ pub(crate) fn remove_file( } #[command] -pub(crate) fn create_dir( +pub(crate) async fn create_dir( app_handle: AppHandle, widget_id: String, path: String, @@ -103,7 +103,7 @@ pub(crate) fn create_dir( } #[command] -pub(crate) fn remove_dir( +pub(crate) async fn remove_dir( app_handle: AppHandle, widget_id: String, path: String, @@ -131,7 +131,7 @@ mod tests { } #[rstest] - fn test_exists() { + async fn test_exists() { // Test the `fs::exists` command let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -142,7 +142,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(!result.unwrap()); @@ -152,13 +153,14 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(result.unwrap()); } #[rstest] - fn test_is_file_or_dir() { + async fn test_is_file_or_dir() { // Test the `fs::is_file` and `fs::is_dir` commands let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -174,30 +176,34 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(result.unwrap()); let result = is_dir( app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(!result.unwrap()); // Check that `is_file` gives false and `is_dir` gives true for the directory let result = - is_file(app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string()); + is_file(app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string()) + .await; assert!(result.is_ok()); assert!(!result.unwrap()); let result = - is_dir(app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string()); + is_dir(app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string()) + .await; assert!(result.is_ok()); assert!(result.unwrap()); } #[rstest] - fn test_read_file() { + async fn test_read_file() { // Test the `fs::read_file` command let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -208,7 +214,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_err()); assert!(result.unwrap_err().contains( format!("Failed to read file '{}'", file_path.display()).as_str() @@ -224,13 +231,14 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert_eq!(result.unwrap(), content); } #[rstest] - fn test_write_file() { + async fn test_write_file() { // Test the `fs::write_file` command let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -243,7 +251,8 @@ mod tests { "dummy".to_string(), "dummy_file.txt".to_string(), content.to_string(), - ); + ) + .await; assert!(result.is_ok()); assert_eq!(std::fs::read_to_string(&file_path).unwrap(), content); @@ -254,13 +263,14 @@ mod tests { "dummy".to_string(), "dummy_file.txt".to_string(), new_content.to_string(), - ); + ) + .await; assert!(result.is_ok()); assert_eq!(std::fs::read_to_string(&file_path).unwrap(), new_content); } #[rstest] - fn test_append_file() { + async fn test_append_file() { // Test the `fs::append_file` command let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -273,7 +283,8 @@ mod tests { "dummy".to_string(), "dummy_file.txt".to_string(), content.to_string(), - ); + ) + .await; assert!(result.is_ok()); assert_eq!(std::fs::read_to_string(&file_path).unwrap(), content); @@ -284,7 +295,8 @@ mod tests { "dummy".to_string(), "dummy_file.txt".to_string(), new_content.to_string(), - ); + ) + .await; assert!(result.is_ok()); assert_eq!( std::fs::read_to_string(&file_path).unwrap(), @@ -293,7 +305,7 @@ mod tests { } #[rstest] - fn test_create_dir() { + async fn test_create_dir() { // Test the `fs::create_dir` command let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -302,13 +314,14 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(widget_dir.join("dummy_dir").is_dir()); } #[rstest] - fn test_remove_file_or_dir() { + async fn test_remove_file_or_dir() { // Test the `fs::remove_file` and `fs::remove_dir` commands let (base_dir, app_handle) = setup_mock_env(); let widget_dir = setup_widget_directory(base_dir.path(), "dummy"); @@ -318,7 +331,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_err()); assert!(result.unwrap_err().contains( format!( @@ -333,7 +347,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string(), - ); + ) + .await; assert!(result.is_err()); assert!(result.unwrap_err().contains( format!( @@ -354,7 +369,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string(), - ); + ) + .await; assert!(result.is_err()); assert!(result.unwrap_err().contains( format!( @@ -369,7 +385,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_err()); assert!(result.unwrap_err().contains( format!( @@ -384,7 +401,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_file.txt".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(!file_path.exists()); @@ -393,7 +411,8 @@ mod tests { app_handle.clone(), "dummy".to_string(), "dummy_dir".to_string(), - ); + ) + .await; assert!(result.is_ok()); assert!(!dir_path.exists()); } diff --git a/src-tauri/src/apis/sys/apis.rs b/src-tauri/src/apis/sys/apis.rs index 14a7ccc5..be8ebcf2 100644 --- a/src-tauri/src/apis/sys/apis.rs +++ b/src-tauri/src/apis/sys/apis.rs @@ -49,7 +49,7 @@ pub(crate) struct NetworkInfo { } #[command] -pub(crate) fn get_system_info() -> CommandOut { +pub(crate) async fn get_system_info() -> CommandOut { let mut sys = System::new_all(); sys.refresh_all(); let disks_info: Vec = Disks::new_with_refreshed_list() diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index d7ced708..ce6a9780 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -81,7 +81,7 @@ macro_rules! cmdbail { /// of the command. Instead, the widget ID will correspond to an error message instead /// of a widget configuration. #[command] -pub(crate) fn refresh_widget_collection( +pub(crate) async fn refresh_widget_collection( app_handle: AppHandle, ) -> CommandOut { let widget_base = &app_handle.state::().0; @@ -149,7 +149,7 @@ pub(crate) fn refresh_widget_collection( /// instead of a widget configuration. /// - There is an error when bundling the widget. #[command] -pub(crate) fn bundle_widget( +pub(crate) async fn bundle_widget( app_handle: AppHandle, widget_id: String, apis_blob_url: String, @@ -183,7 +183,8 @@ pub(crate) fn bundle_widget( /// Register or unregister a global shortcut for toggling the click-through state. /// -/// If `reverse` this will register the shortcut, otherwise it will unregister it. +/// If `reverse` is `false` this will register the shortcut, otherwise it will +/// unregister it. /// /// This command will fail if: /// @@ -191,7 +192,7 @@ pub(crate) fn bundle_widget( /// - The shortcut is not registered yet but we want to unregister it. /// - There is an error registering or unregistering the shortcut. #[command] -pub(crate) fn register_toggle_shortcut( +pub(crate) async fn register_toggle_shortcut( app_handle: AppHandle, shortcut: String, reverse: bool, @@ -235,7 +236,7 @@ pub(crate) fn register_toggle_shortcut( /// - The given widget ID is not found in the widget collection. /// - Tauri fails to open the resource. #[command] -pub(crate) fn open_widget_resource( +pub(crate) async fn open_widget_resource( app_handle: AppHandle, widget_id: Option, path: Option, @@ -265,7 +266,7 @@ pub(crate) fn open_widget_resource( /// This command tries to load the previously stored settings. It never fails, but /// instead returns the default settings upon any error. #[command] -pub(crate) fn init_settings( +pub(crate) async fn init_settings( app_handle: AppHandle, ) -> CommandOut { let app_config_dir = match app_handle.path().app_config_dir() { @@ -280,7 +281,7 @@ pub(crate) fn init_settings( /// This command will try to save the widget internals for persistence before exiting /// the application, but failure to do so will not prevent the application from exiting. #[command] -pub(crate) fn exit_app( +pub(crate) async fn exit_app( app_handle: AppHandle, settings: Settings, ) -> CommandOut<()> { @@ -380,7 +381,7 @@ mod tests { } #[rstest] - fn test_refresh_widget_collection() { + async fn test_refresh_widget_collection() { // Test the `refresh_widget_collection` command let (base_dir, app_handle) = setup_mock_env(); @@ -391,7 +392,7 @@ mod tests { copy_dir(fixture_dir().join("config"), &widget_base).unwrap(); // The command should not fail just because contents of any configuration file - let new_collection = refresh_widget_collection(app_handle.clone()); + let new_collection = refresh_widget_collection(app_handle.clone()).await; assert!(new_collection.is_ok()); let new_collection = new_collection.unwrap(); @@ -430,13 +431,14 @@ mod tests { } #[rstest] - fn test_bundle_widget_pass( + async fn test_bundle_widget_pass( setup_bundle_widget_env: &(TempDir, AppHandle), ) { // Test that the `bundle_widget` command bundles a widget correctly let (_base_dir, app_handle) = setup_bundle_widget_env; let result = - bundle_widget(app_handle.clone(), "pass".to_string(), Default::default()); + bundle_widget(app_handle.clone(), "pass".to_string(), Default::default()) + .await; // We only check that the result is Ok; the actual bundled content should be // checked in the bundler unit tests @@ -444,13 +446,14 @@ mod tests { } #[rstest] - fn test_bundle_widget_bundling_error( + async fn test_bundle_widget_bundling_error( setup_bundle_widget_env: &(TempDir, AppHandle), ) { // Test that the `bundle_widget` command raises upon bundling error let (_base_dir, app_handle) = setup_bundle_widget_env; let result = - bundle_widget(app_handle.clone(), "fail".to_string(), Default::default()); + bundle_widget(app_handle.clone(), "fail".to_string(), Default::default()) + .await; assert!(result.is_err()); let error = result.unwrap_err(); @@ -461,7 +464,7 @@ mod tests { } #[rstest] - fn test_bundle_widget_id_not_found( + async fn test_bundle_widget_id_not_found( setup_bundle_widget_env: &(TempDir, AppHandle), ) { // Test that the `bundle_widget` command raises for an unknown widget ID @@ -470,7 +473,8 @@ mod tests { app_handle.clone(), "non_existent_id".to_string(), Default::default(), - ); + ) + .await; assert!(result.is_err()); let error = result.unwrap_err(); @@ -478,7 +482,7 @@ mod tests { } #[rstest] - fn test_bundle_widget_invalid_conf( + async fn test_bundle_widget_invalid_conf( setup_bundle_widget_env: &(TempDir, AppHandle), ) { // Test that the `bundle_widget` command propagates the error message held in @@ -488,7 +492,8 @@ mod tests { app_handle.clone(), "invalid_conf".to_string(), Default::default(), - ); + ) + .await; assert!(result.is_err()); let error = result.unwrap_err();