From 6581476265a27b0ee276addc0d60885761ceed96 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Sun, 26 May 2024 15:38:45 +0300 Subject: [PATCH] Use a message result wrapper instead of hand-writing the result json Signed-off-by: Dimitris Zervas --- packages/cadmium/src/lib.rs | 9 +++------ packages/cadmium/src/message.rs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/cadmium/src/lib.rs b/packages/cadmium/src/lib.rs index 4241e1bf..38ce23ab 100644 --- a/packages/cadmium/src/lib.rs +++ b/packages/cadmium/src/lib.rs @@ -1,4 +1,4 @@ -use message::Message; +use message::{Message, MessageResult}; use wasm_bindgen::prelude::*; extern crate console_error_panic_hook; @@ -76,11 +76,8 @@ impl Project { } #[wasm_bindgen] - pub fn send_message(&mut self, message: Message) -> String { - match message.handle(&mut self.native) { - Ok(d) => format!(r#"{{ "success": {{ {} }}"#, d), - Err(e) => format!(r#"{{ "error": {{ {} }}"#, e), - } + pub fn send_message(&mut self, message: Message) -> MessageResult { + message.handle(&mut self.native).into() } // #[wasm_bindgen(getter)] diff --git a/packages/cadmium/src/message.rs b/packages/cadmium/src/message.rs index 64f8d4e3..759f3f46 100644 --- a/packages/cadmium/src/message.rs +++ b/packages/cadmium/src/message.rs @@ -9,6 +9,24 @@ use crate::extrusion::{Direction, Extrusion, ExtrusionMode}; use crate::project::Project; use crate::step::StepData; +#[derive(Tsify, Debug, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub enum MessageResult { + #[serde(rename = "success")] + Success(String), + #[serde(rename = "error")] + Error(String), +} + +impl From> for MessageResult { + fn from(result: Result) -> Self { + match result { + Ok(msg) => MessageResult::Success(msg), + Err(e) => MessageResult::Error(e.to_string()), + } + } +} + #[derive(Tsify, Debug, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum Message {