From e22d15f381e0deef7ed4ad1f04c8c548b8841a11 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sun, 19 Mar 2023 23:18:09 -0400 Subject: [PATCH] Impl TryInto for all coordinate types --- spatial-coordinate-systems/src/lib.rs | 46 ++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/spatial-coordinate-systems/src/lib.rs b/spatial-coordinate-systems/src/lib.rs index e079c94..9bd46e1 100644 --- a/spatial-coordinate-systems/src/lib.rs +++ b/spatial-coordinate-systems/src/lib.rs @@ -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 for Coordinate { + type Error = (); + + fn try_into(self) -> Result { + 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 for Coordinate { + fn from(c: dd::Coordinate) -> Self { + Self::DD(c) + } +} +impl From for Coordinate { + fn from(c: dms::Coordinate) -> Self { + Self::DMS(c) + } +} +// impl From for Coordinate { +// fn from(c: dmm::Coordinate) -> Self { +// Self::DMM(c) +// } +// } +impl From for Coordinate { + fn from(c: utm::Coordinate) -> Self { + Self::UTM(c) + } +} +impl From for Coordinate { + fn from(c: plus::Coordinate) -> Self { + Self::Plus(c) + } +} + impl FromStr for Coordinate { type Err = ();