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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Networks as data: entries in the format of the `transport-apis`
//! repository, and the set that ships with the crate.

use std::collections::BTreeMap;

use serde::Deserialize;

use crate::{Coordinate, Error, HafasOptions, HafasProduct, Network, Protocol};

/// One of the three things a [`Network`] can be asked.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub enum Operation {
    /// A search for locations.
    Locations,
    /// The departures at a stop.
    Departures,
    /// Trips between two places.
    Trips,
}

/// Where a [`Network`] has data, in three tiers of quality.
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub struct Coverage {
    /// Where the network has real-time data.
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(skip_serializing_if = "Option::is_none")
    )]
    pub realtime: Option<Area>,
    /// Where it has complete timetables.
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(skip_serializing_if = "Option::is_none")
    )]
    pub regular: Option<Area>,
    /// Where it has any data.
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(skip_serializing_if = "Option::is_none")
    )]
    pub any: Option<Area>,
}

/// A part of the world, described by region codes, by polygons or by both.
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub struct Area {
    /// ISO 3166 codes of countries (`DE`) or their subdivisions (`DE-SN`).
    #[cfg_attr(feature = "uniffi", uniffi(default = []))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(default, deserialize_with = "crate::wasm::or_default")
    )]
    pub regions: Vec<String>,
    /// The outlines of the area.
    #[cfg_attr(feature = "uniffi", uniffi(default = []))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(default, deserialize_with = "crate::wasm::or_default")
    )]
    pub polygons: Vec<Polygon>,
}

/// A polygon as in `GeoJSON`: the first ring is the outline, further rings
/// are holes in it.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub struct Polygon {
    /// Each ring is a closed line of positions.
    pub rings: Vec<Vec<Coordinate>>,
}

/// A tier of a [`Coverage`], best first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub enum CoverageTier {
    /// Real-time data.
    Realtime,
    /// Complete timetables.
    Regular,
    /// Any data.
    Any,
}

/// Whom to name as the source of a [`Network`]'s data.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub struct Attribution {
    /// The name of the source.
    pub name: String,
    /// Its website.
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(skip_serializing_if = "Option::is_none")
    )]
    pub homepage: Option<String>,
    /// The license of the data, where the source has a single one.
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    #[cfg_attr(
        feature = "unstable-wasm",
        serde(skip_serializing_if = "Option::is_none")
    )]
    pub license: Option<String>,
}

impl Attribution {
    /// An attribution with a name only.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            homepage: None,
            license: None,
        }
    }
}

impl Area {
    /// Whether one of the polygons contains the position.
    fn contains(&self, position: Coordinate) -> bool {
        self.polygons
            .iter()
            .any(|polygon| polygon.contains(position))
    }
}

impl Polygon {
    /// Counts the edges of all rings that a line from the position to the
    /// east crosses. An odd count means inside the outline and outside
    /// every hole.
    fn contains(&self, position: Coordinate) -> bool {
        let crossings = self
            .rings
            .iter()
            .flat_map(|ring| ring.iter().zip(ring.iter().cycle().skip(1)))
            .filter(|(from, to)| {
                (from.latitude > position.latitude) != (to.latitude > position.latitude) && {
                    let along = (position.latitude - from.latitude) / (to.latitude - from.latitude);
                    position.longitude < from.longitude + along * (to.longitude - from.longitude)
                }
            })
            .count();
        crossings % 2 == 1
    }
}

#[cfg_attr(feature = "uniffi", uniffi::export)]
impl Coverage {
    /// The best tier that has a polygon containing the position. Region
    /// codes are not used: the crate has no borders to resolve them with.
    #[must_use]
    pub fn tier_at(&self, position: Coordinate) -> Option<CoverageTier> {
        [
            (&self.realtime, CoverageTier::Realtime),
            (&self.regular, CoverageTier::Regular),
            (&self.any, CoverageTier::Any),
        ]
        .into_iter()
        .find(|(area, _)| area.as_ref().is_some_and(|area| area.contains(position)))
        .map(|(_, tier)| tier)
    }
}

