From cea0b70a51d73c9eda7b2cf83451ca328182f0a8 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sat, 26 Feb 2022 11:37:00 +0100 Subject: fix the Trip type (and make it Display) it's not as nice as i'd like, but whatever --- src/onboard/iceportal.rs | 12 ++++++------ src/onboard/mod.rs | 5 ++--- src/onboard/zugportal.rs | 8 ++++---- src/types.rs | 45 ++++++++++++++++++++++++--------------------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/onboard/iceportal.rs b/src/onboard/iceportal.rs index bc97261..bde0b1f 100644 --- a/src/onboard/iceportal.rs +++ b/src/onboard/iceportal.rs @@ -8,12 +8,14 @@ use crate::onboard; use crate::onboard::{OnBoardAPI, OnBoardInfo}; use crate::{serde::*, traits::*, travelynx::TrainRef}; +use crate::types::Trip; + pub struct Iceportal {} #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct TripInfo { - trip: Trip, + trip: IceTrip, connection: Option, selected_route: Option, active: Option @@ -21,7 +23,7 @@ pub struct TripInfo { #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] -struct Trip { +struct IceTrip { train_type: String, vzn: String, // train number // some position info here @@ -100,10 +102,8 @@ impl OnBoardInfo for TripInfo { } } - fn stops<'a>( - &'a self - ) -> Box + 'a> { - Box::new(self.trip.stops.iter().map(|s| s as &dyn IsStation)) + fn stops<'a>(&'a self) -> Trip<'a> { + (&self.trip.stops).into() } } diff --git a/src/onboard/mod.rs b/src/onboard/mod.rs index 56bd144..b6717e8 100644 --- a/src/onboard/mod.rs +++ b/src/onboard/mod.rs @@ -1,5 +1,6 @@ use crate::traits::IsStation; use crate::travelynx::TrainRef; +use crate::types::Trip; use serde::de::DeserializeOwned; pub mod iceportal; @@ -47,9 +48,7 @@ pub trait OnBoardInfo { fn get_train_ref(&self) -> TrainRef; - fn stops<'a>( - &'a self - ) -> Box + 'a>; + fn stops<'a>(&'a self) -> Trip<'a>; } fn get_request(uri: &str) -> Result diff --git a/src/onboard/zugportal.rs b/src/onboard/zugportal.rs index 0ad6cfd..c649107 100644 --- a/src/onboard/zugportal.rs +++ b/src/onboard/zugportal.rs @@ -7,6 +7,8 @@ use crate::onboard; use crate::onboard::{OnBoardAPI, OnBoardInfo}; use crate::{traits::*, travelynx::TrainRef}; +use crate::types::Trip; + pub struct Zugportal {} #[derive(Deserialize, Debug)] @@ -88,10 +90,8 @@ impl OnBoardInfo for Journey { } } - fn stops<'a>( - &'a self - ) -> Box + 'a> { - Box::new(self.stops.iter().map(|s| s as &dyn IsStation)) + fn stops<'a>(&'a self) -> Trip<'a> { + (&self.stops).into() } } diff --git a/src/types.rs b/src/types.rs index 482025a..5afbad2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -87,6 +87,12 @@ pub struct Train { id: String } +impl std::fmt::Display for Train { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {}", self._type, self.no) + } +} + #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Status { @@ -105,31 +111,28 @@ pub struct Ds100 { inner: String } -pub struct Trip<'a, 'i, S: IsStation>( - pub &'i mut dyn std::iter::Iterator -); +/// this type is a little silly, but apparently there's no better way +/// to 'forget' what the concrete type is than re-constructing the vec? +pub struct Trip<'a>(Vec<&'a dyn IsStation>); -impl std::fmt::Display for Train { +impl std::fmt::Display for Trip<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{} {}", self._type, self.no) + if self.0.len() != 0 { + self + .0 + .iter() + .map(|stop| stop.to_fancy_string()) + .for_each(|l| writeln!(f, " {}\n ↓", l).unwrap()); + } + Ok(()) } } -// #[allow(unstable_name_collisions)] -impl std::fmt::Display for Trip<'_, '_, S> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!(); - // self.0.map(|stop| stop.to_fancy_string()) - // .for_each(|l| writeln!(f, " {}\n ↓", l).unwrap()); - // if self.0.len() != 0 { - // self - // .0 - // .iter() - // .map(|stop| stop.to_fancy_string()) - // // .intersperse(" ↓".to_string()) - // .for_each(|l| writeln!(f, " {}\n ↓", l).unwrap()); - // } - Ok(()) +/// with this, you sometimes have to write (&self.stops).into() +/// yay for reference syntax! +impl<'a, S: IsStation + 'a> From<&'a Vec> for Trip<'a> { + fn from(from: &'a Vec) -> Self { + Trip(from.iter().map(|s| s as &dyn IsStation).collect()) } } @@ -154,7 +157,7 @@ impl std::fmt::Display for Status { .unwrap_or("".to_string()) .green(), self.from_station.to_fancy_string(), - Trip(&mut self.intermediate_stops.iter()), + Trip::from(&self.intermediate_stops), self .to_station .as_ref() -- cgit v1.2.3