publictransport 0.1.2

Client for public transport APIs that builds requests and parses responses without doing any I/O
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! The MOTIS API, as served by Transitous.

use serde::Deserialize;

use crate::{
    Coordinate, Departure, DeparturesQuery, Error, GlobalId, HttpRequest, HttpResponse, Leg,
    LegKind, Line, Location, LocationKind, LocationQuery, Mode, Network, Stopover, Trip, TripTime,
    Trips, TripsQuery, model::Target, time::WireTime,
};

pub(crate) fn locations_request(
    network: &Network,
    query: &LocationQuery,
) -> Result<HttpRequest, Error> {
    let limit = query.limit.map(|limit| limit.to_string());
    // The server has no type for the other kinds.
    let types: Vec<&str> = [
        (LocationKind::Stop, "STOP"),
        (LocationKind::Address, "ADDRESS"),
        (LocationKind::Poi, "PLACE"),
    ]
    .into_iter()
    .filter(|(kind, _)| query.kinds.contains(kind))
    .map(|(_, name)| name)
    .collect();
    if types.is_empty() && !query.kinds.is_empty() {
        return Err(Error::InvalidQuery {
            message: "MOTIS has no locations of the kinds asked for".to_owned(),
        });
    }
    let types = types.join(",");
    let mut parameters = vec![("text", query.text.as_str())];
    if !types.is_empty() {
        parameters.push(("type", &types));
    }
    if let Some(limit) = &limit {
        parameters.push(("numResults", limit));
    }
    Ok(HttpRequest::get(
        &network.endpoint,
        "v1/geocode",
        parameters,
    ))
}

pub(crate) fn parse_locations(response: &HttpResponse) -> Result<Vec<Location>, Error> {
    let matches: Vec<Match> = response.json()?;
    Ok(matches.into_iter().map(Location::from).collect())
}

#[derive(Deserialize)]
struct Match {
    #[serde(rename = "type")]
    kind: String,
    name: String,
    id: String,
    lat: f64,
    lon: f64,
    tz: Option<String>,
    #[serde(default)]
    areas: Vec<Area>,
}

#[derive(Deserialize)]
struct Area {
    name: String,
    /// Marks the area to show next to the name, usually the municipality.
    #[serde(default)]
    default: bool,
}

fn non_empty(text: Option<String>) -> Option<String> {
    text.filter(|text| !text.is_empty())
}

impl From<Match> for Location {
    fn from(wire: Match) -> Self {
        let kind = match wire.kind.as_str() {
            "STOP" => LocationKind::Stop,
            "ADDRESS" => LocationKind::Address,
            "PLACE" => LocationKind::Poi,
            _ => LocationKind::Other,
        };
        Self {
            // Other kinds carry OpenStreetMap identifiers.
            global_ids: (kind == LocationKind::Stop)
                .then(|| global_id(&wire.id))
                .flatten()
                .into_iter()
                .collect(),
            id: wire.id,
            kind,
            name: wire.name,
            locality: wire
                .areas
                .into_iter()
                .find(|area| area.default)
                .map(|area| area.name),
            coordinate: Some(Coordinate {
                latitude: wire.lat,
                longitude: wire.lon,
            }),
            timezone: wire.tz,
            network: None,
        }
    }
}

/// The IFOPT identifier inside a stop ID of the form `<feed>_<id in feed>`.
/// Feed names and IFOPT identifiers may both contain underscores, so every
/// split is tried.
fn global_id(id: &str) -> Option<GlobalId> {
    id.match_indices('_')
        .filter_map(|(index, _)| id.get(index + 1..))
        .find_map(GlobalId::ifopt)
}

pub(crate) fn departures_request(
    network: &Network,
    query: &DeparturesQuery,
) -> Result<HttpRequest, Error> {
    // The server needs `n` or a time window.
    let limit = query.limit.unwrap_or(10).to_string();
    let time = query
        .time
        .map(|time| time.to_jiff().map(|time| time.to_string()))
        .transpose()?;
    let stop = query.stop.stop_id(network)?;
    let mut parameters = vec![("stopId", stop.as_str()), ("n", limit.as_str())];
    if let Some(time) = &time {
        parameters.push(("time", time));
    }
    Ok(HttpRequest::get(
        &network.endpoint,
        "v6/stoptimes",
        parameters,
    ))
}

pub(crate) fn parse_departures(response: &HttpResponse) -> Result<Vec<Departure>, Error> {
    let response: StopTimesResponse = json(response)?;
    let mut departures = response
        .stop_times
        .into_iter()
        .map(StopTime::into_departure)
        .collect::<Result<Vec<_>, _>>()?;
    departures.sort_by_key(Departure::expected_time);
    Ok(departures)
}