impl Network {
    /// Reads a network from an entry in the JSON format of the
    /// [transport-apis](https://github.com/public-transport/transport-apis)
    /// repository. `id` is the name of the entry's file without `.json`.
    ///
    /// The crate reads four fields that repository does not have: the type
    /// `ojp` with `options.ojpVersion`, `options.requestorRef`, and
    /// `usageTerms` for [`Network::terms`]. An entry of the type
    /// `otpGraphQl` is read if it is a Transmodel server, which the entry
    /// shows by `options.apiVersion` being `entur` or by `transmodel` in its
    /// endpoint. The crate reads [`Network::geocoder`] from the field
    /// `options.geocoder`, which that repository does not have, so its
    /// entries give a network without one. An entry of the type
    /// `hafasMgate` gives a network with [`Network::hafas`] that asks in the
    /// first of the entry's `supportedLanguages`. Its salts for signed
    /// requests are not read: the crate does not sign, and no server that
    /// was tried asks for it. For
    /// MOTIS the entry names the root of the server, and the endpoint gets
    /// `api` appended unless it ends in it.
    ///
    /// An entry is taken at its word. Whether the server answers is only
    /// known for the [bundled](Registry::bundled) networks. Entries from
    /// that repository often lack what a request needs later: EFA entries
    /// the time zone, TRIAS entries the requestor reference. The versions
    /// they declare are not checked either, because they are often out of
    /// date, and nothing there says whether an EFA server has the JSON
    /// output this crate uses.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidNetwork`] if the text is not such an entry,
    /// if its protocol is not one of this crate's, or if the server wants a
    /// key in an HTTP header.
    pub fn from_json(id: &str, json: &str) -> Result<Self, Error> {
        let invalid = |message: String| Error::InvalidNetwork { message };
        let entry: Entry =
            serde_json::from_str(json).map_err(|error| invalid(format!("{id}: {error}")))?;
        let mut options = entry.options;

        let mut types = entry.kind.iter().filter(|(_, enabled)| **enabled);
        let protocol = match (types.next().map(|(name, _)| name.as_str()), types.next()) {
            (Some("efa"), None) => Protocol::Efa,
            (Some("motis"), None) => Protocol::Motis,
            (Some("trias"), None) => Protocol::Trias,
            (Some("ojp"), None) => Protocol::Ojp1,
            (Some("hafasMgate"), None) => Protocol::HafasMgate,
            // The type covers both APIs of OTP, and only Entur's entry
            // says which one it is.
            (Some("otpGraphQl"), None)
                if options.api_version.as_deref() == Some("entur")
                    || options.endpoint.contains("transmodel") =>
            {
                Protocol::OtpTransmodel
            }
            (Some("otpGraphQl"), None) => {
                return Err(invalid(format!(
                    "{id}: of OpenTripPlanner, only the Transmodel API is supported"
                )));
            }
            _ => {
                return Err(invalid(format!(
                    "{id}: needs exactly one of the types efa, motis, trias, ojp, hafasMgate and otpGraphQl"
                )));
            }
        };
        let hafas = (protocol == Protocol::HafasMgate)
            .then(|| options.hafas(id, entry.supported_languages))
            .transpose()?;
        if protocol == Protocol::Ojp1 {
            // Written as text here, as a number for TRIAS upstream.
            let version = options.ojp_version.as_ref().map(ToString::to_string);
            if !matches!(version.as_deref(), Some("\"1.0\"" | "1.0")) {
                return Err(invalid(format!("{id}: only OJP 1.0 is supported")));
            }
        }
        // The other method is an HTTP header, which a network cannot carry.
        if options
            .authorization_method
            .is_some_and(|method| method != "RequestorRef")
        {
            return Err(invalid(format!(
                "{id}: the server takes its key in a way this crate cannot send"
            )));
        }
        let endpoint = if protocol == Protocol::Motis {
            let root = options.endpoint.trim_end_matches('/');
            if root.ends_with("/api") {
                root.to_owned()
            } else {
                format!("{root}/api")
            }
        } else {
            options.endpoint
        };

        let xml = matches!(protocol, Protocol::Trias | Protocol::Ojp1);
        let unsupported =
            options
                .provided_services
                .filter(|_| xml)
                .map_or_else(Vec::new, |provided| {
                    [
                        ("LocationInformationRequest", Operation::Locations),
                        ("StopEventRequest", Operation::Departures),
                        ("TripRequest", Operation::Trips),
                    ]
                    .into_iter()
                    .filter(|(service, _)| !provided.iter().any(|name| name == service))
                    .map(|(_, operation)| operation)
                    .collect()
                });

        let coverage = entry.coverage.unwrap_or_default();
        let area = |area: Option<WireArea>| area.map(|area| area.into_area(id)).transpose();
        Ok(Self {
            id: id.to_owned(),
            name: entry.name,
            endpoint,
            protocol,
            timezone: entry.timezone,
            requestor_ref: options.requestor_ref,
            coverage: Coverage {
                realtime: area(coverage.realtime)?,
                regular: area(coverage.regular)?,
                any: area(coverage.any)?,
            },
            attribution: entry.attribution.map(|attribution| Attribution {
                name: attribution.name,
                homepage: attribution.homepage,
                license: attribution.license,
            }),
            unsupported,
            requires_key: xml && options.requires_sign_up,
            terms: entry.usage_terms.filter(|terms| !terms.is_empty()),
            headers: Vec::new(),
            geocoder: options.geocoder.filter(|url| !url.is_empty()),
            hafas,
        })
    }
}

