use crate::schedule::Schedule;
use crate::stop::StopId;
pub type RouteId = u64;
#[derive(Debug, Clone)]
pub struct Route {
pub id: RouteId,
pub name: String,
pub stops: Vec<StopId>,
pub inter_stop_times: Vec<f64>,
pub schedule: Schedule,
}
impl Route {
pub fn new(
id: RouteId,
name: impl Into<String>,
stops: Vec<StopId>,
inter_stop_times: Vec<f64>,
schedule: Schedule,
) -> Self {
assert!(stops.len() >= 2, "route needs at least two stops");
assert_eq!(
inter_stop_times.len(),
stops.len() - 1,
"inter_stop_times length must be stops.len() - 1"
);
Self {
id,
name: name.into(),
stops,
inter_stop_times,
schedule,
}
}
pub fn scheduled_runtime(&self) -> f64 {
self.inter_stop_times.iter().sum()
}
pub fn index_of(&self, stop_id: StopId) -> Option<usize> {
self.stops.iter().position(|s| *s == stop_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn route_runtime_sums_inter_stop() {
let r = Route::new(
42,
"N17",
vec![1, 2, 3, 4],
vec![60.0, 90.0, 75.0],
Schedule::fixed_headway(600.0),
);
assert_eq!(r.scheduled_runtime(), 225.0);
assert_eq!(r.index_of(3), Some(2));
}
#[test]
#[should_panic]
fn inter_stop_length_mismatch_panics() {
Route::new(
1,
"bad",
vec![1, 2, 3],
vec![60.0], Schedule::fixed_headway(600.0),
);
}
}