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)| {
@ -27,4 +28,5 @@ 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() {
@ -34,9 +27,18 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
} }
}, },
)(i) )(i)
}
} }
pub fn parse(i: &str) -> IResult<&str, DMM> { #[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> {
map( map(
tuple(( tuple((
// Degrees // Degrees
@ -54,6 +56,7 @@ pub fn parse(i: &str) -> IResult<&str, DMM> {
direction, direction,
}, },
)(i) )(i)
}
} }
impl FromStr for DMM { impl FromStr for DMM {
@ -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() {
@ -27,9 +28,19 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
} }
}, },
)(i) )(i)
}
} }
pub fn parse(i: &str) -> IResult<&str, DMS> { #[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> {
map( map(
tuple(( tuple((
// Degrees // Degrees
@ -51,16 +62,8 @@ pub fn parse(i: &str) -> IResult<&str, DMS> {
direction, direction,
}, },
)(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),
)), )),