#[derive(Deserialize)]
struct Entry {
    name: String,
    #[serde(rename = "type")]
    kind: BTreeMap<String, bool>,
    coverage: Option<WireCoverage>,
    options: Options,
    timezone: Option<String>,
    attribution: Option<WireAttribution>,
    #[serde(rename = "usageTerms")]
    usage_terms: Option<String>,
    #[serde(rename = "supportedLanguages", default)]
    supported_languages: Vec<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Options {
    endpoint: String,
    authorization_method: Option<String>,
    ojp_version: Option<serde_json::Value>,
    requestor_ref: Option<String>,
    #[serde(default)]
    requires_sign_up: bool,
    provided_services: Option<Vec<String>>,
    api_version: Option<String>,
    geocoder: Option<String>,
    client: Option<serde_json::Value>,
    auth: Option<serde_json::Value>,
    ext: Option<String>,
    ver: Option<String>,
    version: Option<String>,
    #[serde(default)]
    products: Vec<WireProduct>,
}

impl Options {
    /// What a HAFAS mgate entry says about its server. Takes the values
    /// out of the options.
    fn hafas(&mut self, id: &str, languages: Vec<String>) -> Result<HafasOptions, Error> {
        let invalid = |name: &str| Error::InvalidNetwork {
            message: format!("{id}: needs options.{name}"),
        };
        let object = |value: Option<serde_json::Value>, name: &str| {
            value
                .filter(serde_json::Value::is_object)
                .map(|value| value.to_string())
                .ok_or_else(|| invalid(name))
        };
        Ok(HafasOptions {
            client: object(self.client.take(), "client")?,
            auth: object(self.auth.take(), "auth")?,
            ext: self.ext.take(),
            // A server rejects a request without a version, and newer
            // servers still answer this old one.
            version: self
                .ver
                .take()
                .or_else(|| self.version.take())
                .unwrap_or_else(|| "1.16".to_owned()),
            language: languages
                .into_iter()
                .next()
                .unwrap_or_else(|| "en".to_owned()),
            products: std::mem::take(&mut self.products)
                .into_iter()
                .map(|product| HafasProduct {
                    id: product.id,
                    name: product.name,
                    bitmasks: product.bitmasks,
                })
                .collect(),
        })
    }
}

#[derive(Deserialize)]
struct WireProduct {
    id: String,
    #[serde(default)]
    name: String,
    bitmasks: Vec<u32>,
}

#[derive(Deserialize, Default)]
struct WireCoverage {
    #[serde(rename = "realtimeCoverage")]
    realtime: Option<WireArea>,
    #[serde(rename = "regularCoverage")]
    regular: Option<WireArea>,
    #[serde(rename = "anyCoverage")]
    any: Option<WireArea>,
}

#[derive(Deserialize)]
struct WireArea {
    #[serde(default)]
    region: Vec<String>,
    area: Option<Geometry>,
}

/// A `GeoJSON` geometry. A ring is a list of positions, and a position is a
/// longitude, a latitude and sometimes more.
#[derive(Deserialize)]
#[serde(tag = "type")]
enum Geometry {
    Polygon {
        coordinates: Vec<Vec<Vec<f64>>>,
    },
    MultiPolygon {
        coordinates: Vec<Vec<Vec<Vec<f64>>>>,
    },
}

#[derive(Deserialize)]
struct WireAttribution {
    name: String,
    homepage: Option<String>,
    license: Option<String>,
}

impl WireArea {
    fn into_area(self, id: &str) -> Result<Area, Error> {
        let polygon = |rings: Vec<Vec<Vec<f64>>>| {
            let rings = rings
                .into_iter()
                .map(|ring| {
                    ring.into_iter()
                        .map(|position| match position.as_slice() {
                            [longitude, latitude, ..] => Ok(Coordinate {
                                latitude: *latitude,
                                longitude: *longitude,
                            }),
                            _ => Err(Error::InvalidNetwork {
                                message: format!("{id}: a position needs two numbers"),
                            }),
                        })
                        .collect()
                })
                .collect::<Result<_, _>>()?;
            Ok(Polygon { rings })
        };
        let polygons = match self.area {
            None => Vec::new(),
            Some(Geometry::Polygon { coordinates }) => vec![polygon(coordinates)?],
            Some(Geometry::MultiPolygon { coordinates }) => coordinates
                .into_iter()
                .map(polygon)
                .collect::<Result<_, Error>>()?,
        };
        Ok(Area {
            regions: self.region,
            polygons,
        })
    }
}

/// A network that covers a position, see [`Registry::covering`].
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "uniffi", uniffi::export(Debug))]
#[cfg_attr(
    feature = "unstable-wasm",
    derive(serde::Serialize, serde::Deserialize, tsify::Tsify),
    serde(rename_all = "camelCase")
)]
pub struct CoveringNetwork {
    /// The network.
    pub network: Network,
    /// The best tier of its coverage at the position.
    pub tier: CoverageTier,
}

