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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use chrono::NaiveDateTime;
use hrdf_parser::{Coordinates, DataStorage, Journey};
use rustc_hash::FxHashSet;
use serde::Serialize;

#[derive(Debug, Clone)]
pub struct RouteSection {
    journey_id: Option<i32>,
    departure_stop_id: i32,
    arrival_stop_id: i32,
    arrival_at: NaiveDateTime,
    duration: Option<i16>,
}

impl RouteSection {
    pub fn new(
        journey_id: Option<i32>,
        departure_stop_id: i32,
        arrival_stop_id: i32,
        arrival_at: NaiveDateTime,
        duration: Option<i16>,
    ) -> Self {
        Self {
            journey_id,
            departure_stop_id,
            arrival_stop_id,
            arrival_at,
            duration,
        }
    }

    // Getters/Setters

    pub fn journey_id(&self) -> Option<i32> {
        self.journey_id
    }

    pub fn departure_stop_id(&self) -> i32 {
        self.departure_stop_id
    }

    pub fn arrival_stop_id(&self) -> i32 {
        self.arrival_stop_id
    }

    pub fn set_arrival_stop_id(&mut self, value: i32) {
        self.arrival_stop_id = value;
    }

    pub fn arrival_at(&self) -> NaiveDateTime {
        self.arrival_at
    }

    pub fn set_arrival_at(&mut self, value: NaiveDateTime) {
        self.arrival_at = value;
    }

    pub fn duration(&self) -> Option<i16> {
        self.duration
    }

    // Functions

    pub fn journey<'a>(&'a self, data_storage: &'a DataStorage) -> Option<&Journey> {
        self.journey_id.map(|id| data_storage.journeys().find(id))
    }
}

#[derive(Debug, Clone)]
pub struct Route {
    sections: Vec<RouteSection>,
    visited_stops: FxHashSet<i32>,
}

impl Route {
    pub fn new(sections: Vec<RouteSection>, visited_stops: FxHashSet<i32>) -> Self {
        Self {
            sections,
            visited_stops,
        }
    }

    // Getters/Setters

    pub fn sections(&self) -> &Vec<RouteSection> {
        &self.sections
    }

    pub fn visited_stops(&self) -> &FxHashSet<i32> {
        &self.visited_stops
    }

    // Functions

    pub fn last_section(&self) -> &RouteSection {
        // A route always contains at least one section.
        self.sections.last().unwrap()
    }

    pub fn last_section_mut(&mut self) -> &mut RouteSection {
        // A route always contains at least one section.
        self.sections.last_mut().unwrap()
    }

    pub fn arrival_stop_id(&self) -> i32 {
        self.last_section().arrival_stop_id()
    }

    pub fn arrival_at(&self) -> NaiveDateTime {
        self.last_section().arrival_at()
    }

    pub fn has_visited_any_stops(&self, stops: &FxHashSet<i32>) -> bool {
        !self.visited_stops.is_disjoint(stops)
    }

    pub fn sections_having_journey(&self) -> Vec<&RouteSection> {
        self.sections
            .iter()
            .filter(|section| section.journey_id().is_some())
            .collect()
    }

    pub fn count_connections(&self) -> usize {
        self.sections_having_journey().len()
    }
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum RoutingAlgorithmMode {
    SolveFromDepartureStopToArrivalStop,
    SolveFromDepartureStopToReachableArrivalStops,
}

pub struct RoutingAlgorithmArgs {
    mode: RoutingAlgorithmMode,
    arrival_stop_id: Option<i32>,
    time_limit: Option<NaiveDateTime>,
}

impl RoutingAlgorithmArgs {
    pub fn new(
        mode: RoutingAlgorithmMode,
        arrival_stop_id: Option<i32>,
        time_limit: Option<NaiveDateTime>,
    ) -> Self {
        Self {
            mode,
            arrival_stop_id,
            time_limit,
        }
    }

    pub fn solve_from_departure_stop_to_arrival_stop(arrival_stop_id: i32) -> Self {
        Self::new(
            RoutingAlgorithmMode::SolveFromDepartureStopToArrivalStop,
            Some(arrival_stop_id),
            None,
        )
    }

