cleanup the old abstutil Error

This commit is contained in:
Dustin Carlino 2020-07-07 17:09:51 -07:00
parent d1fdcbbd59
commit 9a27f6d3dd
6 changed files with 16 additions and 76 deletions

View File

@ -1,46 +0,0 @@
use std::{error, fmt};
#[derive(PartialEq)]
pub struct Error {
message: String,
context: Vec<String>,
}
impl Error {
pub fn new(message: String) -> Error {
Error {
message,
context: Vec::new(),
}
}
pub fn context(mut self, msg: String) -> Error {
self.context.push(msg);
self
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)?;
for c in &self.context {
writeln!(f, "\n - {}", c)?;
}
Ok(())
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Do the same thing as the Display trait
write!(f, "{}", self)
}
}
impl error::Error for Error {}
impl std::convert::From<std::num::ParseFloatError> for Error {
fn from(err: std::num::ParseFloatError) -> Self {
Error::new(err.to_string())
}
}

View File

@ -1,7 +1,6 @@
mod cli;
mod clone;
mod collections;
mod error;
mod io;
mod logs;
mod random;
@ -13,7 +12,6 @@ pub use crate::collections::{
contains_duplicates, retain_btreemap, retain_btreeset, wraparound_get, Counter, MultiMap,
VecMap,
};
pub use crate::error::Error;
pub use crate::io::{
basename, deserialize_btreemap, deserialize_multimap, file_exists, find_next_file,
find_prev_file, list_all_objects, list_dir, load_all_objects, maybe_read_binary,

View File

@ -80,20 +80,17 @@ impl Duration {
}
// TODO This is NOT the inverse of Display!
pub fn parse(string: &str) -> Result<Duration, abstutil::Error> {
pub fn parse(string: &str) -> Result<Duration, Box<dyn std::error::Error>> {
let parts: Vec<&str> = string.split(':').collect();
if parts.is_empty() {
return Err(abstutil::Error::new(format!("Duration {}: no :'s", string)));
return Err(format!("Duration {}: no :'s", string).into());
}
let mut seconds: f64 = 0.0;
if parts.last().unwrap().contains('.') {
let last_parts: Vec<&str> = parts.last().unwrap().split('.').collect();
if last_parts.len() != 2 {
return Err(abstutil::Error::new(format!(
"Duration {}: no . in last part",
string
)));
return Err(format!("Duration {}: no . in last part", string).into());
}
seconds += last_parts[1].parse::<f64>()? / 10.0;
seconds += last_parts[0].parse::<f64>()?;
@ -112,10 +109,7 @@ impl Duration {
seconds += 3600.0 * parts[0].parse::<f64>()?;
Ok(Duration::seconds(seconds))
}
_ => Err(abstutil::Error::new(format!(
"Duration {}: weird number of parts",
string
))),
_ => Err(format!("Duration {}: weird number of parts", string).into()),
}
}

View File

@ -111,20 +111,17 @@ impl Time {
)
}
pub fn parse(string: &str) -> Result<Time, abstutil::Error> {
pub fn parse(string: &str) -> Result<Time, Box<dyn std::error::Error>> {
let parts: Vec<&str> = string.split(':').collect();
if parts.is_empty() {
return Err(abstutil::Error::new(format!("Time {}: no :'s", string)));
return Err(format!("Time {}: no :'s", string).into());
}
let mut seconds: f64 = 0.0;
if parts.last().unwrap().contains('.') {
let last_parts: Vec<&str> = parts.last().unwrap().split('.').collect();
if last_parts.len() != 2 {
return Err(abstutil::Error::new(format!(
"Time {}: no . in last part",
string
)));
return Err(format!("Time {}: no . in last part", string).into());
}
seconds += last_parts[1].parse::<f64>()? / 10.0;
seconds += last_parts[0].parse::<f64>()?;
@ -143,10 +140,7 @@ impl Time {
seconds += 3600.0 * parts[0].parse::<f64>()?;
Ok(Time::seconds_since_midnight(seconds))
}
_ => Err(abstutil::Error::new(format!(
"Time {}: weird number of parts",
string
))),
_ => Err(format!("Time {}: weird number of parts", string).into()),
}
}

View File

@ -5,7 +5,7 @@ use crate::{
ParkingLot, ParkingLotID, Path, PathConstraints, PathRequest, Position, Road, RoadID, Turn,
TurnGroupID, TurnID, TurnType,
};
use abstutil::{Error, Timer, Warn};
use abstutil::{Timer, Warn};
use geom::{Angle, Bounds, Distance, GPSBounds, Line, PolyLine, Polygon, Pt2D};
use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
@ -427,7 +427,11 @@ impl Map {
abstutil::write_binary(abstutil::path_map(&self.name), self);
}
pub fn find_closest_lane(&self, from: LaneID, types: Vec<LaneType>) -> Result<LaneID, Error> {
pub fn find_closest_lane(
&self,
from: LaneID,
types: Vec<LaneType>,
) -> Result<LaneID, Box<dyn std::error::Error>> {
self.get_parent(from).find_closest_lane(from, types)
}

View File

@ -1,6 +1,5 @@
use crate::raw::{OriginalRoad, RestrictionType};
use crate::{osm, BusStopID, IntersectionID, LaneID, LaneType, Map, PathConstraints, Zone};
use abstutil::Error;
use enumset::EnumSet;
use geom::{Distance, PolyLine, Polygon, Speed};
use serde::{Deserialize, Serialize};
@ -234,7 +233,7 @@ impl Road {
&self,
from: LaneID,
types: Vec<LaneType>,
) -> Result<LaneID, Error> {
) -> Result<LaneID, Box<dyn std::error::Error>> {
let lane_types: HashSet<LaneType> = types.into_iter().collect();
let (dir, from_idx) = self.dir_and_offset(from);
let mut list = self.children(dir);
@ -252,10 +251,7 @@ impl Road {
{
Ok(lane)
} else {
Err(Error::new(format!(
"{} isn't near a {:?} lane",
from, lane_types
)))
Err(format!("{} isn't near a {:?} lane", from, lane_types).into())
}
}