mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-28 12:12:00 +03:00
failure crate actually sucks for context, rolling my own simple thing
This commit is contained in:
parent
843742b708
commit
6f33c9a6da
40
abstutil/src/error.rs
Normal file
40
abstutil/src/error.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use std::{error, fmt};
|
||||
|
||||
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, "\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 {}
|
@ -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,
|
||||
|
@ -6,7 +6,6 @@ authors = ["Dustin Carlino <dabreegster@gmail.com>"]
|
||||
[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" }
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
@ -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<Map, Error> {
|
||||
pub fn new(path: &str, road_edits: RoadEdits) -> Result<Map, io::Error> {
|
||||
// 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<LaneID, Error> {
|
||||
@ -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<LaneID, Error> {
|
||||
|
@ -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<LaneID, Error> {
|
||||
@ -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<LaneID, Error> {
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user