Organize parsers

This commit is contained in:
Austen Adler 2023-03-19 18:59:25 -04:00
parent cda11079e4
commit 21a88e6cb8
4 changed files with 93 additions and 85 deletions

View File

@ -15,7 +15,8 @@ use std::str::FromStr;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct Coordinate(pub f64, pub f64); pub struct Coordinate(pub f64, pub f64);
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> { impl Coordinate {
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
map_opt( map_opt(
tuple((double, optional_separator(','), double)), tuple((double, optional_separator(','), double)),
|(ns, _, ew)| { |(ns, _, ew)| {
@ -28,3 +29,4 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
}, },
)(i) )(i)
} }
}

View File

@ -14,17 +14,10 @@ use std::str::FromStr;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct Coordinate(pub DMM, pub DMM); pub struct Coordinate(pub DMM, pub DMM);
impl Coordinate {
#[derive(PartialEq, Debug)] pub fn parse(i: &str) -> IResult<&str, Coordinate> {
pub struct DMM {
pub degrees: i16,
pub minutes: f64,
pub direction: Direction,
}
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
map_opt( map_opt(
tuple((parse, optional_separator(','), parse)), tuple((DMM::parse, optional_separator(','), DMM::parse)),
|(ns, _, ew)| { |(ns, _, ew)| {
// Ensure this is a north/south then east/west direction // Ensure this is a north/south then east/west direction
if ns.direction.is_lat() && ew.direction.is_lon() { if ns.direction.is_lat() && ew.direction.is_lon() {
@ -35,7 +28,16 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
}, },
)(i) )(i)
} }
}
#[derive(PartialEq, Debug)]
pub struct DMM {
pub degrees: i16,
pub minutes: f64,
pub direction: Direction,
}
impl DMM {
pub fn parse(i: &str) -> IResult<&str, DMM> { pub fn parse(i: &str) -> IResult<&str, DMM> {
map( map(
tuple(( tuple((
@ -55,6 +57,7 @@ pub fn parse(i: &str) -> IResult<&str, DMM> {
}, },
)(i) )(i)
} }
}
impl FromStr for DMM { impl FromStr for DMM {
type Err = (); type Err = ();
@ -81,6 +84,6 @@ impl FromStr for DMM {
/// }); /// });
/// ``` /// ```
fn from_str(i: &str) -> Result<Self, Self::Err> { fn from_str(i: &str) -> Result<Self, Self::Err> {
parse(i).map_err(|_| ()).map(|(_, ret)| ret) DMM::parse(i).map_err(|_| ()).map(|(_, ret)| ret)
} }
} }

View File

@ -15,9 +15,10 @@ use std::str::FromStr;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct Coordinate(pub DMS, pub DMS); pub struct Coordinate(pub DMS, pub DMS);
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> { impl Coordinate {
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
map_opt( map_opt(
tuple((parse, optional_separator(','), parse)), tuple((DMS::parse, optional_separator(','), DMS::parse)),
|(ns, _, ew)| { |(ns, _, ew)| {
// Ensure this is a north/south then east/west direction // Ensure this is a north/south then east/west direction
if ns.direction.is_lat() && ew.direction.is_lon() { if ns.direction.is_lat() && ew.direction.is_lon() {
@ -28,7 +29,17 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
}, },
)(i) )(i)
} }
}
#[derive(PartialEq, Debug)]
pub struct DMS {
pub degrees: i16,
pub minutes: i16,
pub seconds: f64,
pub direction: Direction,
}
impl DMS {
pub fn parse(i: &str) -> IResult<&str, DMS> { pub fn parse(i: &str) -> IResult<&str, DMS> {
map( map(
tuple(( tuple((
@ -52,15 +63,7 @@ pub fn parse(i: &str) -> IResult<&str, DMS> {
}, },
)(i) )(i)
} }
#[derive(PartialEq, Debug)]
pub struct DMS {
pub degrees: i16,
pub minutes: i16,
pub seconds: f64,
pub direction: Direction,
} }
impl FromStr for DMS { impl FromStr for DMS {
type Err = (); type Err = ();
@ -88,6 +91,6 @@ impl FromStr for DMS {
/// }); /// });
/// ``` /// ```
fn from_str(i: &str) -> Result<Self, Self::Err> { fn from_str(i: &str) -> Result<Self, Self::Err> {
parse(i).map_err(|_| ()).map(|(_, ret)| ret) DMS::parse(i).map_err(|_| ()).map(|(_, ret)| ret)
} }
} }

View File

@ -65,9 +65,9 @@ impl FromStr for Coordinate {
tuple(( tuple((
space0, space0,
alt(( alt((
map(dd::parse_coordinate, Coordinate::DD), map(dd::Coordinate::parse, Coordinate::DD),
map(dms::parse_coordinate, Coordinate::DMS), map(dms::Coordinate::parse, Coordinate::DMS),
map(dmm::parse_coordinate, Coordinate::DM), map(dmm::Coordinate::parse, Coordinate::DM),
// map(utm::parse_coordinate, Coordinate::UTM), // map(utm::parse_coordinate, Coordinate::UTM),
// map(plus::parse_coordinate, Coordinate::PLUS), // map(plus::parse_coordinate, Coordinate::PLUS),
)), )),