From 45d5720f1610fe288d7e6e3a2734f35f50f193f1 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Mon, 14 Nov 2022 10:13:24 +0100 Subject: [PATCH] improve error handling --- src/clipboard.rs | 4 --- src/dialog.rs | 63 ++++++++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/clipboard.rs b/src/clipboard.rs index 3fd5d93..1715f18 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,7 +1,3 @@ -use wasm_bindgen_futures::JsFuture; - -use crate::Error; - /// Gets the clipboard content as plain text. /// /// # Example diff --git a/src/dialog.rs b/src/dialog.rs index 2127741..8933ed1 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -136,10 +136,10 @@ impl SaveDialogOptions { /// @param options Dialog options. /// @returns Whether the user selected `Yes` or `No`. #[inline(always)] -pub async fn ask(message: &str, options: Option) -> Option { - inner::ask(message, serde_wasm_bindgen::to_value(&options).unwrap()) - .await - .as_bool() +pub async fn ask(message: &str, options: Option) -> crate::Result { + let js_val = inner::ask(message, serde_wasm_bindgen::to_value(&options)?).await?; + + Ok(serde_wasm_bindgen::from_value(js_val)?) } /// Shows a question dialog with `Ok` and `Cancel` buttons. @@ -152,10 +152,10 @@ pub async fn ask(message: &str, options: Option) -> Option /// let confirmed = confirm("Are you sure?", None).await; /// ``` /// @returns Whether the user selelced `Ok` or `Cancel`. -pub async fn confirm(message: &str, options: Option) -> Option { - inner::confirm(message, serde_wasm_bindgen::to_value(&options).unwrap()) - .await - .as_bool() +pub async fn confirm(message: &str, options: Option) -> crate::Result { + let js_val = inner::confirm(message, serde_wasm_bindgen::to_value(&options)?).await?; + + Ok(serde_wasm_bindgen::from_value(js_val)?) } /// Shows a message dialog with an `Ok` button. @@ -170,8 +170,8 @@ pub async fn confirm(message: &str, options: Option) -> Op /// @param message Message to display. /// @param options Dialog options. /// @returns Promise resolved when user closes the dialog. -pub async fn message(message: &str, options: Option) { - inner::message(message, serde_wasm_bindgen::to_value(&options).unwrap()).await +pub async fn message(message: &str, options: Option) -> crate::Result<()> { + Ok(inner::message(message, serde_wasm_bindgen::to_value(&options)?).await?) } /// Opens a file/directory selection dialog for a single file. @@ -198,9 +198,10 @@ pub async fn message(message: &str, options: Option) { /// ``` /// @param options Dialog options. /// @returns List of file paths, or `None` if user cancelled the dialog. -pub async fn open(options: Option) -> Option { - let file = inner::open(serde_wasm_bindgen::to_value(&options).unwrap()).await; - serde_wasm_bindgen::from_value(file).unwrap() +pub async fn open(options: Option) -> crate::Result> { + let file = inner::open(serde_wasm_bindgen::to_value(&options)?).await?; + + Ok(serde_wasm_bindgen::from_value(file)?) } /// Opens a file/directory selection dialog for multiple files. @@ -228,9 +229,12 @@ pub async fn open(options: Option) -> Option { /// ``` /// @param options Dialog options. /// @returns List of file paths, or `None` if user cancelled the dialog. -pub async fn open_multiple(options: Option) -> Option> { - let files = inner::open_multiple(serde_wasm_bindgen::to_value(&options).unwrap()).await; - serde_wasm_bindgen::from_value(files).unwrap() +pub async fn open_multiple( + options: Option, +) -> crate::Result>> { + let files = inner::open_multiple(serde_wasm_bindgen::to_value(&options)?).await?; + + Ok(serde_wasm_bindgen::from_value(files)?) } /// Opens a file/directory save dialog. @@ -252,21 +256,28 @@ pub async fn open_multiple(options: Option) -> Option) -> Option { - let path = inner::save(serde_wasm_bindgen::to_value(&options).unwrap()).await; - serde_wasm_bindgen::from_value(path).unwrap() +pub async fn save(options: Option) -> crate::Result> { + let path = inner::save(serde_wasm_bindgen::to_value(&options)?).await?; + + Ok(serde_wasm_bindgen::from_value(path)?) } mod inner { use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; - #[wasm_bindgen(module = "/dist/dialog.js")] + #[wasm_bindgen(module = "/src/dialog.js")] extern "C" { - pub async fn ask(message: &str, options: JsValue) -> JsValue; - pub async fn confirm(message: &str, options: JsValue) -> JsValue; - pub async fn open(options: JsValue) -> JsValue; - pub async fn open_multiple(options: JsValue) -> JsValue; - pub async fn message(message: &str, option: JsValue); - pub async fn save(options: JsValue) -> JsValue; + #[wasm_bindgen(catch)] + pub async fn ask(message: &str, options: JsValue) -> Result; + #[wasm_bindgen(catch)] + pub async fn confirm(message: &str, options: JsValue) -> Result; + #[wasm_bindgen(catch)] + pub async fn open(options: JsValue) -> Result; + #[wasm_bindgen(catch)] + pub async fn open_multiple(options: JsValue) -> Result; + #[wasm_bindgen(catch)] + pub async fn message(message: &str, option: JsValue) -> Result<(), JsValue>; + #[wasm_bindgen(catch)] + pub async fn save(options: JsValue) -> Result; } }