/// Declares the networks that ship with the crate: the enum that names
/// them and the table of their entries, from one list.
macro_rules! bundled {
    ($($variant:ident => $id:literal, $entry:literal;)*) => {
        /// A network that ships with the library. Its name is its ID,
        /// `vvo-efa`, in the spelling the language has for such names.
        /// Each network was checked against its live server when it was
        /// added, and leaves the list when its server is gone for good.
        // No export of `Debug`: the other languages then print the name as
        // they spell it.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[non_exhaustive]
        #[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
        #[cfg_attr(feature = "unstable-wasm", derive(serde::Serialize, serde::Deserialize, tsify::Tsify))]
        pub enum BundledNetwork {
            $(
                // The documentation of a network is its ID. UniFFI cannot
                // read a `concat!` there that would make a sentence of it.
                #[doc = $id]
                #[cfg_attr(feature = "unstable-wasm", serde(rename = $id))]
                $variant,
            )*
        }

        impl BundledNetwork {
            /// Every bundled network, in the order of the registry.
            pub const ALL: &'static [Self] = &[$(Self::$variant,)*];

            /// The ID of the network, such as `vvo-efa`.
            #[must_use]
            pub fn id(self) -> &'static str {
                match self {
                    $(Self::$variant => $id,)*
                }
            }

            /// The entry of the network in the format of transport-apis.
            fn entry(self) -> &'static str {
                match self {
                    $(Self::$variant => include_str!($entry),)*
                }
            }

            /// Where the entry is, to check it against the ID.
            #[cfg(test)]
            fn entry_path(self) -> &'static str {
                match self {
                    $(Self::$variant => $entry,)*
                }
            }
        }
    };
}

