66 lines
1.6 KiB
Rust
Raw Normal View History

2023-02-25 14:49:09 -05:00
use std::str::FromStr;
2023-02-25 11:14:25 -05:00
use wasm_bindgen::prelude::*;
2023-03-14 23:26:42 -04:00
use xpin::Address;
2023-02-25 11:14:25 -05:00
#[wasm_bindgen]
2023-03-14 23:26:42 -04:00
pub struct EncodedAddress {
address: String,
pub lat: f64,
pub lon: f64,
2023-02-25 11:14:25 -05:00
}
2023-02-25 14:12:18 -05:00
2023-02-25 14:49:09 -05:00
#[wasm_bindgen]
2023-03-14 23:26:42 -04:00
impl EncodedAddress {
2023-03-19 13:03:19 -04:00
/// Get the current address as decimal degrees
#[wasm_bindgen]
pub fn get_decimal_degrees(&self) -> String {
format!("{}, {}", self.lat, self.lon)
}
/// Get an encoded address from a latitude/longitude
2023-03-14 23:26:42 -04:00
#[wasm_bindgen]
pub fn from_lat_lon(lat: f64, lon: f64) -> Result<EncodedAddress, String> {
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<EncodedAddress, String> {
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<f64> {
vec![self.lat, self.lon]
}
}
2023-03-20 00:19:18 -04:00
#[wasm_bindgen]
pub fn parse(i: &str) -> Result<String, String> {
spatial_coordinate_systems::Coordinate::from_str(i)
.map(|c| format!("{c:?}"))
.map_err(|()| format!("Could not parse str as a coordinate {i:?}"))
}
2023-03-14 23:26:42 -04:00
impl From<&'_ Address<'_>> for EncodedAddress {
fn from(addr: &Address) -> Self {
let (lat, lon) = addr.as_lat_lon();
Self {
address: addr.to_string(),
lat,
lon,
}
}
2023-02-25 14:49:09 -05:00
}