Organize parsers
This commit is contained in:
parent
cda11079e4
commit
21a88e6cb8
@ -15,16 +15,18 @@ use std::str::FromStr;
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Coordinate(pub f64, pub f64);
|
||||
|
||||
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
tuple((double, optional_separator(','), double)),
|
||||
|(ns, _, ew)| {
|
||||
// Ensure this is a north/south then east/west direction
|
||||
if (-90_f64..=90_f64).contains(&ns) && (-180_f64..=180_f64).contains(&ew) {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
impl Coordinate {
|
||||
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
tuple((double, optional_separator(','), double)),
|
||||
|(ns, _, ew)| {
|
||||
// Ensure this is a north/south then east/west direction
|
||||
if (-90_f64..=90_f64).contains(&ns) && (-180_f64..=180_f64).contains(&ew) {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,21 @@ use std::str::FromStr;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Coordinate(pub DMM, pub DMM);
|
||||
impl Coordinate {
|
||||
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
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() {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct DMM {
|
||||
@ -22,38 +37,26 @@ pub struct DMM {
|
||||
pub direction: Direction,
|
||||
}
|
||||
|
||||
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
tuple((parse, optional_separator(','), parse)),
|
||||
|(ns, _, ew)| {
|
||||
// Ensure this is a north/south then east/west direction
|
||||
if ns.direction.is_lat() && ew.direction.is_lon() {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
|
||||
pub fn parse(i: &str) -> IResult<&str, DMM> {
|
||||
map(
|
||||
tuple((
|
||||
// Degrees
|
||||
complete::i16,
|
||||
optional_separator('°'),
|
||||
// Minutes
|
||||
double,
|
||||
optional_separator('\''),
|
||||
// Direction
|
||||
parse_direction,
|
||||
)),
|
||||
|(degrees, (), minutes, (), direction)| DMM {
|
||||
degrees,
|
||||
minutes,
|
||||
direction,
|
||||
},
|
||||
)(i)
|
||||
impl DMM {
|
||||
pub fn parse(i: &str) -> IResult<&str, DMM> {
|
||||
map(
|
||||
tuple((
|
||||
// Degrees
|
||||
complete::i16,
|
||||
optional_separator('°'),
|
||||
// Minutes
|
||||
double,
|
||||
optional_separator('\''),
|
||||
// Direction
|
||||
parse_direction,
|
||||
)),
|
||||
|(degrees, (), minutes, (), direction)| DMM {
|
||||
degrees,
|
||||
minutes,
|
||||
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,42 +15,20 @@ use std::str::FromStr;
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Coordinate(pub DMS, pub DMS);
|
||||
|
||||
pub fn parse_coordinate(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
tuple((parse, optional_separator(','), parse)),
|
||||
|(ns, _, ew)| {
|
||||
// Ensure this is a north/south then east/west direction
|
||||
if ns.direction.is_lat() && ew.direction.is_lon() {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
|
||||
pub fn parse(i: &str) -> IResult<&str, DMS> {
|
||||
map(
|
||||
tuple((
|
||||
// Degrees
|
||||
complete::i16,
|
||||
optional_separator('°'),
|
||||
// Minutes
|
||||
complete::i16,
|
||||
optional_separator('\''),
|
||||
// Seconds
|
||||
double,
|
||||
optional_separator('"'),
|
||||
// Direction
|
||||
parse_direction,
|
||||
)),
|
||||
|(degrees, (), minutes, (), seconds, (), direction)| DMS {
|
||||
degrees,
|
||||
minutes,
|
||||
seconds,
|
||||
direction,
|
||||
},
|
||||
)(i)
|
||||
impl Coordinate {
|
||||
pub fn parse(i: &str) -> IResult<&str, Coordinate> {
|
||||
map_opt(
|
||||
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() {
|
||||
Some(Coordinate(ns, ew))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
@ -61,6 +39,31 @@ pub struct DMS {
|
||||
pub direction: Direction,
|
||||
}
|
||||
|
||||
impl DMS {
|
||||
pub fn parse(i: &str) -> IResult<&str, DMS> {
|
||||
map(
|
||||
tuple((
|
||||
// Degrees
|
||||
complete::i16,
|
||||
optional_separator('°'),
|
||||
// Minutes
|
||||
complete::i16,
|
||||
optional_separator('\''),
|
||||
// Seconds
|
||||
double,
|
||||
optional_separator('"'),
|
||||
// Direction
|
||||
parse_direction,
|
||||
)),
|
||||
|(degrees, (), minutes, (), seconds, (), direction)| DMS {
|
||||
degrees,
|
||||
minutes,
|
||||
seconds,
|
||||
direction,
|
||||
},
|
||||
)(i)
|
||||
}
|
||||
}
|
||||
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