// The entries come from the transport-apis repository, a submodule, where
// it has a usable entry. Those in `data/networks` are waiting to get there,
// or need the fields that only this crate reads.
bundled! {
    VvoEfa => "vvo-efa", "../data/networks/vvo-efa.json";
    VvoTrias => "vvo-trias", "../data/networks/vvo-trias.json";
    VvoOjp => "vvo-ojp", "../data/networks/vvo-ojp.json";
    VrrEfa => "vrr-efa", "../data/networks/vrr-efa.json";
    VrrTrias => "vrr-trias", "../data/networks/vrr-trias.json";
    VrrOjp => "vrr-ojp", "../data/networks/vrr-ojp.json";
    OebbHafas => "oebb-hafas", "../data/transport-apis/data/at/oebb-hafas-mgate.json";
    OoevvHafas => "ooevv-hafas", "../data/transport-apis/data/at/ooevv-hafas-mgate.json";
    SalzburgHafas => "salzburg-hafas", "../data/transport-apis/data/at/salzburg-hafas-mgate.json";
    StvHafas => "stv-hafas", "../data/transport-apis/data/at/stv-hafas-mgate.json";
    SvvHafas => "svv-hafas", "../data/transport-apis/data/at/svv-hafas-mgate.json";
    VkgHafas => "vkg-hafas", "../data/transport-apis/data/at/vkg-hafas-mgate.json";
    VorHafas => "vor-hafas", "../data/transport-apis/data/at/vor-hafas-mgate.json";
    VvtHafas => "vvt-hafas", "../data/transport-apis/data/at/vvt-hafas-mgate.json";
    VvvHafas => "vvv-hafas", "../data/transport-apis/data/at/vvv-hafas-mgate.json";
    TpgHafas => "tpg-hafas", "../data/transport-apis/data/ch/tpg-hafas-mgate.json";
    AvvHafas => "avv-hafas", "../data/transport-apis/data/de/avv-hafas-mgate.json";
    BvgHafas => "bvg-hafas", "../data/transport-apis/data/de/bvg-hafas-mgate.json";
    DartHafas => "dart-hafas", "../data/transport-apis/data/us/dart-hafas-mgate.json";
    DbBusradarNrwHafas => "db-busradar-nrw-hafas", "../data/transport-apis/data/de/db-busradar-nrw-hafas-mgate.json";
    DbSbahnMuenchenHafas => "db-sbahn-muenchen-hafas", "../data/transport-apis/data/de/db-sbahn-muenchen-hafas-mgate.json";
    DbSmartrblHafas => "db-smartrbl-hafas", "../data/transport-apis/data/de/db-smartrbl-hafas-mgate.json";
    GvhHafas => "gvh-hafas", "../data/transport-apis/data/de/gvh-hafas-mgate.json";
    InvgHafas => "invg-hafas", "../data/transport-apis/data/de/invg-hafas-mgate.json";
    KvbHafas => "kvb-hafas", "../data/transport-apis/data/de/kvb-hafas-mgate.json";
    NahshHafas => "nahsh-hafas", "../data/transport-apis/data/de/nahsh-hafas-mgate.json";
    NasaHafas => "nasa-hafas", "../data/transport-apis/data/de/nasa-hafas-mgate.json";
    NvvHafas => "nvv-hafas", "../data/transport-apis/data/de/nvv-hafas-mgate.json";
    RmvHafas => "rmv-hafas", "../data/transport-apis/data/de/rmv-hafas-mgate.json";
    RsagHafas => "rsag-hafas", "../data/transport-apis/data/de/rsag-hafas-mgate.json";
    SaarvvHafas => "saarvv-hafas", "../data/transport-apis/data/de/saarvv-hafas-mgate.json";
    VbbHafas => "vbb-hafas", "../data/transport-apis/data/de/vbb-hafas-mgate.json";
    VbnHafas => "vbn-hafas", "../data/transport-apis/data/de/vbn-hafas-mgate.json";
    VmtHafas => "vmt-hafas", "../data/transport-apis/data/de/vmt-hafas-mgate.json";
    VosHafas => "vos-hafas", "../data/transport-apis/data/de/vos-hafas-mgate.json";
    VsnHafas => "vsn-hafas", "../data/transport-apis/data/de/vsn-hafas-mgate.json";
    RejseplanenHafas => "rejseplanen-hafas", "../data/transport-apis/data/dk/rejseplanen-hafas-mgate.json";
    IarnrodEireannHafas => "iarnrod-eireann-hafas", "../data/transport-apis/data/ie/iarnrod-eireann-hafas-mgate.json";
    MobiliteitLuHafas => "mobiliteit-lu-hafas", "../data/transport-apis/data/lu/mobiliteit-lu-hafas-mgate.json";
    ResrobotHafas => "resrobot-hafas", "../data/transport-apis/data/se/resrobot-hafas-mgate.json";
    BartHafas => "bart-hafas", "../data/transport-apis/data/us/bart-hafas-mgate.json";
    Entur => "entur", "../data/transport-apis/data/no/entur-otp.json";
    Transitous => "transitous", "../data/transport-apis/data/un/transitous.json";
}

impl BundledNetwork {
    /// The network, as [`Registry::bundled`] has it.
    pub(crate) fn network(self) -> Network {
        #[expect(clippy::expect_used, reason = "the entries are part of the crate")]
        let network =
            Network::from_json(self.id(), self.entry()).expect("a bundled entry can be read");
        corrected(network)
    }
}

