diff --git a/spatial-coordinate-systems/src/all.rs b/spatial-coordinate-systems/src/all.rs new file mode 100644 index 0000000..4b5e384 --- /dev/null +++ b/spatial-coordinate-systems/src/all.rs @@ -0,0 +1,46 @@ +use std::str::FromStr; + +use crate::{ + dd, + dmm, + dms, + plus, //xpin + utm, + Coordinate, + Error, + LatLon, +}; + +pub struct Coordinates { + pub latlon: LatLon, + + pub dd: dd::Coordinate, + pub dms: dms::Coordinate, + pub dmm: dmm::Coordinate, + pub utm: utm::Coordinate, + // pub xpin: xpin::Xpin, + pub plus: plus::Coordinate, +} + +impl TryFrom for Coordinates { + type Error = Error; + + fn try_from(latlon: LatLon) -> Result { + Ok(Self { + latlon, + dd: dd::Coordinate::try_from(latlon)?, + dms: dms::Coordinate::try_from(latlon)?, + dmm: dmm::Coordinate::try_from(latlon)?, + utm: utm::Coordinate::try_from(latlon)?, + plus: plus::Coordinate::try_from(latlon)?, + }) + } +} + +impl FromStr for Coordinates { + type Err = Error; + + fn from_str(i: &str) -> Result { + Self::try_from(LatLon::from(&Coordinate::from_str(i)?)) + } +} diff --git a/spatial-coordinate-systems/src/lib.rs b/spatial-coordinate-systems/src/lib.rs index 00eda9e..2c5071f 100644 --- a/spatial-coordinate-systems/src/lib.rs +++ b/spatial-coordinate-systems/src/lib.rs @@ -1,14 +1,15 @@ use std::{fmt, str::FromStr}; +pub mod all; mod common; pub mod dd; pub mod dmm; pub mod dms; -mod error; pub mod latlon; pub mod plus; pub mod utm; pub use error::Error; // pub mod xpin; +mod error; use nom::{ branch::alt, @@ -210,10 +211,10 @@ impl fmt::Display for Coordinate { } } impl FromStr for Coordinate { - type Err = (); + type Err = Error; fn from_str(i: &str) -> Result { - Self::parse(i).map_err(|_| ()).map(|(_, ret)| ret) + Self::parse(i).map(|(_, ret)| ret).map_err(Into::into) } } diff --git a/xpin-wasm/src/lib.rs b/xpin-wasm/src/lib.rs index ffd35bb..be9a6d4 100644 --- a/xpin-wasm/src/lib.rs +++ b/xpin-wasm/src/lib.rs @@ -35,7 +35,7 @@ impl From for CoordinateType { spatial_coordinate_systems::CoordinateType::DMS => Self::DMS, spatial_coordinate_systems::CoordinateType::DMM => Self::DMM, spatial_coordinate_systems::CoordinateType::UTM => Self::UTM, - // spatial_coordinate_systems::CoordinateType::Xpin => Self::Xpin , + // spatial_coordinate_systems::CoordinateType::Xpin => Self::Xpin, spatial_coordinate_systems::CoordinateType::Plus => Self::Plus, } } @@ -79,7 +79,7 @@ impl EncodedAddress { .as_ref() .map_err(|e| e.to_string())?, ) - .map_err(|()| String::from("Could not convert xpin to address")) + .map_err(|e| e.to_string()) } /// Get an encoded address from a latitude/longitude @@ -88,7 +88,7 @@ impl EncodedAddress { console_error_panic_hook::set_once(); let src_coords = Coordinate::from_str(i) - .map_err(|()| format!("Could not parse str as a coordinate {i:?}"))?; + .map_err(|e| format!("Could not parse str {i:?} as a coordinate {e:?}"))?; // TODO: Remove the clone here let latlon = LatLon::from(&src_coords); @@ -98,7 +98,7 @@ impl EncodedAddress { .as_ref() .map_err(|e| e.to_string())?, ) - .map_err(|()| String::from("Could not convert xpin to address"))?; + .map_err(|e| e.to_string())?; ret.src_coords_repr = src_coords.to_string(); ret.src_coords_type = src_coords.get_type().into(); @@ -114,22 +114,23 @@ impl EncodedAddress { .as_ref() .map_err(|e| e.to_string())?, ) - .map_err(|()| String::from("Could not convert xpin to address")) + .map_err(|e| e.to_string()) } } impl TryFrom<&'_ Address<'_>> for EncodedAddress { - type Error = (); + type Error = String; fn try_from(addr: &Address) -> Result { console_error_panic_hook::set_once(); let (lat, lon) = addr.as_lat_lon(); - let src_coords = Coordinate::from_str(&format!("{}, {}", lat, lon))?; + // TODO: Do not allocate a string here + let src_coords = + Coordinate::from_str(&format!("{}, {}", lat, lon)).map_err(|e| e.to_string())?; Ok(Self { address: addr.to_string(), - // TODO: Do not use formatting here lat_lon: Box::new([lat, lon]), src_coords_repr: src_coords.to_string(), src_coords_type: src_coords.get_type().into(),