Organize parsers
This commit is contained in:
parent
cda11079e4
commit
21a88e6cb8
@ -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)| {
|
||||
@ -27,4 +28,5 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
@ -34,9 +27,18 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
}
|
||||
},
|
||||
)(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(
|
||||
tuple((
|
||||
// Degrees
|
||||
@ -54,6 +56,7 @@ pub fn parse(i: &str) -> IResult<&str, DMM> {
|
||||
direction,
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DMM {
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
@ -27,9 +28,19 @@ pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
}
|
||||
},
|
||||
)(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(
|
||||
tuple((
|
||||
// Degrees
|
||||
@ -51,16 +62,8 @@ pub fn parse(i: &str) -> IResult<&str, DMS> {
|
||||
direction,
|
||||
},
|
||||
)(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)
|
||||
}
|
||||
}
|
||||
|
@ -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),
|
||||
)),
|
||||
|
Loading…
Reference in New Issue
Block a user