impl Network {
    /// The network that ships with the crate under this name.
    ///
    /// # Panics
    ///
    /// If an entry of the crate cannot be read, which its tests rule out.
    #[must_use]
    pub fn bundled(network: BundledNetwork) -> Self {
        network.network()
    }
}

/// A set of networks to choose from.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct Registry {
    networks: Vec<Network>,
}

/// What the entries of bundled networks in transport-apis lack or have
/// wrong. A line leaves this function when transport-apis has the value.
fn corrected(mut network: Network) -> Network {
    // The address a server has moved to.
    let moved = match network.id.as_str() {
        "mobiliteit-lu-hafas" => Some("https://cdt.hafas.de/gate"),
        "ooevv-hafas" => Some("https://app.verkehrsauskunft.at/hamm/gate"),
        "salzburg-hafas" => Some("https://verkehrsauskunft.salzburg.gv.at/hamm/gate"),
        "stv-hafas" => Some("https://verkehrsauskunft.verbundlinie.at/hamm/gate"),
        "svv-hafas" => Some("https://fahrplan.salzburg-verkehr.at/hamm/gate"),
        "vkg-hafas" => Some("https://routenplaner.kaerntner-linien.at/hamm/gate"),
        "vor-hafas" => Some("https://anachb.vor.at/hamm/gate"),
        "vvv-hafas" => Some("https://fahrplan.vmobil.at/hamm/gate"),
        _ => None,
    };
    if let Some(endpoint) = moved {
        endpoint.clone_into(&mut network.endpoint);
    }
    match network.id.as_str() {
        // The entry names the time zone of Berlin for a server in Iowa.
        "dart-hafas" => network.timezone = Some("America/Chicago".to_owned()),
        // The format has no field for a geocoder or for usage terms, and
        // the entry names no time zone.
        "entur" => {
            network.geocoder = Some("https://api.entur.io/geocoder/v1".to_owned());
            network.timezone = Some("Europe/Oslo".to_owned());
        }
        "transitous" => network.terms = Some("https://transitous.org/api/".to_owned()),
        _ => {}
    }
    network
}

#[cfg_attr(feature = "uniffi", uniffi::export)]
impl Registry {
    /// The networks that ship with the crate. Each was checked against its
    /// live server when it was added.
    #[cfg_attr(feature = "uniffi", uniffi::constructor)]
    #[must_use]
    pub fn bundled() -> Self {
        let networks = BundledNetwork::ALL
            .iter()
            .map(|network| network.network())
            .collect();
        Self { networks }
    }

    /// A registry of the given networks, for example the bundled ones
    /// together with an application's own. If two share an ID,
    /// [`Registry::get`] finds the first.
    #[cfg_attr(feature = "uniffi", uniffi::constructor)]
    #[must_use]
    pub fn new(networks: Vec<Network>) -> Self {
        Self { networks }
    }

    /// All networks, in the order they were given.
    #[must_use]
    pub fn networks(&self) -> Vec<Network> {
        self.networks.clone()
    }

    /// The network with the given ID.
    #[must_use]
    pub fn get(&self, id: &str) -> Option<Network> {
        self.networks
            .iter()
            .find(|network| network.id == id)
            .cloned()
    }

    /// The networks with a coverage polygon that contains the position,
    /// see [`Coverage::tier_at`]. Those with the best tier come first, in
    /// the order of the registry within a tier. Coverage that an entry
    /// gives only as region codes is not found.
    #[must_use]
    pub fn covering(&self, position: Coordinate) -> Vec<CoveringNetwork> {
        let mut covering: Vec<_> = self
            .networks
            .iter()
            .filter_map(|network| {
                Some(CoveringNetwork {
                    tier: network.coverage.tier_at(position)?,
                    network: network.clone(),
                })
            })
            .collect();
        covering.sort_by_key(|covering| covering.tier);
        covering
    }
}

#[cfg(test)]
mod tests {
    use super::BundledNetwork;

    /// The macro pairs each ID with a file by hand.
    #[test]
    fn entry_of_a_bundled_network_is_the_file_of_its_id() {
        for network in BundledNetwork::ALL {
            let path = network.entry_path();
            let file = path.rsplit('/').next().unwrap_or(path);
            assert!(file.starts_with(network.id()), "{network:?}: {path}");
        }
    }
}