From 403024bccbdf1410a087196fca3b7a62b273b7a9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 11 Oct 2020 22:12:35 +0100 Subject: [PATCH] wip wasm bindings Signed-off-by: Ian Jackson --- wasm/wasm.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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)] -- 2.30.2