Convert all coordinates to string for wasm

This commit is contained in:
Austen Adler 2023-03-25 22:08:25 -04:00
parent 8c6fff2a3b
commit 99d111ea17
4 changed files with 49 additions and 0 deletions

View File

@ -37,6 +37,22 @@ impl TryFrom<LatLon> for Coordinates {
}
}
impl TryFrom<&crate::Coordinate> for Coordinates {
type Error = Error;
fn try_from(value: &crate::Coordinate) -> Result<Self, Self::Error> {
Self::try_from(LatLon::from(value))
}
}
impl TryFrom<crate::Coordinate> for Coordinates {
type Error = Error;
fn try_from(value: crate::Coordinate) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
impl FromStr for Coordinates {
type Err = Error;

View File

@ -152,6 +152,7 @@ mod tests {
easting: 666962.588,
northing: 0.0,
// Allow nearby comparisons
latlon: LatLon::new(1.534972646100779e-9, 100.50027886695585).unwrap()
})
);

View File

@ -76,6 +76,7 @@
})
.then(async () => {
// Initialize the app
// TODO: This leads to a `map is undefined` error
updateAddr(wasm.call.EncodedAddress.from_coordinate(coordinateInputValue), true);
})
.then(async () => {

View File

@ -55,6 +55,9 @@ pub struct EncodedAddress {
#[wasm_bindgen(js_name = decimalDegrees)]
pub decimal_degrees: String,
#[wasm_bindgen(js_name = allCoordinates)]
pub all_coordinates: Coordinates,
}
#[wasm_bindgen]
@ -118,6 +121,30 @@ impl EncodedAddress {
}
}
#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct Coordinates {
// pub latlon: LatLon,
pub dd: String,
pub dms: String,
pub dmm: String,
pub utm: String,
// pub xpin: String,
pub plus: String,
}
impl From<&spatial_coordinate_systems::all::Coordinates> for Coordinates {
fn from(value: &spatial_coordinate_systems::all::Coordinates) -> Self {
Self {
dd: value.dd.to_string(),
dms: value.dms.to_string(),
dmm: value.dmm.to_string(),
utm: value.utm.to_string(),
plus: value.plus.to_string(),
}
}
}
impl TryFrom<&'_ Address<'_>> for EncodedAddress {
type Error = String;
@ -129,6 +156,9 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress {
let src_coords =
Coordinate::from_str(&format!("{}, {}", lat, lon)).map_err(|e| e.to_string())?;
let all_coordinates = spatial_coordinate_systems::all::Coordinates::try_from(&src_coords)
.map_err(|e| e.to_string())?;
Ok(Self {
address: addr.to_string(),
lat_lon: Box::new([lat, lon]),
@ -136,6 +166,7 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress {
src_coords_type: src_coords.get_type().into(),
src_coords,
decimal_degrees: format!("{}, {}", lat, lon),
all_coordinates: Coordinates::from(&all_coordinates),
})
}
}