diff --git a/web-frontend/src/lib/common.js b/web-frontend/src/lib/common.js index 8962149..7e12429 100644 --- a/web-frontend/src/lib/common.js +++ b/web-frontend/src/lib/common.js @@ -1,6 +1,21 @@ import init from 'xpin-wasm'; import * as xpinWasm from 'xpin-wasm'; +export const emptyxpin = { + address: '', + latLon: [0.0, 0.0] +}; + +export function getxpin(xpin) { + if (!xpin) { + return; + } + return { + address: xpin.get_address(), + latLon: xpin.get_lat_lon() + }; +} + export const WasmStatus = { NotLoaded: -1, Loaded: 0, diff --git a/web-frontend/src/routes/app/+page.svelte b/web-frontend/src/routes/app/+page.svelte index 4466232..9fc11a5 100644 --- a/web-frontend/src/routes/app/+page.svelte +++ b/web-frontend/src/routes/app/+page.svelte @@ -4,8 +4,8 @@ import { onMount, onDestroy } from 'svelte'; import AddressInput from '$lib/AddressInput.svelte'; import { browser } from '$app/environment'; - // import tick from 'svelte'; import Error from './Error.svelte'; + import { emptyxpin, getxpin } from '$lib/common.js'; let leaflet; import Map from '$lib/Map.svelte'; @@ -13,7 +13,9 @@ let map; let latlng = { lat: '', lng: '' }; - let addr = ''; + let addr = emptyxpin; + let addrInputValue = ''; + let wasm = { status: WasmStatus.NotLoaded, error: undefined, @@ -25,11 +27,17 @@ try { latlng = e.latlng; - addr = wasm.call.address_from_lat_lon(latlng.lat, latlng.lng); - popup.setLatLng(e.latlng).setContent(`${addr}`).openOn(map); + addr = getxpin(wasm.call.EncodedAddress.from_lat_lon(latlng.lat, latlng.lng)); + console.log(latlng, addr); + popup.setLatLng({ + lat:addr.latLon[0], + lng:addr.latLon[1] + }).setContent(`${addr.address}`).openOn(map); + // popup.setLatLng(e.latlng).setContent(`${addr.address}`).openOn(map); } catch (err) { console.error(err); - addr = ''; + // addr = emptyxpin; + addrInputValue = ''; popup.setLatLng(e.latlng).setContent(`You clicked at ${e.latlng}`).openOn(map); } }; @@ -62,7 +70,7 @@ const input = () => { try { if (browser) { - outputValue = wasm.call.address_to_lat_lon(addr); + addr = getxpin(wasm.call.EncodedAddress.from_address(addr)); let latlng = new leaflet.LatLng(outputValue[0], outputValue[1]); map.panTo(latlng, 20); leaflet.popup().setLatLng(latlng).setContent(`${outputValue}
hi`).openOn(map); @@ -91,9 +99,9 @@ {#await init()}

Loading WebAssembly module...

{:then} -

Current cursor: {addr} => ({latlng.lat}, {latlng.lng})

+

Current cursor: {addr.address} ({addr.latLon}) => ({latlng.lat}, {latlng.lng})

- + {:catch message} diff --git a/xpin-wasm/src/lib.rs b/xpin-wasm/src/lib.rs index c05c076..159c4b6 100644 --- a/xpin-wasm/src/lib.rs +++ b/xpin-wasm/src/lib.rs @@ -1,25 +1,51 @@ use std::str::FromStr; use wasm_bindgen::prelude::*; - -// #[wasm_bindgen] -// extern "C" { -// #[wasm_bindgen(js_namespace = console, js_name = log)] -// fn log_str(s: &str); -// } +use xpin::Address; #[wasm_bindgen] -pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result { - xpin::Address::from_lat_lon(lat, lon) - .map(|a| a.to_string()) - .map_err(|e| e.to_string()) +pub struct EncodedAddress { + address: String, + pub lat: f64, + pub lon: f64, } #[wasm_bindgen] -pub fn address_to_lat_lon(addr_str: &str) -> Result, String> { - xpin::Address::from_str(addr_str) - .as_ref() - .map_err(|e| e.to_string()) - .map(xpin::Address::as_lat_lon) - .map(|(lat, lon)| vec![lat, lon]) +impl EncodedAddress { + #[wasm_bindgen] + pub fn from_lat_lon(lat: f64, lon: f64) -> Result { + xpin::Address::from_lat_lon(lat, lon) + .as_ref() + .map(EncodedAddress::from) + .map_err(|e| e.to_string()) + } + + #[wasm_bindgen] + pub fn from_address(addr_str: &str) -> Result { + xpin::Address::from_str(addr_str) + .as_ref() + .map(EncodedAddress::from) + .map_err(|e| e.to_string()) + } + + #[wasm_bindgen] + pub fn get_address(&self) -> String { + self.address.clone() + } + + #[wasm_bindgen] + pub fn get_lat_lon(&self) -> Vec { + vec![self.lat, self.lon] + } +} + +impl From<&'_ Address<'_>> for EncodedAddress { + fn from(addr: &Address) -> Self { + let (lat, lon) = addr.as_lat_lon(); + Self { + address: addr.to_string(), + lat, + lon, + } + } }