Skip to content

Commit

Permalink
Use a message result wrapper instead of hand-writing the result json
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas authored and MattFerraro committed May 27, 2024
1 parent 4d58e31 commit 6581476
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/cadmium/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use message::Message;
use message::{Message, MessageResult};
use wasm_bindgen::prelude::*;
extern crate console_error_panic_hook;

Expand Down Expand Up @@ -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)]
Expand Down
18 changes: 18 additions & 0 deletions packages/cadmium/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<String, anyhow::Error>> for MessageResult {
fn from(result: Result<String, anyhow::Error>) -> 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 {
Expand Down

0 comments on commit 6581476

Please sign in to comment.