88 lines
3.0 KiB
Rust
88 lines
3.0 KiB
Rust
use std::collections::{HashMap, HashSet};
|
|
|
|
use crate::bahn_api::{
|
|
basic_types::station_ids::{DbStopId, StationEvaNumber, StationIdRef},
|
|
transport_modes::TransportModesSet,
|
|
};
|
|
|
|
pub struct Stations {
|
|
pub stations: HashMap<StationEvaNumber, Station>,
|
|
}
|
|
|
|
pub struct Station {
|
|
pub name: String,
|
|
pub db_station_id: DbStopId,
|
|
pub related_stations: HashSet<StationEvaNumber>,
|
|
pub lat_lon: Option<(f64, f64)>,
|
|
pub transport_modes: Option<TransportModesSet>,
|
|
pub has_been_changed: bool,
|
|
}
|
|
|
|
impl Stations {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
stations: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn insert(&mut self, eva_number: StationEvaNumber, mut station: Station) {
|
|
if let Some(prev) = self.stations.get_mut(&eva_number) {
|
|
// partial update: if we had more information than the new `station` has,
|
|
// keep parts of the old information, but wherever the new `station` has
|
|
// information, use the newer information.
|
|
#[allow(dead_code, unreachable_code)]
|
|
fn never_called() -> Station {
|
|
// NOTE: this is here in case the contents of `Station` change,
|
|
// as a reminder that the partial update must be adjusted too.
|
|
Station {
|
|
name: String::new(),
|
|
db_station_id: DbStopId(unreachable!()),
|
|
related_stations: HashSet::new(),
|
|
lat_lon: None,
|
|
transport_modes: None,
|
|
has_been_changed: true,
|
|
}
|
|
}
|
|
prev.has_been_changed = true;
|
|
prev.name = station.name;
|
|
prev.db_station_id = station.db_station_id;
|
|
for station in station.related_stations {
|
|
prev.related_stations.insert(station);
|
|
}
|
|
if let Some(v) = station.lat_lon {
|
|
prev.lat_lon = Some(v);
|
|
}
|
|
if let Some(v) = station.transport_modes {
|
|
prev.transport_modes = Some(v);
|
|
}
|
|
} else {
|
|
station.has_been_changed = true;
|
|
self.stations.insert(eva_number, station);
|
|
}
|
|
}
|
|
pub(super) fn insert_raw(&mut self, eva_number: StationEvaNumber, station: Station) {
|
|
self.stations.insert(eva_number, station);
|
|
}
|
|
|
|
pub fn get(&self, eva_number: &StationEvaNumber) -> Option<&Station> {
|
|
self.stations.get(eva_number)
|
|
}
|
|
pub fn get_mut(&mut self, eva_number: &StationEvaNumber) -> Option<&mut Station> {
|
|
self.stations.get_mut(eva_number)
|
|
}
|
|
}
|
|
|
|
impl<'a> From<(&'a StationEvaNumber, &'a Station)> for StationIdRef<'a> {
|
|
fn from(value: (&'a StationEvaNumber, &'a Station)) -> Self {
|
|
(value.0, &value.1.db_station_id).into()
|
|
}
|
|
}
|
|
impl<'a> From<(&'a StationEvaNumber, &'a DbStopId)> for StationIdRef<'a> {
|
|
fn from(value: (&'a StationEvaNumber, &'a DbStopId)) -> Self {
|
|
StationIdRef {
|
|
eva_number: value.0,
|
|
db_station_id: &value.1,
|
|
}
|
|
}
|
|
}
|