From 7b88580fe7c03290bd63066ea7395f37ef4bb8e6 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 25 Feb 2023 14:12:18 -0500 Subject: [PATCH] Implement wasm bindgen initial version --- this_algorithm-wasm/index.html | 15 ++++++++++----- this_algorithm-wasm/index.js | 23 +++++++++++++++++++++++ this_algorithm-wasm/src/lib.rs | 16 +++++++++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 this_algorithm-wasm/index.js diff --git a/this_algorithm-wasm/index.html b/this_algorithm-wasm/index.html index a0d5288..12548a3 100644 --- a/this_algorithm-wasm/index.html +++ b/this_algorithm-wasm/index.html @@ -5,11 +5,16 @@ hello-wasm example - + diff --git a/this_algorithm-wasm/index.js b/this_algorithm-wasm/index.js new file mode 100644 index 0000000..af725cc --- /dev/null +++ b/this_algorithm-wasm/index.js @@ -0,0 +1,23 @@ +// import init, { log_to_address, greet } from "./pkg/this_algorithm_wasm.js"; +// init().then(() => { +// console.log(greet()); +// }); + +import init, { address_lat_lon } from "./pkg/this_algorithm_wasm.js"; +init().then(() => { + let update = () => { + let lat = parseFloat(addressLat.value); + let lon = parseFloat(addressLon.value); + + if (!(lat || lat == 0.0) && !(lon || lon == 0.0)) { + return; + } + + let ret = address_lat_lon(lat, lon); + addressResult.textContent = ret; + }; + + addressLat.addEventListener("input", update); + addressLon.addEventListener("input", update); + update(); +}); diff --git a/this_algorithm-wasm/src/lib.rs b/this_algorithm-wasm/src/lib.rs index d49cf4d..c695bec 100644 --- a/this_algorithm-wasm/src/lib.rs +++ b/this_algorithm-wasm/src/lib.rs @@ -1,11 +1,21 @@ +use this_algorithm::Address; use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { - pub fn alert(s: &str); + // fn alert(s: &str); + #[wasm_bindgen(js_namespace = console, js_name = log)] + fn log_str(s: &str); } #[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)); +pub fn address_lat_lon(lat: f64, lon: f64) -> Result { + Address::from_lat_lon(lat, lon) + .map(|a| a.to_string()) + .map_err(|e| e.to_string()) } + +// #[wasm_bindgen] +// pub fn to_lat_lon(address: &str) -> Result>< { + +// }