From 85d92d3e3a83d10a7de6d86811b5ca4d9067e162 Mon Sep 17 00:00:00 2001 From: Brian Carlsen Date: Mon, 1 Jul 2024 10:37:43 +0200 Subject: [PATCH] Implemented core > invoke and convert_file_src. --- Cargo.toml | 3 ++- README.md | 3 ++- src/core.js | 12 ++++++++++++ src/core.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 ++++- 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/core.js create mode 100644 src/core.rs diff --git a/Cargo.toml b/Cargo.toml index 1ad58ed..9da2b6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,8 @@ wasm-bindgen-test = "0.3.42" all-features = true [features] -all = ["event"] +all = ["core", "event"] +core = [] event = ["dep:futures"] [workspace] diff --git a/README.md b/README.md index 4eea7ca..f9af4f4 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ fn main() { All modules are gated by accordingly named Cargo features. It is recommended you keep this synced with the features enabled in your [Tauri Allowlist] but no automated tool for this exists (yet). - **all**: Enables all modules. +- **core**: Enables the `core` module. (Only `invoke` and `convertFileSrc` currently implemented.) - **event**: Enables the `event` module. ## Are we Tauri yet? @@ -58,7 +59,7 @@ All modules are gated by accordingly named Cargo features. It is recommended you These API bindings are not completely on-par with `@tauri-apps/api` yet, but here is the current status-quo: - [ ] `app` -- [ ] `core` +- [-] `core` - [ ] `dpi` - [x] `event` - [ ] `image` diff --git a/src/core.js b/src/core.js new file mode 100644 index 0000000..18db3e2 --- /dev/null +++ b/src/core.js @@ -0,0 +1,12 @@ +// tauri/tooling/api/src/core.ts +async function invoke(cmd, args = {}) { + // NB: `options` ignored as not used here. + return window.__TAURI_INTERNALS__.invoke(cmd, args) +} +function convertFileSrc(filePath, protocol = 'asset') { + return window.__TAURI_INTERNALS__.convertFileSrc(filePath, protocol) +} +export { + invoke, + convertFileSrc +} diff --git a/src/core.rs b/src/core.rs new file mode 100644 index 0000000..a5b598c --- /dev/null +++ b/src/core.rs @@ -0,0 +1,53 @@ +//! Common functionality +use std::path::PathBuf; + +use serde::{de::DeserializeOwned, Serialize}; +use serde_wasm_bindgen as swb; + +pub async fn invoke(command: &str, args: impl Serialize) -> T +where + T: DeserializeOwned, +{ + let value = inner::invoke(command, swb::to_value(&args).unwrap()).await; + swb::from_value(value).unwrap() +} + +pub async fn invoke_result(command: &str, args: impl Serialize) -> Result +where + T: DeserializeOwned, + E: DeserializeOwned, +{ + inner::invoke_result(command, swb::to_value(&args).unwrap()) + .await + .map(|val| swb::from_value(val).unwrap()) + .map_err(|err| swb::from_value(err).unwrap()) +} + +pub fn convert_file_src(file_path: impl AsRef) -> String { + inner::convert_file_src(file_path.as_ref(), "asset") + .as_string() + .unwrap() +} + +pub fn convert_file_src_with_protocol( + file_path: impl AsRef, + protocol: impl AsRef, +) -> String { + inner::convert_file_src(file_path.as_ref(), protocol.as_ref()) + .as_string() + .unwrap() +} + +mod inner { + use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; + + #[wasm_bindgen(module = "/src/core.js")] + extern "C" { + #[wasm_bindgen] + pub async fn invoke(cmd: &str, args: JsValue) -> JsValue; + #[wasm_bindgen(js_name = "invoke", catch)] + pub async fn invoke_result(cmd: &str, args: JsValue) -> Result; + #[wasm_bindgen(js_name = "convertFileSrc")] + pub fn convert_file_src(filePath: &str, protocol: &str) -> JsValue; + } +} diff --git a/src/lib.rs b/src/lib.rs index 3798226..0aca1c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,8 +89,11 @@ mod error; #[cfg(feature = "event")] pub mod event; +#[cfg(feature = "core")] +pub mod core; + pub use error::Error; -pub(crate) type Result = core::result::Result; +pub(crate) type Result = std::result::Result; // #[cfg(any(feature = "window"))] // pub(crate) mod utils {