From 8406d37991ad788bf9d39d7166c9cdbe7c7bb87f Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 27 Jan 2024 16:13:17 -0500 Subject: [PATCH] Switch to using JSON for wasm. serde_wasm_bindgen was silently dropping many attributes (I suspect it is triggered by serde flatten) so this switches to serializing to JSON for passing values from wasm to js. --- Cargo.toml | 1 + src/wasm_cli/mod.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9ed49bce..dcc86327 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ path = "src/lib.rs" [dependencies] futures = { version = "0.3.28", optional = true } +gloo-utils = "0.2.0" nom = "7.1.1" opentelemetry = { version = "0.20.0", optional = true, default-features = false, features = ["trace", "rt-tokio"] } opentelemetry-otlp = { version = "0.13.0", optional = true } diff --git a/src/wasm_cli/mod.rs b/src/wasm_cli/mod.rs index 54cf0c58..c3d3c3a3 100644 --- a/src/wasm_cli/mod.rs +++ b/src/wasm_cli/mod.rs @@ -1,3 +1,6 @@ +use gloo_utils::format::JsValueSerdeExt; +use wasm_bindgen::JsValue; + use crate::parser::parse_with_settings; use crate::settings::GlobalSettings; use crate::wasm::ParseResult; @@ -8,17 +11,15 @@ pub fn parse_org(org_contents: &str) -> wasm_bindgen::JsValue { let rust_parsed = match parse_with_settings(org_contents, &GlobalSettings::default()) { Ok(document) => document, Err(err) => { - return serde_wasm_bindgen::to_value(&ParseResult::Error(format!("{:?}", err))) - .unwrap(); + return JsValue::from_serde(&ParseResult::Error(format!("{:?}", err))).unwrap(); } }; let to_wasm_context = ToWasmContext::new(org_contents); let wasm_document = match rust_parsed.to_wasm(to_wasm_context) { Ok(document) => document, Err(err) => { - return serde_wasm_bindgen::to_value(&ParseResult::Error(format!("{:?}", err))) - .unwrap(); + return JsValue::from_serde(&ParseResult::Error(format!("{:?}", err))).unwrap(); } }; - serde_wasm_bindgen::to_value(&ParseResult::Success(wasm_document)).unwrap() + JsValue::from_serde(&ParseResult::Success(wasm_document)).unwrap() }