diff --git a/spatial-coordinate-systems/src/dd.rs b/spatial-coordinate-systems/src/dd.rs index 4041c47..970e94d 100644 --- a/spatial-coordinate-systems/src/dd.rs +++ b/spatial-coordinate-systems/src/dd.rs @@ -15,16 +15,18 @@ use std::str::FromStr; #[derive(PartialEq, Debug)] pub struct Coordinate(pub f64, pub f64); -pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> { - map_opt( - tuple((double, optional_separator(','), double)), - |(ns, _, ew)| { - // Ensure this is a north/south then east/west direction - if (-90_f64..=90_f64).contains(&ns) && (-180_f64..=180_f64).contains(&ew) { - Some(Coordinate(ns, ew)) - } else { - None - } - }, - )(i) +impl Coordinate { + pub fn parse(i: &str) -> IResult<&str, Coordinate> { + map_opt( + tuple((double, optional_separator(','), double)), + |(ns, _, ew)| { + // Ensure this is a north/south then east/west direction + if (-90_f64..=90_f64).contains(&ns) && (-180_f64..=180_f64).contains(&ew) { + Some(Coordinate(ns, ew)) + } else { + None + } + }, + )(i) + } } diff --git a/spatial-coordinate-systems/src/dmm.rs b/spatial-coordinate-systems/src/dmm.rs index d59798f..4488c3d 100644 --- a/spatial-coordinate-systems/src/dmm.rs +++ b/spatial-coordinate-systems/src/dmm.rs @@ -14,6 +14,21 @@ use std::str::FromStr; #[derive(PartialEq, Debug)] pub struct Coordinate(pub DMM, pub DMM); +impl Coordinate { + pub fn parse(i: &str) -> IResult<&str, Coordinate> { + map_opt( + tuple((DMM::parse, optional_separator(','), DMM::parse)), + |(ns, _, ew)| { + // Ensure this is a north/south then east/west direction + if ns.direction.is_lat() && ew.direction.is_lon() { + Some(Coordinate(ns, ew)) + } else { + None + } + }, + )(i) + } +} #[derive(PartialEq, Debug)] pub struct DMM { @@ -22,38 +37,26 @@ pub struct DMM { pub direction: Direction, } -pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> { - map_opt( - tuple((parse, optional_separator(','), parse)), - |(ns, _, ew)| { - // Ensure this is a north/south then east/west direction - if ns.direction.is_lat() && ew.direction.is_lon() { - Some(Coordinate(ns, ew)) - } else { - None - } - }, - )(i) -} - -pub fn parse(i: &str) -> IResult<&str, DMM> { - map( - tuple(( - // Degrees - complete::i16, - optional_separator('°'), - // Minutes - double, - optional_separator('\''), - // Direction - parse_direction, - )), - |(degrees, (), minutes, (), direction)| DMM { - degrees, - minutes, - direction, - }, - )(i) +impl DMM { + pub fn parse(i: &str) -> IResult<&str, DMM> { + map( + tuple(( + // Degrees + complete::i16, + optional_separator('°'), + // Minutes + double, + optional_separator('\''), + // Direction + parse_direction, + )), + |(degrees, (), minutes, (), direction)| DMM { + degrees, + minutes, + direction, + }, + )(i) + } } impl FromStr for DMM { @@ -81,6 +84,6 @@ impl FromStr for DMM { /// }); /// ``` fn from_str(i: &str) -> Result { - parse(i).map_err(|_| ()).map(|(_, ret)| ret) + DMM::parse(i).map_err(|_| ()).map(|(_, ret)| ret) } } diff --git a/spatial-coordinate-systems/src/dms.rs b/spatial-coordinate-systems/src/dms.rs index 1d6fafc..54dc049 100644 --- a/spatial-coordinate-systems/src/dms.rs +++ b/spatial-coordinate-systems/src/dms.rs @@ -15,42 +15,20 @@ use std::str::FromStr; #[derive(PartialEq, Debug)] pub struct Coordinate(pub DMS, pub DMS); -pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> { - map_opt( - tuple((parse, optional_separator(','), parse)), - |(ns, _, ew)| { - // Ensure this is a north/south then east/west direction - if ns.direction.is_lat() && ew.direction.is_lon() { - Some(Coordinate(ns, ew)) - } else { - None - } - }, - )(i) -} - -pub fn parse(i: &str) -> IResult<&str, DMS> { - map( - tuple(( - // Degrees - complete::i16, - optional_separator('°'), - // Minutes - complete::i16, - optional_separator('\''), - // Seconds - double, - optional_separator('"'), - // Direction - parse_direction, - )), - |(degrees, (), minutes, (), seconds, (), direction)| DMS { - degrees, - minutes, - seconds, - direction, - }, - )(i) +impl Coordinate { + pub fn parse(i: &str) -> IResult<&str, Coordinate> { + map_opt( + tuple((DMS::parse, optional_separator(','), DMS::parse)), + |(ns, _, ew)| { + // Ensure this is a north/south then east/west direction + if ns.direction.is_lat() && ew.direction.is_lon() { + Some(Coordinate(ns, ew)) + } else { + None + } + }, + )(i) + } } #[derive(PartialEq, Debug)] @@ -61,6 +39,31 @@ pub struct DMS { pub direction: Direction, } +impl DMS { + pub fn parse(i: &str) -> IResult<&str, DMS> { + map( + tuple(( + // Degrees + complete::i16, + optional_separator('°'), + // Minutes + complete::i16, + optional_separator('\''), + // Seconds + double, + optional_separator('"'), + // Direction + parse_direction, + )), + |(degrees, (), minutes, (), seconds, (), direction)| DMS { + degrees, + minutes, + seconds, + direction, + }, + )(i) + } +} impl FromStr for DMS { type Err = (); @@ -88,6 +91,6 @@ impl FromStr for DMS { /// }); /// ``` fn from_str(i: &str) -> Result { - parse(i).map_err(|_| ()).map(|(_, ret)| ret) + DMS::parse(i).map_err(|_| ()).map(|(_, ret)| ret) } } diff --git a/spatial-coordinate-systems/src/lib.rs b/spatial-coordinate-systems/src/lib.rs index 62ea435..4789549 100644 --- a/spatial-coordinate-systems/src/lib.rs +++ b/spatial-coordinate-systems/src/lib.rs @@ -65,9 +65,9 @@ impl FromStr for Coordinate { tuple(( space0, alt(( - map(dd::parse_coordinate, Coordinate::DD), - map(dms::parse_coordinate, Coordinate::DMS), - map(dmm::parse_coordinate, Coordinate::DM), + map(dd::Coordinate::parse, Coordinate::DD), + map(dms::Coordinate::parse, Coordinate::DMS), + map(dmm::Coordinate::parse, Coordinate::DM), // map(utm::parse_coordinate, Coordinate::UTM), // map(plus::parse_coordinate, Coordinate::PLUS), )),