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)]
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(
tuple((double, optional_separator(','), double)),
|(ns, _, ew)| {
@ -28,3 +29,4 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
},
)(i)
}
}

View File

@ -14,17 +14,10 @@ use std::str::FromStr;
#[derive(PartialEq, Debug)]
pub struct Coordinate(pub DMM, pub DMM);
#[derive(PartialEq, Debug)]
pub struct DMM {
pub degrees: i16,
pub minutes: f64,
pub direction: Direction,
}
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
impl Coordinate {
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
map_opt(
tuple((parse, optional_separator(','), parse)),
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() {
@ -35,7 +28,16 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
},
)(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> {
map(
tuple((
@ -55,6 +57,7 @@ pub fn parse(i: &str) -> IResult<&str, DMM> {
},
)(i)
}
}
impl FromStr for DMM {
type Err = ();
@ -81,6 +84,6 @@ impl FromStr for DMM {
/// });
/// ```
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)]
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(
tuple((parse, optional_separator(','), parse)),
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() {
@ -28,7 +29,17 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
},
)(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> {
map(
tuple((
@ -52,15 +63,7 @@ pub fn parse(i: &str) -> IResult<&str, DMS> {
},
)(i)
}
#[derive(PartialEq, Debug)]
pub struct DMS {
pub degrees: i16,
pub minutes: i16,
pub seconds: f64,
pub direction: Direction,
}
impl FromStr for DMS {
type Err = ();
@ -88,6 +91,6 @@ impl FromStr for DMS {
/// });
/// ```
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((
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),
)),