From cd13c85c69cf761b2da84ad91af64d23a3568aa5 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Wed, 9 Feb 2022 02:51:59 +0100 Subject: existential types in rust are weird … lots and lots of traits … --- src/types.rs | 64 ++++++++++++++++++------------------------------------------ 1 file changed, 19 insertions(+), 45 deletions(-) (limited to 'src/types.rs') diff --git a/src/types.rs b/src/types.rs index c1c949a..482025a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,9 +1,10 @@ use serde::{Deserialize, Deserializer}; -use chrono::{DateTime, Local, Utc}; +use chrono::{DateTime, Utc}; use colored::*; use crate::serde::*; +use crate::traits::IsStation; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -44,38 +45,6 @@ pub struct Stop { real_departure: Option> } -pub trait IsStation { - fn name(&self) -> &str; - fn scheduled_arrival(&self) -> Option<&DateTime>; - fn real_arrival(&self) -> Option<&DateTime>; - fn ds100(&self) -> &str; - - fn to_fancy_string(&self) -> String { - format!( - "{} {} – {} ({})", - self - .real_arrival() // chrono's API for timezones is expressive, but reads like c++ … - .map(|t| >::from(*t).time().to_string()) - .unwrap_or("??:??:??".to_string()) - .blue(), - { - let delay = match (self.real_arrival(), self.scheduled_arrival()) { - (Some(a), Some(s)) => (a.time() - s.time()).num_minutes(), - _ => 0 - }; - let text = format!("({:+})", delay); - if delay > 0 { - text.red() - } else { - text.green() - } - }, - self.ds100().red(), - self.name() - ) - } -} - impl IsStation for Station { fn name(&self) -> &str { &self.name @@ -136,7 +105,9 @@ pub struct Ds100 { inner: String } -pub struct Trip<'a, S: IsStation>(pub &'a Vec); +pub struct Trip<'a, 'i, S: IsStation>( + pub &'i mut dyn std::iter::Iterator +); impl std::fmt::Display for Train { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -144,17 +115,20 @@ impl std::fmt::Display for Train { } } -#[allow(unstable_name_collisions)] -impl std::fmt::Display for Trip<'_, S> { +// #[allow(unstable_name_collisions)] +impl std::fmt::Display for Trip<'_, '_, S> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - 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()); - } + 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(()) } } @@ -180,7 +154,7 @@ impl std::fmt::Display for Status { .unwrap_or("".to_string()) .green(), self.from_station.to_fancy_string(), - Trip(&self.intermediate_stops), + Trip(&mut self.intermediate_stops.iter()), self .to_station .as_ref() -- cgit v1.2.3