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
|
use serde::Deserialize;
use serde_json::Value;
use crate::travelynx::TrainRef;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TripInfo {
trip: Trip,
connection: Option<Value>,
selected_route: Option<Value>,
active: Option<Value>
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Trip {
train_type: String,
vzn: String, // train number
// some position info here
actual_position: u64, // distance along track, presumably
stops: Vec<Stop>
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Stop {
info: StopInfo,
station: Station
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct StopInfo {
distance_from_start: u64,
position_status: String // one of "departed", "future", ... ?
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Station {
eva_nr: String,
name: String
}
impl TripInfo {
pub fn guess_last_station (&self) -> Option<String> {
let current_pos = self.trip.actual_position;
self.trip
.stops
.iter()
.rev()
.map(|stop| (stop.info.distance_from_start, stop))
.filter(|(dist,_)| dist <= ¤t_pos)
.next()
.map(|(_,stop)| stop.station.name.clone())
}
pub fn get_train_ref (&self) -> TrainRef {
TrainRef {
_type: self.trip.train_type.clone(),
no: self.trip.vzn.clone()
}
}
}
|