    pub fn solve_from_departure_stop_to_reachable_arrival_stops(time_limit: NaiveDateTime) -> Self {
        Self::new(
            RoutingAlgorithmMode::SolveFromDepartureStopToReachableArrivalStops,
            None,
            Some(time_limit),
        )
    }

    // Getters/Setters

    pub fn mode(&self) -> RoutingAlgorithmMode {
        self.mode
    }

    /// Do not call this function if you are not sure that arrival_stop_id is not None.
    pub fn arrival_stop_id(&self) -> i32 {
        self.arrival_stop_id.unwrap()
    }

    /// Do not call this function if you are not sure that time_limit is not None.
    pub fn time_limit(&self) -> NaiveDateTime {
        self.time_limit.unwrap()
    }
}

#[derive(Debug, Serialize)]
pub struct RouteResult {
    departure_at: NaiveDateTime,
    arrival_at: NaiveDateTime,
    sections: Vec<RouteSectionResult>,
}

impl RouteResult {
    pub fn new(
        departure_at: NaiveDateTime,
        arrival_at: NaiveDateTime,
        sections: Vec<RouteSectionResult>,
    ) -> Self {
        Self {
            departure_at,
            arrival_at,
            sections,
        }
    }

    // Getters/Setters

    pub fn arrival_at(&self) -> NaiveDateTime {
        self.arrival_at
    }

    pub fn sections(&self) -> &Vec<RouteSectionResult> {
        &self.sections
    }
}

#[derive(Debug, Serialize)]
pub struct RouteSectionResult {
    journey_id: Option<i32>,
    departure_stop_id: i32,
    departure_stop_lv95_coordinates: Option<Coordinates>,
    departure_stop_wgs84_coordinates: Option<Coordinates>,
    arrival_stop_id: i32,
    arrival_stop_lv95_coordinates: Option<Coordinates>,
    arrival_stop_wgs84_coordinates: Option<Coordinates>,
    departure_at: Option<NaiveDateTime>,
    arrival_at: Option<NaiveDateTime>,
    duration: Option<i16>,
}

impl RouteSectionResult {
    pub fn new(
        journey_id: Option<i32>,
        departure_stop_id: i32,
        departure_stop_lv95_coordinates: Option<Coordinates>,
        departure_stop_wgs84_coordinates: Option<Coordinates>,
        arrival_stop_id: i32,
        arrival_stop_lv95_coordinates: Option<Coordinates>,
        arrival_stop_wgs84_coordinates: Option<Coordinates>,
        departure_at: Option<NaiveDateTime>,
        arrival_at: Option<NaiveDateTime>,
        duration: Option<i16>,
    ) -> Self {
        Self {
            journey_id,
            departure_stop_id,
            departure_stop_lv95_coordinates,
            departure_stop_wgs84_coordinates,
            arrival_stop_id,
            arrival_stop_lv95_coordinates,
            arrival_stop_wgs84_coordinates,
            departure_at,
            arrival_at,
            duration,
        }
    }

    // Getters/Setters

    pub fn departure_stop_id(&self) -> i32 {
        self.departure_stop_id
    }

    pub fn departure_at(&self) -> Option<NaiveDateTime> {
        self.departure_at
    }

    pub fn arrival_stop_id(&self) -> i32 {
        self.arrival_stop_id
    }

    pub fn arrival_stop_lv95_coordinates(&self) -> Option<Coordinates> {
        self.arrival_stop_lv95_coordinates
    }

    // pub fn arrival_stop_wgs84_coordinates(&self) -> Option<Coordinates> {
    //     self.arrival_stop_wgs84_coordinates
    // }

    pub fn arrival_at(&self) -> Option<NaiveDateTime> {
        self.arrival_at
    }

    pub fn duration(&self) -> Option<i16> {
        self.duration
    }

    // Functions

    pub fn journey<'a>(&'a self, data_storage: &'a DataStorage) -> Option<&Journey> {
        self.journey_id.map(|id| data_storage.journeys().find(id))
    }

    pub fn is_walking_trip(&self) -> bool {
        self.journey_id.is_none()
    }
}