From: Ian Jackson Date: Sun, 11 Oct 2020 21:12:35 +0000 (+0100) Subject: wip wasm bindings X-Git-Tag: otter-0.2.0~678 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=403024bccbdf1410a087196fca3b7a62b273b7a9;p=otter.git wip wasm bindings Signed-off-by: Ian Jackson --- diff --git a/wasm/wasm.rs b/wasm/wasm.rs index 819cba58..af7a2c7e 100644 --- a/wasm/wasm.rs +++ b/wasm/wasm.rs @@ -1,12 +1,41 @@ use wasm_bindgen::prelude::*; use fehler::throws; +use std::fmt::Display; use zcoord::ZCoord; #[wasm_bindgen] pub struct ZCoordIterator (zcoord::Mutable); +trait WasmError { + fn e(self) -> JsValue; +} + +impl WasmError for E where E: Display { + fn e(self) -> JsValue { + let s = format!("{}", self); + JsValue::from_str(&s) + } +} + +trait WasmResult { + fn e(self) -> Result; +} + +impl WasmResult for Result { + fn e(self) -> Result { self.map_err(WasmError::e) } +} + +#[throws(JsValue)] +#[wasm_bindgen] +pub fn check(packed: &JsValue) { + let s = packed.as_string().ok_or_else( + ||"packed Z coordinate wrong JS type (not a string)" + ).e()?; + ZCoord::check_str(&s).ok_or(zcoord::ParseError).e()?; +} + const X : &'static str = "invalid value passed to wasm"; #[throws(JsValue)]