use std::fmt;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use abstutil::{deserialize_usize, serialize_usize};
use geom::Time;
use crate::{LaneID, Map, Path, PathConstraints, PathRequest, Position, RoadID};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TransitStopID {
pub road: RoadID,
pub(crate) idx: usize,
}
impl fmt::Display for TransitStopID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TransitStopID({0}, {1})", self.road, self.idx)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TransitRouteID(
#[serde(
serialize_with = "serialize_usize",
deserialize_with = "deserialize_usize"
)]
pub usize,
);
impl fmt::Display for TransitRouteID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TransitRoute #{}", self.0)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TransitStop {
pub id: TransitStopID,
pub name: String,
pub gtfs_id: String,
pub driving_pos: Position,
pub sidewalk_pos: Position,
pub is_train_stop: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TransitRoute {
pub id: TransitRouteID,
pub long_name: String,
pub short_name: String,
pub gtfs_id: String,
pub stops: Vec<TransitStopID>,
pub start: LaneID,
pub end_border: Option<LaneID>,
pub route_type: PathConstraints,
pub spawn_times: Vec<Time>,
pub orig_spawn_times: Vec<Time>,
}
impl TransitRoute {
fn all_path_requests(&self, map: &Map) -> Vec<PathRequest> {
let mut steps = vec![PathRequest::vehicle(
Position::start(self.start),
map.get_ts(self.stops[0]).driving_pos,
self.route_type,
)];
for pair in self.stops.windows(2) {
steps.push(PathRequest::vehicle(
map.get_ts(pair[0]).driving_pos,
map.get_ts(pair[1]).driving_pos,
self.route_type,
));
}
let last_stop_pos = map.get_ts(*self.stops.last().unwrap()).driving_pos;
if let Some(end) = self.end_border {
steps.push(PathRequest::vehicle(
last_stop_pos,
Position::end(end, map),
self.route_type,
));
} else {
steps.push(PathRequest::vehicle(
last_stop_pos,
Position::end(last_stop_pos.lane(), map),
self.route_type,
));
}
steps
}
pub fn all_paths(&self, map: &Map) -> Result<Vec<Path>> {
let mut paths = Vec::new();
for req in self.all_path_requests(map) {
if req.start.lane().road == req.end.lane().road
&& req.start.dist_along() > req.end.dist_along()
{
bail!(
"Two consecutive stops are on the same road, but they travel backwards: {}",
req
);
}
let path = map.pathfind(req)?;
if path.is_empty() {
bail!("Empty path between stops: {}", path.get_req());
}
paths.push(path);
}
for pair in paths.windows(2) {
if pair[0].get_req().end != pair[1].get_req().start {
bail!(
"Transit route will warp from {} to {}",
pair[0].get_req().end,
pair[1].get_req().start
);
}
}
Ok(paths)
}
pub fn plural_noun(&self) -> &'static str {
if self.route_type == PathConstraints::Bus {
"buses"
} else {
"trains"
}
}
}