From 6f33c9a6daa30253a8890e628644d3bdb9ea4797 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 10 Oct 2018 16:05:32 -0700 Subject: [PATCH] failure crate actually sucks for context, rolling my own simple thing --- abstutil/src/error.rs | 40 ++++++++++++++++++++++++++++++++++++++++ abstutil/src/lib.rs | 2 ++ map_model/Cargo.toml | 1 - map_model/src/lib.rs | 14 -------------- map_model/src/map.rs | 9 +++++---- map_model/src/road.rs | 34 ++++++++++++---------------------- 6 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 abstutil/src/error.rs diff --git a/abstutil/src/error.rs b/abstutil/src/error.rs new file mode 100644 index 0000000000..edf7028546 --- /dev/null +++ b/abstutil/src/error.rs @@ -0,0 +1,40 @@ +use std::{error, fmt}; + +pub struct Error { + message: String, + context: Vec, +} + +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, "\n{}\n", self.message)?; + for c in &self.context { + write!(f, " - {}\n", c)?; + } + write!(f, "\n")?; + 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 {} diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 16698dc9df..615f785ff5 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -5,11 +5,13 @@ extern crate serde_json; mod abst_multimap; mod clone; +mod error; mod io; mod time; pub use abst_multimap::MultiMap; pub use clone::Cloneable; +pub use error::Error; pub use io::{ deserialize_btreemap, deserialize_multimap, list_all_objects, load_all_objects, read_binary, read_json, save_object, serialize_btreemap, serialize_multimap, to_json, write_binary, diff --git a/map_model/Cargo.toml b/map_model/Cargo.toml index 5bdf0a711a..112098aef8 100644 --- a/map_model/Cargo.toml +++ b/map_model/Cargo.toml @@ -6,7 +6,6 @@ authors = ["Dustin Carlino "] [dependencies] abstutil = { path = "../abstutil" } dimensioned = { git = "https://github.com/paholg/dimensioned", rev = "0e1076ebfa5128d1ee544bdc9754c948987b6fe3", features = ["serde"] } -failure = "0.1.2" flame = "0.2.2" geo = "0.9.1" geom = { path = "../geom" } diff --git a/map_model/src/lib.rs b/map_model/src/lib.rs index f30c38f804..8b8234cf1f 100644 --- a/map_model/src/lib.rs +++ b/map_model/src/lib.rs @@ -1,7 +1,5 @@ extern crate abstutil; extern crate dimensioned; -#[macro_use] -extern crate failure; extern crate flame; extern crate geo; extern crate geom; @@ -48,15 +46,3 @@ pub use traversable::Traversable; pub use turn::{Turn, TurnID}; pub const LANE_THICKNESS: f64 = 2.5; - -#[derive(Debug, Fail)] -#[fail(display = "{}", reason)] -pub struct MapError { - reason: String, -} - -impl MapError { - pub fn new(reason: String) -> MapError { - MapError { reason } - } -} diff --git a/map_model/src/map.rs b/map_model/src/map.rs index e24764470d..83a254b2bd 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -1,13 +1,14 @@ // Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0 use abstutil; +use abstutil::Error; use edits::RoadEdits; -use failure::{Error, ResultExt}; use flame; use geom::{Bounds, HashablePt2D, PolyLine, Pt2D}; use make; use raw_data; use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::io; use std::path; use { Area, AreaID, Building, BuildingID, BusRoute, BusStop, BusStopID, Intersection, IntersectionID, @@ -34,7 +35,7 @@ pub struct Map { } impl Map { - pub fn new(path: &str, road_edits: RoadEdits) -> Result { + pub fn new(path: &str, road_edits: RoadEdits) -> Result { // TODO I think I want something a bit different than flame: // - Print as each phase occurs // - Print with nicely formatted durations @@ -384,7 +385,7 @@ impl Map { let road = self.get_parent(sidewalk); road.find_parking_lane(sidewalk) .and_then(|parking| road.find_driving_lane(parking)) - .context(format!("get_driving_lane_from_bldg({})", bldg)) + .map_err(|e| e.context(format!("get_driving_lane_from_bldg({})", bldg))) } pub fn get_sidewalk_from_driving_lane(&self, driving: LaneID) -> Result { @@ -395,7 +396,7 @@ impl Map { } road.find_parking_lane(driving) .and_then(|parking| road.find_sidewalk(parking)) - .context(format!("get_sidewalk_from_driving_lane({})", driving)) + .map_err(|e| e.context(format!("get_sidewalk_from_driving_lane({})", driving))) } pub fn get_driving_lane_from_parking(&self, parking: LaneID) -> Result { diff --git a/map_model/src/road.rs b/map_model/src/road.rs index aa825097d5..448282c446 100644 --- a/map_model/src/road.rs +++ b/map_model/src/road.rs @@ -1,9 +1,9 @@ +use abstutil::Error; use dimensioned::si; -use failure::Error; use geom::PolyLine; use std::collections::BTreeMap; use std::fmt; -use {LaneID, LaneType, MapError}; +use {LaneID, LaneType}; // TODO reconsider pub usize. maybe outside world shouldnt know. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] @@ -89,10 +89,10 @@ impl Road { .find(|pair| pair.1 == LaneType::Sidewalk) .map(|pair| pair.0) .ok_or_else(|| { - Error::from(MapError::new(format!( + Error::new(format!( "{} doesn't have sidewalk sibling", parking_or_driving - ))) + )) }) } @@ -102,12 +102,7 @@ impl Road { .iter() .find(|pair| pair.1 == LaneType::Driving) .map(|pair| pair.0) - .ok_or_else(|| { - Error::from(MapError::new(format!( - "{} doesn't have driving lane sibling", - parking - ))) - }) + .ok_or_else(|| Error::new(format!("{} doesn't have driving lane sibling", parking))) } // Handles intermediate parking and bus lanes and such @@ -145,10 +140,10 @@ impl Road { if this_side.len() == 1 && opposite[0].1 == LaneType::Driving { return Ok(opposite[0].0); } - bail!(MapError::new(format!( + Err(Error::new(format!( "Sidewalk {} doesn't have driving lane", sidewalk - ))); + ))) } pub fn find_parking_lane(&self, driving: LaneID) -> Result { @@ -157,12 +152,7 @@ impl Road { .iter() .find(|pair| pair.1 == LaneType::Parking) .map(|pair| pair.0) - .ok_or_else(|| { - Error::from(MapError::new(format!( - "{} doesn't have parking lane sibling", - driving - ))) - }) + .ok_or_else(|| Error::new(format!("{} doesn't have parking lane sibling", driving))) } pub fn get_opposite_lane(&self, lane: LaneID, lane_type: LaneType) -> Result { @@ -181,18 +171,18 @@ impl Road { if let Some(idx) = forwards.iter().position(|id| *id == lane) { return backwards.get(idx).map(|id| *id).ok_or_else(|| { - Error::from(MapError::new(format!( + Error::new(format!( "{} doesn't have opposite lane of type {:?}", lane, lane_type - ))) + )) }); } if let Some(idx) = backwards.iter().position(|id| *id == lane) { return forwards.get(idx).map(|id| *id).ok_or_else(|| { - Error::from(MapError::new(format!( + Error::new(format!( "{} doesn't have opposite lane of type {:?}", lane, lane_type - ))) + )) }); } panic!("{} doesn't contain {}", self.id, lane);