pub(crate) fn trips_request(network: &Network, query: &TripsQuery) -> Result<HttpRequest, Error> {
    let from = place(query.origin.target(network)?);
    let to = place(query.destination.target(network)?);
    let limit = query.limit.map(|limit| limit.to_string());
    let (time, arrive_by) = match query.time {
        Some(TripTime::DepartAt(time)) => (Some(time), false),
        Some(TripTime::ArriveBy(time)) => (Some(time), true),
        None => (None, false),
    };
    let time = time
        .map(|time| time.to_jiff().map(|time| time.to_string()))
        .transpose()?;

    let mut parameters = vec![("fromPlace", from.as_str()), ("toPlace", to.as_str())];
    if let Some(limit) = &limit {
        // The first is the least number of trips. A page would hold all the
        // trips of two hours without the second.
        parameters.extend([
            ("numItineraries", limit.as_str()),
            ("maxItineraries", limit),
        ]);
    }
    if let Some(time) = &time {
        parameters.push(("time", time));
    }
    if arrive_by {
        parameters.push(("arriveBy", "true"));
    }
    // The server asks for the cursor on top of the request it answered.
    if let Some(page) = &query.page {
        parameters.push(("pageCursor", page));
    }
    Ok(HttpRequest::get(&network.endpoint, "v6/plan", parameters))
}

fn place(place: Target) -> String {
    match place {
        Target::Id(id) => id,
        Target::Coordinate(coordinate) => coordinate_id(coordinate),
    }
}

/// The form in which the server takes a position as a place.
fn coordinate_id(coordinate: Coordinate) -> String {
    format!("{},{}", coordinate.latitude, coordinate.longitude)
}

pub(crate) fn parse_trips(response: &HttpResponse) -> Result<Trips, Error> {
    let response: PlanResponse = json(response)?;
    let mut trips = Vec::new();
    for itinerary in response.direct.into_iter().chain(response.itineraries) {
        let legs = itinerary
            .legs
            .into_iter()
            .map(WireLeg::into_leg)
            .collect::<Result<Vec<_>, _>>()?;
        if !legs.is_empty() {
            trips.push(Trip { legs });
        }
    }
    Ok(Trips {
        trips,
        earlier: response.previous_page_cursor,
        later: response.next_page_cursor,
    })
}

/// Parses a response. The server answers an unknown stop with status 404
/// and a JSON body, unlike a wrong endpoint.
fn json<'a, T: Deserialize<'a>>(response: &'a HttpResponse) -> Result<T, Error> {
    if response.status == 404
        && let Ok(ErrorBody { error }) = serde_json::from_slice(&response.body)
    {
        return Err(Error::NotFound { message: error });
    }
    response.json()
}

