summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstuebinm2022-02-01 17:41:56 +0100
committerstuebinm2022-02-01 17:42:59 +0100
commit80a4af9f0a6f7e3bd2d8254f8bc8475ca73b8980 (patch)
treea2077ce6d328f2b94d0394fa1590c0cc5feac6fe /src
parentaacb2d78a4a97e3e8d7fdff4c06cba8be5f3139f (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/iceportal.rs39
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs10
-rw-r--r--src/serde.rs22
-rw-r--r--src/travelynx.rs6
-rw-r--r--src/types.rs30
6 files changed, 80 insertions, 28 deletions
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<NaiveDateTime>,
+ #[serde(deserialize_with = "option_naive_read_unixtime")]
+ actual_arrival_time: Option<NaiveDateTime>
+}
+
+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::<TripInfo>("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<NaiveDateTime, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ let ts = <i64>::deserialize(d)?;
+ Ok(NaiveDateTime::from_timestamp(ts, 0))
+}
+
+pub fn option_naive_read_unixtime<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ match <i64>::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<bool>,
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<NaiveDateTime, D::Error>
-where
- D: Deserializer<'de>,
-{
- let ts = <i64>::deserialize(d)?;
- Ok(NaiveDateTime::from_timestamp(ts, 0))
-}
-fn option_naive_read_unixtime<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
-where
- D: Deserializer<'de>,
-{
- match <i64>::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<NaiveDateTime>,
}
-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<Stop>);
+pub struct Trip<'a, S: IsStation> (pub &'a Vec<S>);
+
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<S: IsStation> 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()