diff options
author | stuebinm | 2022-02-09 02:51:59 +0100 |
---|---|---|
committer | stuebinm | 2022-02-09 02:51:59 +0100 |
commit | cd13c85c69cf761b2da84ad91af64d23a3568aa5 (patch) | |
tree | 4cbdbf6f348508ad7f70aa11450dccc8148ae0cb /src/iceportal.rs | |
parent | cf88935b5245daea51d2b513709b61a0e43483d6 (diff) |
existential types in rust are weird
… lots and lots of traits …
Diffstat (limited to '')
-rw-r--r-- | src/onboard/iceportal.rs (renamed from src/iceportal.rs) | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/src/iceportal.rs b/src/onboard/iceportal.rs index 3cb5e6f..04ec291 100644 --- a/src/iceportal.rs +++ b/src/onboard/iceportal.rs @@ -1,8 +1,14 @@ +/// implementation of traits to query the iceportal.de +/// (available in high speed trains in DE) use chrono::{DateTime, Utc}; use serde::Deserialize; use serde_json::Value; -use crate::{serde::*, travelynx::TrainRef, types::IsStation}; +use crate::onboard; +use crate::onboard::{OnBoardAPI, OnBoardInfo}; +use crate::{serde::*, traits::*, travelynx::TrainRef}; + +pub struct Iceportal {} #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -72,8 +78,8 @@ impl IsStation for Stop { } } -impl TripInfo { - pub fn guess_last_station(&self) -> Option<String> { +impl OnBoardInfo for TripInfo { + fn guess_last_station(&self) -> Option<&dyn IsStation> { let current_pos = self.trip.actual_position; self .trip @@ -83,17 +89,38 @@ impl TripInfo { .map(|stop| (stop.info.distance_from_start, stop)) .filter(|(dist, _)| dist <= ¤t_pos) .next() - .map(|(_, stop)| stop.station.name.clone()) + .map(|(_, stop)| stop as &dyn IsStation) } - pub fn get_train_ref(&self) -> TrainRef { + fn get_train_ref(&self) -> TrainRef { TrainRef { _type: self.trip.train_type.clone(), no: self.trip.vzn.clone() } } - pub fn trip(&self) -> crate::types::Trip<'_, Stop> { - crate::types::Trip(&self.trip.stops) + fn stops<'a>( + &'a self + ) -> Box<dyn std::iter::Iterator<Item = &'a dyn IsStation> + 'a> { + Box::new(self.trip.stops.iter().map(|s| s as &dyn IsStation)) + } +} + +impl std::fmt::Display for Stop { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.station.name) + } +} + +impl OnBoardAPI for Iceportal { + fn apiurl(&self) -> &'static str { + "https://iceportal.de/api1/rs/tripInfo/trip" + } + + fn request( + &self, + debug: bool + ) -> Result<Box<dyn OnBoardInfo>, serde_json::Error> { + onboard::request::<_, TripInfo>(self, debug) } } |