Fix more tests

This commit is contained in:
Austen Adler 2023-03-19 23:52:27 -04:00
parent 88fb2e0770
commit 670fbf31cb
2 changed files with 73 additions and 10 deletions

View File

@ -35,6 +35,7 @@ impl Direction {
}
}
#[derive(Debug, PartialEq)]
pub enum Coordinate {
DD(dd::Coordinate),
DMS(dms::Coordinate),
@ -63,11 +64,11 @@ impl Coordinate {
}
}
impl TryInto<LatLon> for Coordinate {
impl TryFrom<Coordinate> for LatLon {
type Error = ();
fn try_into(self) -> Result<LatLon, Self::Error> {
match self {
fn try_from(value: Coordinate) -> Result<LatLon, Self::Error> {
match value {
Coordinate::DD(dd) => dd.try_into().or(Err(())),
Coordinate::DMM(dm) => dm.try_into(),
Coordinate::DMS(dms) => dms.try_into(),
@ -103,6 +104,31 @@ impl From<plus::Coordinate> for Coordinate {
}
}
impl TryFrom<Coordinate> for dms::Coordinate {
type Error = ();
fn try_from(c: Coordinate) -> Result<Self, Self::Error> {
dms::Coordinate::try_from(LatLon::try_from(c)?)
}
}
impl TryFrom<Coordinate> for dmm::Coordinate {
type Error = ();
fn try_from(c: Coordinate) -> Result<Self, Self::Error> {
dmm::Coordinate::try_from(LatLon::try_from(c)?).or(Err(()))
}
}
impl TryFrom<Coordinate> for utm::Coordinate {
type Error = ();
fn try_from(c: Coordinate) -> Result<Self, Self::Error> {
utm::Coordinate::try_from(LatLon::try_from(c)?)
}
}
impl TryFrom<Coordinate> for plus::Coordinate {
type Error = ();
fn try_from(c: Coordinate) -> Result<Self, Self::Error> {
plus::Coordinate::try_from(LatLon::try_from(c)?).or(Err(()))
}
}
impl FromStr for Coordinate {
type Err = ();
@ -110,3 +136,39 @@ impl FromStr for Coordinate {
Self::parse(i).map_err(|_| ()).map(|(_, ret)| ret)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_general() {
assert_eq!(
Coordinate::from_str("0.000000 100.500278"),
Ok(Coordinate::DD(dd::Coordinate {
lat: 0.0,
lon: 100.500278
}))
);
assert_eq!(
Coordinate::from_str("47N 666962.588 0.000"),
Ok(Coordinate::UTM(utm::Coordinate {
zone_num: 47,
zone_letter: 'N',
easting: 666962.588,
northing: 0.0
}))
);
assert_eq!(
dms::Coordinate::try_from(Coordinate::from_str("0.000000 100.500278").unwrap()),
dms::Coordinate::try_from(Coordinate::from_str("0.000000 100.500278").unwrap())
)
// assert_eq!(
// LatLon::try_from(Coordinate::from_str("0.000000 100.500278").unwrap()),
// LatLon::try_from(Coordinate::from_str("47N 666962.588 0.000").unwrap())
// )
}
}

View File

@ -1,16 +1,16 @@
use crate::{
common::{parse_direction, parse_f64}, LatLon,
common::{parse_direction, parse_f64},
LatLon,
};
use nom::{
bytes::complete::{take},
bytes::complete::take,
character::complete::{self, space0, space1},
combinator::{map_opt, opt},
sequence::{tuple},
sequence::tuple,
IResult,
};
use std::str::FromStr;
#[derive(PartialEq, Debug)]
pub struct Coordinate {
pub zone_num: u8,
@ -27,7 +27,7 @@ impl Coordinate {
/// assert!(Coordinate::parse("10S 706832mE 4344683N").is_ok());
/// assert!(Coordinate::parse("10S 706832E 4344683N").is_ok());
/// assert!(Coordinate::parse("10S706832mE 4344683mN").is_err());
/// assert!(Coordinate::parse("10S 706832mE 4344683m").is_err());
/// assert!(Coordinate::parse("10S 706832mE 4344683m").is_ok());
/// ```
pub fn parse(i: &str) -> IResult<&str, Self> {
map_opt(
@ -39,11 +39,12 @@ impl Coordinate {
parse_f64,
// TODO: Can there be spaces around the m here or no?
opt(complete::char('m')),
parse_direction,
// TODO: Should I allow a direction here nor no
opt(parse_direction),
space1,
parse_f64,
opt(complete::char('m')),
parse_direction,
opt(parse_direction),
)),
|(
zone_num,