Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Handle Maxspeed Bugs Gracefully (#190)
Browse files Browse the repository at this point in the history
* Handle Maxspeed Bugs Gracefully

* Passthrough error display
  • Loading branch information
droogmic authored May 8, 2022
1 parent baa7a5e commit a22100a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
5 changes: 2 additions & 3 deletions data/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,8 @@
- way_id: 102917976
description: Road with impossible speed limit
rust:
false
#expect_warnings: true
#separator: true
expect_warnings: true
separator: false
tags:
bicycle: "designated"
cycleway: "lane"
Expand Down
28 changes: 23 additions & 5 deletions osm2lanes/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,22 @@ pub enum SpeedError {
Empty,
Parse(std::num::ParseFloatError),
UnknownUnit(String),
OutOfRange,
}

impl std::fmt::Display for SpeedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => write!(f, "empty"),
Self::Parse(e) => e.fmt(f),
Self::UnknownUnit(unit) => write!(f, "unknown unit '{unit}'"),
Self::OutOfRange => write!(f, "out of range"),
}
}
}

impl std::error::Error for SpeedError {}

impl std::convert::From<std::num::ParseFloatError> for SpeedError {
fn from(e: std::num::ParseFloatError) -> Self {
SpeedError::Parse(e)
Expand All @@ -89,12 +103,16 @@ impl std::str::FromStr for Speed {
if s.is_empty() {
return Err(SpeedError::Empty);
}
match s.split_once(' ') {
None => Ok(Self::Kph(s.parse()?)),
Some((s, "mph")) => Ok(Self::Mph(s.parse()?)),
Some((s, "knots")) => Ok(Self::Knots(s.parse()?)),
Some((_, unit)) => Err(SpeedError::UnknownUnit(unit.to_owned())),
let speed = match s.split_once(' ') {
None => Self::Kph(s.parse()?),
Some((s, "mph")) => Self::Mph(s.parse()?),
Some((s, "knots")) => Self::Knots(s.parse()?),
Some((_, unit)) => return Err(SpeedError::UnknownUnit(unit.to_owned())),
};
if speed.kph() < 0_f64 || speed.kph() > 300_f64 {
return Err(SpeedError::OutOfRange);
}
Ok(speed)
}
}

Expand Down
7 changes: 5 additions & 2 deletions osm2lanes/src/transform/tags_to_lanes/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,11 @@ impl RoadBuilder {
const MAXSPEED: TagKey = TagKey::from("maxspeed");
let max_speed = match tags.get(MAXSPEED).map(str::parse::<Speed>).transpose() {
Ok(max_speed) => max_speed,
Err(_e) => {
warnings.push(TagsToLanesMsg::unsupported_tags(tags.subset(&[MAXSPEED])));
Err(e) => {
warnings.push(TagsToLanesMsg::unsupported(
&e.to_string(),
tags.subset(&[MAXSPEED]),
));
None
},
};
Expand Down

0 comments on commit a22100a

Please sign in to comment.