Add all coordinate type

This commit is contained in:
Austen Adler 2023-03-25 00:41:27 -04:00
parent df0b688a3c
commit 67cf26202d
3 changed files with 59 additions and 11 deletions

View File

@ -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<LatLon> for Coordinates {
type Error = Error;
fn try_from(latlon: LatLon) -> Result<Self, Self::Error> {
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, Self::Err> {
Self::try_from(LatLon::from(&Coordinate::from_str(i)?))
}
}

View File

@ -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, Self::Err> {
Self::parse(i).map_err(|_| ()).map(|(_, ret)| ret)
Self::parse(i).map(|(_, ret)| ret).map_err(Into::into)
}
}

View File

@ -35,7 +35,7 @@ impl From<spatial_coordinate_systems::CoordinateType> 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<Self, Self::Error> {
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(),