Allow encoding and decoding

This commit is contained in:
Austen Adler 2023-02-25 14:49:09 -05:00
parent 7b88580fe7
commit d8958e0645
4 changed files with 91 additions and 23 deletions

View File

@ -196,6 +196,12 @@ impl Address<'_> {
} }
} }
} }
// TODO: Test this
pub fn to_lat_lon(&self) -> (f64, f64) {
let lat_lng = LatLng::from(self.as_cell_id());
(lat_lng.lat.deg(), lat_lng.lng.deg())
}
} }
/// Extracts a version number from a number /// Extracts a version number from a number

View File

@ -5,14 +5,42 @@
<title>hello-wasm example</title> <title>hello-wasm example</title>
</head> </head>
<body> <body>
<input id="address-lat" type="text"></input> <fieldset>
<input id="address-lon" type="text"></input> <label for="encode-address-lat">Latitude</label>
<p id="address-result">(None)</p> <input id="encode-address-lat" type="text" />
<br>
<label for="encode-address-lon">Longitude</label>
<input id="encode-address-lon" type="text" />
<br>
<label for="encode-address-result">encode-address-result</label>
<input disabled id="encode-address-result" />
<br>
<p type="text" id="encode-address-error"></p>
</fieldset>
<fieldset>
<label for="decode-address-input">decode-address-input</label>
<input id="decode-address-input" type="text" />
<br>
<label for="decode-address-lat">Latitude</label>
<input disabled id="decode-address-lat" type="text" />
<br>
<label for="decode-address-lon">Longitude</label>
<input disabled id="decode-address-lon" type="text" />
<br>
<p type="text" id="decode-address-error"></p>
</fieldset>
<script> <script>
const addressResult = document.getElementById("address-result"); const encodeAddressResult = document.getElementById("encode-address-result");
const addressLat = document.getElementById("address-lat"); const encodeAddressError = document.getElementById("encode-address-error");
const addressLon = document.getElementById("address-lon"); const encodeAddressLat = document.getElementById("encode-address-lat");
var lta; const encodeAddressLon = document.getElementById("encode-address-lon");
const decodeAddressInput = document.getElementById("decode-address-input");
const decodeAddressError = document.getElementById("decode-address-error");
const decodeAddressLat = document.getElementById("decode-address-lat");
const decodeAddressLon = document.getElementById("decode-address-lon");
</script> </script>
<script type="module" src="index.js"> <script type="module" src="index.js">
</script> </script>

View File

@ -3,21 +3,49 @@
// console.log(greet()); // console.log(greet());
// }); // });
import init, { address_lat_lon } from "./pkg/this_algorithm_wasm.js"; import init, { address_from_lat_lon, address_to_lat_lon } from "./pkg/this_algorithm_wasm.js";
init().then(() => { init().then(() => {
let update = () => { let updateEncode = () => {
let lat = parseFloat(addressLat.value); if (!encodeAddressLat.value || !encodeAddressLon.value) {
let lon = parseFloat(addressLon.value);
if (!(lat || lat == 0.0) && !(lon || lon == 0.0)) {
return; return;
} }
let ret = address_lat_lon(lat, lon); let lat = parseFloat(encodeAddressLat.value);
addressResult.textContent = ret; let lon = parseFloat(encodeAddressLon.value);
try {
encodeAddressResult.value = address_from_lat_lon(lat, lon);
encodeAddressError.textContent = "";
} catch(e) {
encodeAddressError.textContent = e;
}
}; };
addressLat.addEventListener("input", update); let updateDecode = () => {
addressLon.addEventListener("input", update); let addressString = decodeAddressInput.value.trim();
update(); if (!addressString) {
return;
}
try {
let latLon = address_to_lat_lon(addressString);
decodeAddressError.textContent = "";
decodeAddressLat.value = latLon[0];
decodeAddressLon.value = latLon[1];
} catch(e) {
decodeAddressError.textContent = e;
decodeAddressLat.value = "";
decodeAddressLon.value = "";
}
}
// Bind events
encodeAddressLat.addEventListener("input", updateEncode);
encodeAddressLon.addEventListener("input", updateEncode);
decodeAddressInput.addEventListener("input", updateDecode);
// Run in case either field contains data
updateEncode();
updateDecode();
}); });

View File

@ -1,3 +1,5 @@
use std::str::FromStr;
use this_algorithm::Address; use this_algorithm::Address;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -9,13 +11,17 @@ extern "C" {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn address_lat_lon(lat: f64, lon: f64) -> Result<String, String> { pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
Address::from_lat_lon(lat, lon) Address::from_lat_lon(lat, lon)
.map(|a| a.to_string()) .map(|a| a.to_string())
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
// #[wasm_bindgen] #[wasm_bindgen]
// pub fn to_lat_lon(address: &str) -> Result>< { pub fn address_to_lat_lon(addr_str: &str) -> Result<Vec<f64>, String> {
Address::from_str(addr_str)
// } .as_ref()
.map_err(|e| e.to_string())
.map(Address::to_lat_lon)
.map(|(lat, lon)| vec![lat, lon])
}