Debian Codegraph @

src/types.rs

raw symbols SCIP
1 use std::borrow::Cow;
2 use std::collections::HashSet;
3
4 use chrono::DateTime;
5 use chrono::Duration;
6 use chrono_tz::Tz;
7 #[cfg(feature = "polylines")]
8 use geojson::FeatureCollection;
9 #[cfg(feature = "serde")]
10 use serde::{Deserialize, Serialize};
11
12 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13 #[derive(Debug, Clone)]
14 /// A general location.
15 pub enum Location {
16 /// An address.
17 Address {
18 /// The address.
19 address: String,
20 /// The latitude.
21 latitude: f32,
22 /// The longitude.
23 longitude: f32,
24 },
25 /// Anything else.
26 Point {
27 /// A not further specified ID.
28 id: Option<String>,
29 /// Some name.
30 name: Option<String>,
31 /// Is this a point of interest.
32 poi: Option<bool>,
33 /// The latitude.
34 latitude: f32,
35 /// The longitude.
36 longitude: f32,
37 },
38 }
39
40 impl PartialEq for Location {
41 fn eq(&self, other: &Self) -> bool {
42 match (&self, other) {
43 (Location::Address { address: a, .. }, Location::Address { address: b, .. }) => a == b,
44 (Location::Point { id: Some(a), .. }, Location::Point { id: Some(b), .. }) => a == b,
45 (_, _) => false,
46 }
47 }
48 }
49
50 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
51 #[derive(Debug, Clone, PartialEq)]
52 /// Any place.
53 pub enum Place {
54 /// A [`Station`].
55 Station(Station),
56 /// A [`Location`].
57 Location(Location),
58 }
59
60 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
61 #[derive(Debug, Default, Clone)]
62 /// A station where trains drive from.
63 pub struct Station {
64 /// A provider-specific, unique station id.
65 pub id: String,
66 /// A human-readable name
67 pub name: Option<String>,
68 /// Where the station is located.
69 pub location: Option<Location>,
70 /// The products served on the station.
71 pub products: Vec<Product>,
72 }
73
74 impl PartialEq for Station {
75 fn eq(&self, other: &Self) -> bool {
76 self.id == other.id
77 }
78 }
79
80 #[derive(Debug, Clone, PartialEq, Eq)]
81 /// A selection of modes.
82 pub struct ProductsSelection(HashSet<Mode>);
83
84 impl Default for ProductsSelection {
85 fn default() -> Self {
86 Self::all()
87 }
88 }
89
90 impl ProductsSelection {
91 pub fn all() -> Self {
92 // TODO: Automatically generate from enum?
93 Self(HashSet::from([
94 Mode::HighSpeedTrain,
95 Mode::RegionalTrain,
96 Mode::SuburbanTrain,
97 Mode::Subway,
98 Mode::Tram,
99 Mode::Bus,
100 Mode::Ferry,
101 Mode::Ferry,
102 Mode::Cablecar,
103 Mode::OnDemand,
104 Mode::Unknown,
105 ]))
106 }
107
108 pub fn contains(&self, mode: &Mode) -> bool {
109 self.0.contains(mode)
110 }
111 }
112
113 impl From<HashSet<Mode>> for ProductsSelection {
114 fn from(modes: HashSet<Mode>) -> Self {
115 Self(modes)
116 }
117 }
118
119 impl From<ProductsSelection> for HashSet<Mode> {
120 fn from(modes: ProductsSelection) -> Self {
121 modes.0
122 }
123 }
124
125 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
126 #[derive(Debug, Clone, PartialEq, Eq)]
127 /// The product a [`Line`] uses.
128 pub struct Product {
129 /// The mode of transport.
130 pub mode: Mode,
131 /// A name.
132 pub name: Cow<'static, str>,
133 /// A shorter name (usually one character).
134 pub short: Cow<'static, str>,
135 }
136
137 impl Product {
138 pub const fn unknown() -> Self {
139 Self {
140 mode: Mode::Unknown,
141 name: Cow::Borrowed("Unknown"),
142 short: Cow::Borrowed("Unknown"),
143 }
144 }
145 }
146
147 /// Different modes of transport.
148 ///
149 /// See also <https://gitlab.com/oeffi/public-transport-enabler/-/blob/fffd2c9acb290c583efecfc88b4e5be56e48e6da/src/de/schildbach/pte/dto/Product.java>.
150 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
151 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
152 pub enum Mode {
153 HighSpeedTrain,
154 RegionalTrain,
155 SuburbanTrain,
156 Subway,
157 Tram,
158 Bus,
159 Ferry,
160 Cablecar,
161 OnDemand,
162 Unknown,
163 // TODO: Walking?
164 }
165
166 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
167 #[derive(Debug, Clone, PartialEq, Eq)]
168 /// Different kinds of loyalty cards supported.
169 pub enum LoyaltyCard {
170 BahnCard25Class1,
171 BahnCard25Class2,
172 BahnCard50Class1,
173 BahnCard50Class2,
174 Vorteilscard,
175 HalbtaxaboRailplus,
176 Halbtaxabo,
177 VoordeelurenaboRailplus,
178 Voordeelurenabo,
179 SHCard,
180 Generalabonnement,
181 }
182
183 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
184 #[derive(Debug, Clone, PartialEq, Eq)]
185 /// The age of a person in years.
186 pub struct Age(pub u64);
187
188 impl LoyaltyCard {
189 /// See <https://gist.github.com/juliuste/202bb04f450a79f8fa12a2ec3abcd72d>.
190 pub fn from_id(value: u8) -> Option<Self> {
191 match value {
192 1 => Some(LoyaltyCard::BahnCard25Class1),
193 2 => Some(LoyaltyCard::BahnCard25Class2),
194 3 => Some(LoyaltyCard::BahnCard50Class1),
195 4 => Some(LoyaltyCard::BahnCard50Class2),
196 9 => Some(LoyaltyCard::Vorteilscard),
197 10 => Some(LoyaltyCard::HalbtaxaboRailplus),
198 11 => Some(LoyaltyCard::Halbtaxabo),
199 12 => Some(LoyaltyCard::VoordeelurenaboRailplus),
200 13 => Some(LoyaltyCard::Voordeelurenabo),
201 14 => Some(LoyaltyCard::SHCard),
202 15 => Some(LoyaltyCard::Generalabonnement),
203 _ => None,
204 }
205 }
206
207 pub fn to_id(self) -> u8 {
208 match self {
209 LoyaltyCard::BahnCard25Class1 => 1,
210 LoyaltyCard::BahnCard25Class2 => 2,
211 LoyaltyCard::BahnCard50Class1 => 3,
212 LoyaltyCard::BahnCard50Class2 => 4,
213 LoyaltyCard::Vorteilscard => 9,
214 LoyaltyCard::HalbtaxaboRailplus => 10,
215 LoyaltyCard::Halbtaxabo => 11,
216 LoyaltyCard::VoordeelurenaboRailplus => 12,
217 LoyaltyCard::Voordeelurenabo => 13,
218 LoyaltyCard::SHCard => 14,
219 LoyaltyCard::Generalabonnement => 15,
220 }
221 }
222 }
223
224 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
225 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
226 /// What class to travel with.
227 #[derive(Default)]
228 pub enum TariffClass {
229 /// First class.
230 First,
231 /// Second class.
232 #[default]
233 Second,
234 }
235
236 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
237 #[derive(Debug, Clone, PartialEq, Eq)]
238 /// How accessible a trip should be.
239 #[derive(Default)]
240 pub enum Accessibility {
241 /// Accessibility is not required.
242 #[default]
243 r#None,
244 /// Partial accessibility is required.
245 Partial,
246 /// Complete accessibility is required.
247 Complete,
248 }
249
250 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
251 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
252 /// How full a [`Leg`] is.
253 pub enum LoadFactor {
254 /// Not full.
255 LowToMedium,
256 /// Full.
257 High,
258 /// Very full.
259 VeryHigh,
260 /// Extremely full.
261 ExceptionallyHigh,
262 }
263
264 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
265 #[derive(Debug, Clone, PartialEq, Eq)]
266 /// A line serving a [`Leg`].
267 pub struct Line {
268 /// The line name.
269 pub name: Option<String>,
270 /// The line number.
271 pub fahrt_nr: Option<String>,
272 /// The mode of transport.
273 pub mode: Mode,
274 /// The product of the transport.
275 pub product: Product,
276 /// The operator of the line.
277 pub operator: Option<Operator>,
278 /// The nameof the product.
279 pub product_name: Option<String>,
280 }
281
282 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
283 #[derive(Debug, Clone, PartialEq)]
284 /// At what frequency the train drives.
285 pub struct Frequency {
286 /// Mimimum duration between the same train.
287 #[cfg_attr(feature = "serde", serde(with = "crate::serialize::duration"))]
288 pub minimum: Option<Duration>,
289 /// Maximum duration between the same train.
290 #[cfg_attr(feature = "serde", serde(with = "crate::serialize::duration"))]
291 pub maximum: Option<Duration>,
292 /// How often this iteration occures.
293 pub iterations: Option<u64>,
294 }
295
296 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
297 #[derive(Debug, Clone, PartialEq)]
298 /// A leg of a journey.
299 pub struct Leg {
300 /// The origin to enter the train from.
301 pub origin: Place,
302 /// The destination to exit the train from.
303 pub destination: Place,
304 #[cfg_attr(
305 feature = "serde",
306 serde(with = "crate::serialize::datetime_with_timezone")
307 )]
308 /// The real-time departure time.
309 pub departure: Option<DateTime<Tz>>,
310 #[cfg_attr(
311 feature = "serde",
312 serde(with = "crate::serialize::datetime_with_timezone")
313 )]
314 /// The scheduled departure time.
315 pub planned_departure: Option<DateTime<Tz>>,
316 #[cfg_attr(
317 feature = "serde",
318 serde(with = "crate::serialize::datetime_with_timezone")
319 )]
320 /// The real-time arrival time.
321 pub arrival: Option<DateTime<Tz>>,
322 #[cfg_attr(
323 feature = "serde",
324 serde(with = "crate::serialize::datetime_with_timezone")
325 )]
326 /// The scheduled arrival time.
327 pub planned_arrival: Option<DateTime<Tz>>,
328 /// Whether this leg is reachable from the previous one.
329 pub reachable: bool,
330 /// A unique ID for the trip.
331 pub trip_id: Option<String>,
332 /// The line serving this leg.
333 pub line: Option<Line>,
334 /// The direction of the leg.
335 pub direction: Option<String>,
336 /// The real-time arrival platform.
337 pub arrival_platform: Option<String>,
338 /// The scheduled arrival platform.
339 pub planned_arrival_platform: Option<String>,
340 /// The real-time departure platform.
341 pub departure_platform: Option<String>,
342 /// The scheduled departure platform.
343 pub planned_departure_platform: Option<String>,
344 /// The frequency of the leg.
345 pub frequency: Option<Frequency>,
346 /// Whether this leg was cancelled.
347 pub cancelled: bool,
348 /// Intermediate locations for the leg.
349 pub intermediate_locations: Vec<IntermediateLocation>,
350 /// The load of the leg.
351 pub load_factor: Option<LoadFactor>,
352 /// Remarks on the leg.
353 pub remarks: Vec<Remark>,
354 /// The polyline of the leg.
355 #[cfg(feature = "polylines")]
356 pub polyline: Option<FeatureCollection>,
357 /// Whether this leg needs to be walked.
358 pub walking: bool,
359 /// Whether this leg requires transfer.
360 pub transfer: bool,
361 /// How long this leg is.
362 pub distance: Option<u64>,
363 }
364
365 impl Leg {
366 // An ID of a leg based on attributes that should not change e.g. when refreshed.
367 pub fn id(&self) -> String {
368 format!(
369 "{};{};{};{};{}",
370 self.trip_id.as_ref().map(|s| &s[..]).unwrap_or_default(),
371 self.planned_departure
372 .as_ref()
373 .map(|t| t.to_string())
374 .unwrap_or_default(),
375 self.planned_arrival
376 .as_ref()
377 .map(|t| t.to_string())
378 .unwrap_or_default(),
379 self.planned_departure_platform
380 .as_ref()
381 .map(|s| &s[..])
382 .unwrap_or_default(),
383 self.planned_arrival_platform
384 .as_ref()
385 .map(|s| &s[..])
386 .unwrap_or_default()
387 )
388 }
389 }
390
391 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
392 #[derive(Debug, Clone, PartialEq)]
393 // Almost always, we have `IntermediateLocation::Stop`. As the documentation for this lint suggests, boxing the larger variant which is almost always used is counterproductive.
394 #[allow(clippy::large_enum_variant)]
395 /// An intermediate locaion of a leg.
396 pub enum IntermediateLocation {
397 /// A place where the train stops to let out passengers.
398 Stop(Stop),
399 /// A named railway track the train is passing over.
400 Railway(Place),
401 }
402
403 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
404 #[derive(Debug, Clone, PartialEq)]
405 /// A place where the train stops.
406 pub struct Stop {
407 /// The place of the stop.
408 pub place: Place,
409 #[cfg_attr(
410 feature = "serde",
411 serde(with = "crate::serialize::datetime_with_timezone")
412 )]
413 /// The real-time departure time.
414 pub departure: Option<DateTime<Tz>>,
415 #[cfg_attr(
416 feature = "serde",
417 serde(with = "crate::serialize::datetime_with_timezone")
418 )]
419 /// The scheduled departure time.
420 pub planned_departure: Option<DateTime<Tz>>,
421 #[cfg_attr(
422 feature = "serde",
423 serde(with = "crate::serialize::datetime_with_timezone")
424 )]
425 /// The real-time arrival time.
426 pub arrival: Option<DateTime<Tz>>,
427 #[cfg_attr(
428 feature = "serde",
429 serde(with = "crate::serialize::datetime_with_timezone")
430 )]
431 /// The scheduled arrival time.
432 pub planned_arrival: Option<DateTime<Tz>>,
433 /// The real-time arrival platform.
434 pub arrival_platform: Option<String>,
435 /// The scheduled arrival platform.
436 pub planned_arrival_platform: Option<String>,
437 /// The real-time departure platform.
438 pub departure_platform: Option<String>,
439 /// The real-time departure platform.
440 pub planned_departure_platform: Option<String>,
441 /// Whether this stop is cancelled.
442 pub cancelled: bool,
443 /// Remarks specific to this stop.
444 pub remarks: Vec<Remark>,
445 }
446
447 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
448 #[derive(Debug, Clone, PartialEq)]
449 /// The price of the trip.
450 pub struct Price {
451 /// The amount.
452 pub amount: f64,
453 /// The currency.
454 pub currency: String,
455 }
456
457 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
458 #[derive(Debug, Clone, PartialEq)]
459 /// A single journey from source to destination.
460 pub struct Journey {
461 /// A unique journey id.
462 pub id: String,
463 /// The legs that make up the journey.
464 pub legs: Vec<Leg>,
465 /// The price of the journey.
466 pub price: Option<Price>,
467 }
468
469 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
470 #[derive(Debug, Clone, PartialEq, Eq)]
471 /// The operator serving a [`Line`].
472 pub struct Operator {
473 /// An unique id.
474 pub id: String,
475 /// The name.
476 pub name: String,
477 }
478
479 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
480 #[derive(Debug, Clone, PartialEq, Eq)]
481 /// What type a remark has.
482 pub enum RemarkType {
483 /// It is a hint, e.g. information that bikes are not allowed.
484 Hint,
485 /// It is a status, e.g. that the trip is cancelled.
486 Status,
487 }
488
489 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
490 #[derive(Debug, Clone, PartialEq, Eq)]
491 /// What to associate a [`Remark`] with.
492 #[derive(Default)]
493 pub enum RemarkAssociation {
494 /// E.g. bikes allowed, disallowed, limited.
495 Bike,
496 /// E.g. accessible equipment in the train.
497 Accessibility,
498 /// E.g. one can buy tickets in the train.
499 Ticket,
500 /// There is a power socket in the train.
501 Power,
502 /// There is air conditioning on the train.
503 AirConditioning,
504 /// There is WiFi on the train.
505 WiFi,
506 /// There is no first class on this train.
507 OnlySecondClass,
508 /// The remark code specifies an association, but this could not yet be decoded.
509 #[default]
510 Unknown,
511 /// The remark did not specify an association.
512 None,
513 }
514
515 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
516 #[derive(Debug, Clone, PartialEq, Eq)]
517 /// A remark on a leg or stopover.
518 pub struct Remark {
519 /// The provider-specific code for it.
520 pub code: String,
521 /// Text to display.
522 pub text: String,
523 /// What kind of remark it has.
524 pub r#type: RemarkType,
525 /// What to associate the remark with.
526 pub association: RemarkAssociation,
527 /// A short summary of the remark.
528 pub summary: Option<String>,
529 /// What trip this remark is about.
530 pub trip_id: Option<String>,
531 }