#[derive(Deserialize)]
struct ErrorBody {
    error: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct StopTimesResponse {
    stop_times: Vec<StopTime>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct PlanResponse {
    /// Trips without public transport, such as a walk all the way.
    #[serde(default)]
    direct: Vec<Itinerary>,
    itineraries: Vec<Itinerary>,
    previous_page_cursor: Option<String>,
    next_page_cursor: Option<String>,
}

#[derive(Deserialize)]
struct Itinerary {
    legs: Vec<WireLeg>,
}

/// What a stop time and a leg of a trip say about the vehicle.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Vehicle {
    mode: String,
    /// Whether the times are predictions. Without one the server repeats
    /// the scheduled times in the fields for the predicted ones.
    #[serde(default)]
    real_time: bool,
    headsign: Option<String>,
    agency_name: Option<String>,
    display_name: Option<String>,
    route_short_name: Option<String>,
    #[serde(default)]
    cancelled: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct StopTime {
    place: WirePlace,
    #[serde(flatten)]
    vehicle: Vehicle,
    #[serde(default)]
    trip_cancelled: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct WireLeg {
    from: WirePlace,
    to: WirePlace,
    #[serde(flatten)]
    vehicle: Vehicle,
    /// The times of the two ends. The specification requires them here and
    /// not on the places, which carry them too in every recorded response.
    scheduled_start_time: Option<WireTime>,
    scheduled_end_time: Option<WireTime>,
    /// The calls between the two ends.
    #[serde(default)]
    intermediate_stops: Vec<WirePlace>,
}

/// A place, with the times of the call there where it is part of a ride.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct WirePlace {
    /// Missing for places that are not stops, such as the start of a trip
    /// from a coordinate.
    stop_id: Option<String>,
    name: String,
    lat: f64,
    lon: f64,
    tz: Option<String>,
    arrival: Option<WireTime>,
    scheduled_arrival: Option<WireTime>,
    departure: Option<WireTime>,
    scheduled_departure: Option<WireTime>,
    track: Option<String>,
    scheduled_track: Option<String>,
    #[serde(default)]
    cancelled: bool,
}

impl WirePlace {
    fn into_stopover(self, real_time: bool) -> Stopover {
        let predicted = |time: Option<WireTime>| time.filter(|_| real_time).map(|time| time.0);
        let coordinate = Coordinate {
            latitude: self.lat,
            longitude: self.lon,
        };
        Stopover {
            scheduled_arrival: self.scheduled_arrival.map(|time| time.0),
            realtime_arrival: predicted(self.arrival),
            scheduled_departure: self.scheduled_departure.map(|time| time.0),
            realtime_departure: predicted(self.departure),
            scheduled_platform: non_empty(self.scheduled_track),
            realtime_platform: non_empty(self.track),
            cancelled: self.cancelled,
            stop: Location {
                global_ids: self
                    .stop_id
                    .as_deref()
                    .and_then(global_id)
                    .into_iter()
                    .collect(),
                kind: if self.stop_id.is_some() {
                    LocationKind::Stop
                } else {
                    LocationKind::Other
                },
                id: self.stop_id.unwrap_or_else(|| coordinate_id(coordinate)),
                name: self.name,
                locality: None,
                coordinate: Some(coordinate),
                timezone: self.tz,
                network: None,
            },
        }
    }
}

impl Vehicle {
    fn into_line(self, mode: Mode) -> (Line, Option<String>) {
        let line = Line {
            name: non_empty(self.display_name)
                .or(non_empty(self.route_short_name))
                .unwrap_or_default(),
            mode,
            product: None,
            operator: non_empty(self.agency_name),
        };
        (line, non_empty(self.headsign))
    }
}

impl StopTime {
    fn into_departure(self) -> Result<Departure, Error> {
        let cancelled = self.vehicle.cancelled || self.trip_cancelled || self.place.cancelled;
        if self.place.stop_id.is_none() {
            return Err(Error::Parse {
                message: "a stop time has no stop".to_owned(),
            });
        }
        let stopover = self.place.into_stopover(self.vehicle.real_time);
        let mode = transit_mode(&self.vehicle.mode).unwrap_or(Mode::Other);
        let (line, direction) = self.vehicle.into_line(mode);
        Ok(Departure {
            stop: stopover.stop,
            line,
            direction,
            scheduled_time: stopover.scheduled_departure.ok_or_else(|| Error::Parse {
                message: "a stop time has no scheduled departure".to_owned(),
            })?,
            realtime_time: stopover.realtime_departure,
            scheduled_platform: stopover.scheduled_platform,
            realtime_platform: stopover.realtime_platform,
            cancelled,
        })
    }
}

impl WireLeg {
    fn into_leg(self) -> Result<Leg, Error> {
        let wire = self;
        let no_time = || Error::Parse {
            message: "a leg has no scheduled time at one of its ends".to_owned(),
        };
        let real_time = wire.vehicle.real_time;
        let mut from = wire.from.into_stopover(real_time);
        from.scheduled_departure = from
            .scheduled_departure
            .or(wire.scheduled_start_time.map(|time| time.0));
        let departure = from.into_leg_start().ok_or_else(no_time)?;
        let mut to = wire.to.into_stopover(real_time);
        to.scheduled_arrival = to
            .scheduled_arrival
            .or(wire.scheduled_end_time.map(|time| time.0));
        let arrival = to.into_leg_end().ok_or_else(no_time)?;
        Ok(Leg {
            cancelled: wire.vehicle.cancelled || departure.cancelled || arrival.cancelled,
            departure,
            arrival,
            intermediate_stops: wire
                .intermediate_stops
                .into_iter()
                .map(|place| place.into_stopover(real_time))
                .collect(),
            kind: match wire.vehicle.mode.as_str() {
                "WALK" => LegKind::Walk,
                // The other modes the server lists as street modes.
                "BIKE" | "RENTAL" | "CAR" | "HGV" | "CAR_PARKING" | "CAR_DROPOFF"
                | "RIDE_SHARING" | "FLEX" => LegKind::Other,
                mode => {
                    let mode = transit_mode(mode).unwrap_or(Mode::Other);
                    let (line, direction) = wire.vehicle.into_line(mode);
                    LegKind::Transit { line, direction }
                }
            },
        })
    }
}

/// The mode of a public transport vehicle. `None` for modes this crate does
/// not know.
fn transit_mode(mode: &str) -> Option<Mode> {
    Some(match mode {
        "HIGHSPEED_RAIL" => Mode::HighSpeedTrain,
        "LONG_DISTANCE" => Mode::LongDistanceTrain,
        "NIGHT_RAIL" => Mode::NightTrain,
        "REGIONAL_RAIL" | "REGIONAL_FAST_RAIL" => Mode::RegionalTrain,
        "SUBURBAN" => Mode::SuburbanTrain,
        "SUBWAY" => Mode::Subway,
        "TRAM" => Mode::Tram,
        "BUS" => Mode::Bus,
        "COACH" => Mode::Coach,
        "FERRY" => Mode::Ferry,
        "AIRPLANE" => Mode::Airplane,
        "FUNICULAR" => Mode::Funicular,
        "AERIAL_LIFT" | "AREAL_LIFT" | "CABLE_CAR" => Mode::CableCar,
        "ODM" => Mode::OnDemand,
        _ => return None,
    })
}