From 80a4af9f0a6f7e3bd2d8254f8bc8475ca73b8980 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Tue, 1 Feb 2022 17:41:56 +0100 Subject: display iceportal itinery & stops I'm pretty sure something in parsing the times is broken, since this train is currently /also/ broken I have absolutely no idea what the correct ones would be and don't have the energy to dig further into this. --- src/iceportal.rs | 39 ++++++++++++++++++++++++++++++++++++--- src/lib.rs | 1 + src/main.rs | 10 +++++++--- src/serde.rs | 22 ++++++++++++++++++++++ src/travelynx.rs | 6 ++++++ src/types.rs | 30 ++++++++---------------------- 6 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 src/serde.rs (limited to 'src') diff --git a/src/iceportal.rs b/src/iceportal.rs index 1b36556..a81d5d3 100644 --- a/src/iceportal.rs +++ b/src/iceportal.rs @@ -1,7 +1,8 @@ +use chrono::NaiveDateTime; use serde::Deserialize; use serde_json::Value; -use crate::travelynx::TrainRef; +use crate::{travelynx::TrainRef, types::IsStation, serde::*}; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -24,9 +25,10 @@ struct Trip { #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] -struct Stop { +pub struct Stop { info: StopInfo, - station: Station + station: Station, + timetable: Timetable } #[derive(Deserialize, Debug)] @@ -43,6 +45,33 @@ struct Station { name: String } +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +struct Timetable { + #[serde(deserialize_with = "option_naive_read_unixtime")] + scheduled_arrival_time: Option, + #[serde(deserialize_with = "option_naive_read_unixtime")] + actual_arrival_time: Option +} + +impl IsStation for Stop { + fn name (&self) -> &str { + &self.station.name + } + + fn scheduled_arrival (&self) -> Option<&chrono::NaiveDateTime> { + self.timetable.scheduled_arrival_time.as_ref() + } + + fn real_arrival (&self) -> Option<&chrono::NaiveDateTime> { + self.timetable.actual_arrival_time.as_ref() + } + + fn ds100 (&self) -> &str { + "??" + } +} + impl TripInfo { @@ -64,4 +93,8 @@ impl TripInfo { no: self.trip.vzn.clone() } } + + pub fn trip (&self) -> crate::types::Trip<'_,Stop> { + crate::types::Trip(&self.trip.stops) + } } diff --git a/src/lib.rs b/src/lib.rs index a528cec..e1ced8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,4 @@ pub mod types; pub mod travelynx; pub mod iceportal; +pub (crate) mod serde; diff --git a/src/main.rs b/src/main.rs index 1712545..cb9ac6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,12 +159,16 @@ fn main() -> Result<(), ureq::Error> { Command::ICEPortal => { match get_request::("https://iceportal.de/api1/rs/tripInfo/trip") { Ok(resp) => { - println!("{:?}", resp); + println!("{}: Currently in {}", traveltext, resp.get_train_ref()); println!("guessing last stop was: {:?}", resp.guess_last_station()); + println!("{}", resp.trip()) } - Err(_) => { + Err(err) => { + if cli.debug { + eprintln!("{:?}", err); + } println!("either this tool or the iceportal broke or you're not actually on an ICE\n\ - (get a response but couldn't to parse it)"); + (get a response but couldn't parse it)"); } } } diff --git a/src/serde.rs b/src/serde.rs new file mode 100644 index 0000000..9049d64 --- /dev/null +++ b/src/serde.rs @@ -0,0 +1,22 @@ +use chrono::NaiveDateTime; +use serde::{Deserialize, Deserializer}; + + +pub fn naive_read_unixtime<'de, D>(d: D) -> Result +where + D: Deserializer<'de>, +{ + let ts = ::deserialize(d)?; + Ok(NaiveDateTime::from_timestamp(ts, 0)) +} + +pub fn option_naive_read_unixtime<'de, D>(d: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + match ::deserialize(d) { + Ok(ts) => + Ok(Some(NaiveDateTime::from_timestamp(ts, 0))), + Err(_) => Ok(None) + } +} diff --git a/src/travelynx.rs b/src/travelynx.rs index 5457641..a6c59e1 100644 --- a/src/travelynx.rs +++ b/src/travelynx.rs @@ -48,6 +48,12 @@ pub struct TrainRef { pub no: String, } +impl std::fmt::Display for TrainRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {}", self._type, self.no) + } +} + #[derive(Deserialize, Debug)] pub struct Response { success: Option, diff --git a/src/types.rs b/src/types.rs index 1efd9c1..2d8d189 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,7 +2,8 @@ use serde::{Deserialize, Deserializer}; use chrono::NaiveDateTime; use colored::*; -use itertools::Itertools; + +use crate::serde::*; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -31,24 +32,6 @@ where } -fn naive_read_unixtime<'de, D>(d: D) -> Result -where - D: Deserializer<'de>, -{ - let ts = ::deserialize(d)?; - Ok(NaiveDateTime::from_timestamp(ts, 0)) -} -fn option_naive_read_unixtime<'de, D>(d: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - match ::deserialize(d) { - Ok(ts) => - Ok(Some(NaiveDateTime::from_timestamp(ts, 0))), - Err(_) => Ok(None) - } -} - #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -64,7 +47,9 @@ pub struct Stop { real_departure: Option, } -trait IsStation { + + +pub trait IsStation { fn name (&self) -> &str; fn scheduled_arrival (&self) -> Option<&NaiveDateTime>; fn real_arrival (&self) -> Option<&NaiveDateTime>; @@ -157,7 +142,8 @@ pub struct Ds100 { inner: String, } -struct Trip<'a> (&'a Vec); +pub struct Trip<'a, S: IsStation> (pub &'a Vec); + impl std::fmt::Display for Train { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -166,7 +152,7 @@ impl std::fmt::Display for Train { } #[allow(unstable_name_collisions)] -impl std::fmt::Display for Trip<'_> { +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() -- cgit v1.2.3