bahnreise/src/storage/stations.rs
2025-08-19 23:27:17 +02:00

68 lines
2.1 KiB
Rust

use std::collections::HashMap;
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 lat_lon: Option<(f64, f64)>,
pub transport_modes: Option<TransportModesSet>,
}
impl Stations {
pub fn new() -> Self {
Self {
stations: HashMap::new(),
}
}
pub fn insert(&mut self, eva_number: StationEvaNumber, 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!()),
lat_lon: None,
transport_modes: None,
}
}
prev.name = station.name;
prev.db_station_id = station.db_station_id;
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 {
self.stations.insert(eva_number, station);
}
}
pub fn get(&mut self, eva_number: &StationEvaNumber) -> Option<&Station> {
self.stations.get(eva_number)
}
}
impl<'a> From<(&'a StationEvaNumber, &'a Station)> for StationIdRef<'a> {
fn from(value: (&'a StationEvaNumber, &'a Station)) -> Self {
StationIdRef {
eva_number: value.0,
db_station_id: &value.1.db_station_id,
}
}
}