Implemented core > invoke and convert_file_src.

This commit is contained in:
Brian Carlsen 2024-07-01 10:37:43 +02:00
parent d1b7948a4c
commit 85d92d3e3a
No known key found for this signature in database
GPG key ID: B14D281A79B2DDB7
5 changed files with 73 additions and 3 deletions

View file

@ -24,7 +24,8 @@ wasm-bindgen-test = "0.3.42"
all-features = true
[features]
all = ["event"]
all = ["core", "event"]
core = []
event = ["dep:futures"]
[workspace]

View file

@ -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`

12
src/core.js Normal file
View file

@ -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
}

53
src/core.rs Normal file
View file

@ -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<T>(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<T, E>(command: &str, args: impl Serialize) -> Result<T, E>
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<str>) -> String {
inner::convert_file_src(file_path.as_ref(), "asset")
.as_string()
.unwrap()
}
pub fn convert_file_src_with_protocol(
file_path: impl AsRef<str>,
protocol: impl AsRef<str>,
) -> 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<JsValue, JsValue>;
#[wasm_bindgen(js_name = "convertFileSrc")]
pub fn convert_file_src(filePath: &str, protocol: &str) -> JsValue;
}
}

View file

@ -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<T> = core::result::Result<T, Error>;
pub(crate) type Result<T> = std::result::Result<T, Error>;
// #[cfg(any(feature = "window"))]
// pub(crate) mod utils {