1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
use serde::{Deserialize, Deserializer};
use chrono::{DateTime, Utc};
use colored::*;
use crate::serde::*;
use crate::traits::IsStation;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Station {
name: String,
pub ds100: Option<String>,
uic: u64,
latitude: f64,
longitude: f64,
#[serde(deserialize_with = "naive_read_unixtime")]
scheduled_time: DateTime<Utc>,
#[serde(deserialize_with = "naive_read_unixtime")]
real_time: DateTime<Utc>
}
pub fn parse_optional_station<'de, D>(d: D) -> Result<Option<Station>, D::Error>
where
D: Deserializer<'de>
{
let val = <serde_json::Value>::deserialize(d)?;
match serde_json::from_value(val) {
Ok(station) => Ok(Some(station)),
Err(_) => Ok(None)
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Stop {
name: String,
#[serde(deserialize_with = "option_naive_read_unixtime")]
scheduled_arrival: Option<DateTime<Utc>>,
#[serde(deserialize_with = "option_naive_read_unixtime")]
real_arrival: Option<DateTime<Utc>>,
#[serde(deserialize_with = "option_naive_read_unixtime")]
scheduled_departure: Option<DateTime<Utc>>,
#[serde(deserialize_with = "option_naive_read_unixtime")]
real_departure: Option<DateTime<Utc>>
}
impl IsStation for Station {
fn name(&self) -> &str {
&self.name
}
fn scheduled_arrival(&self) -> Option<&DateTime<Utc>> {
Some(&self.scheduled_time)
}
fn real_arrival(&self) -> Option<&DateTime<Utc>> {
Some(&self.real_time)
}
fn ds100(&self) -> Option<&str> {
self.ds100.as_deref()
}
}
impl IsStation for Stop {
fn name(&self) -> &str {
&self.name
}
fn scheduled_arrival(&self) -> Option<&DateTime<Utc>> {
self.scheduled_arrival.as_ref()
}
fn real_arrival(&self) -> Option<&DateTime<Utc>> {
self.real_arrival.as_ref()
}
fn ds100(&self) -> Option<&str> {
None
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Train {
#[serde(rename = "type")]
pub _type: String,
pub line: Option<String>,
pub no: String,
pub 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 {
deprecated: bool,
pub checked_in: bool,
pub from_station: Station,
#[serde(deserialize_with = "parse_optional_station")]
pub to_station: Option<Station>,
intermediate_stops: Vec<Stop>,
pub train: Option<Train>,
action_time: u64
}
#[allow(dead_code)]
pub struct Ds100 {
inner: String
}
/// 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 Trip<'_> {
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())
.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<S>> for Trip<'a> {
fn from(from: &'a Vec<S>) -> Self {
Trip(from.iter().map(|s| s as &dyn IsStation).collect())
}
}
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.checked_in {
false => write!(
f,
"not checked in. \n\n\
last trip: \n {} {}",
self.from_station.to_fancy_string(),
self.to_station.as_ref().unwrap().to_fancy_string()
),
true => write!(
f,
"checked in to: {}.\n\n\
stops:\n {}\n ↓\n{} {}",
self
.train
.as_ref()
.map(|t| t.to_string())
.unwrap_or("".to_string())
.green(),
self.from_station.to_fancy_string(),
Trip::from(&self.intermediate_stops),
self
.to_station
.as_ref()
.map(|s| s.to_fancy_string())
.unwrap_or_else(|| "🚄 Fahrt ins Blaue".blue().to_string())
)
}
}
}
|