Impl TryInto for all coordinate types

This commit is contained in:
Austen Adler 2023-03-19 23:18:09 -04:00
parent c9cc544802
commit e22d15f381

View File

@ -8,8 +8,6 @@ pub mod dms;
pub mod plus;
pub mod utm;
// use dmm::DMM;
use dms::DMS;
use nom::{
branch::alt,
character::complete::space0,
@ -42,7 +40,7 @@ impl Direction {
pub enum Coordinate {
DD(dd::Coordinate),
DMS(dms::Coordinate),
// DM(dmm::Coordinate),
// DMM(dmm::Coordinate),
UTM(utm::Coordinate),
Plus(plus::Coordinate),
}
@ -55,7 +53,7 @@ impl Coordinate {
alt((
map(dd::Coordinate::parse, Coordinate::DD),
map(dms::Coordinate::parse, Coordinate::DMS),
// map(dmm::Coordinate::parse, Coordinate::DM),
// map(dmm::Coordinate::parse, Coordinate::DMM),
map(utm::Coordinate::parse, Coordinate::UTM),
map(plus::Coordinate::parse, Coordinate::Plus),
)),
@ -67,6 +65,46 @@ impl Coordinate {
}
}
impl TryInto<LatLon> for Coordinate {
type Error = ();
fn try_into(self) -> Result<LatLon, Self::Error> {
match self {
Coordinate::DD(dd) => dd.try_into().or(Err(())),
// Coordinate::DMM(dm) => dm.try_into(),
Coordinate::DMS(dms) => dms.try_into(),
Coordinate::UTM(utm) => utm.try_into(),
Coordinate::Plus(plus) => plus.try_into(),
}
}
}
impl From<dd::Coordinate> for Coordinate {
fn from(c: dd::Coordinate) -> Self {
Self::DD(c)
}
}
impl From<dms::Coordinate> for Coordinate {
fn from(c: dms::Coordinate) -> Self {
Self::DMS(c)
}
}
// impl From<dmm::Coordinate> for Coordinate {
// fn from(c: dmm::Coordinate) -> Self {
// Self::DMM(c)
// }
// }
impl From<utm::Coordinate> for Coordinate {
fn from(c: utm::Coordinate) -> Self {
Self::UTM(c)
}
}
impl From<plus::Coordinate> for Coordinate {
fn from(c: plus::Coordinate) -> Self {
Self::Plus(c)
}
}
impl FromStr for Coordinate {
type Err = ();