gistools/readers/gtfs/realtime/
mod.rs

1mod alert;
2mod entity;
3mod header;
4mod position;
5mod shape;
6mod stop;
7mod trip;
8mod util;
9mod vehicle_position;
10
11pub use alert::*;
12use alloc::vec::Vec;
13pub use entity::*;
14pub use header::*;
15use pbf::{ProtoRead, Protobuf};
16pub use position::*;
17pub use shape::*;
18pub use stop::*;
19pub use trip::*;
20pub use util::*;
21pub use vehicle_position::*;
22
23/// # GTFS Realtime message.
24///
25/// ## Description
26/// The input is a Uint8Array that has encoded protobuffer messages.
27/// See <https://open-s2.github.io/pbf/classes/PbfReader.html>.
28///
29/// The contents of a feed message.
30/// A feed is a continuous stream of feed messages. Each message in the stream is
31/// obtained as a response to an appropriate HTTP GET request.
32/// A realtime feed is always defined with relation to an existing GTFS feed.
33/// All the entity ids are resolved with respect to the GTFS feed.
34/// Note that "required" and "optional" as stated in this file refer to Protocol
35/// Buffer cardinality, not semantic cardinality.  See reference.md at
36/// <https://github.com/google/transit/tree/master/gtfs-realtime> for field
37/// semantic cardinality.
38///
39/// ## Usage
40///
41/// The methods you have access to:
42/// - [`GTFSRealtimeReader::new`]: Create a new GTFSRealtimeReader
43///
44/// ```rust
45/// use gistools::readers::GTFSRealtimeReader;
46/// use std::path::PathBuf;
47///  
48/// let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
49/// path.push("tests/readers/gtfs/fixtures/vehicle_position.pb");
50///
51/// let data = std::fs::read(path).unwrap();
52/// let reader = GTFSRealtimeReader::new(data, None);
53///
54/// let entities = &reader.entities;
55/// assert_eq!(entities.len(), 1);
56/// ```
57///
58/// ## Links
59/// - <https://mobilitydatabase.org>
60/// - <https://developers.google.com/transit/gtfs/examples/overview>
61/// - <https://mobilitydata.github.io/>
62/// - <https://www.transit.land>
63#[derive(Debug, Default, Clone, PartialEq)]
64pub struct GTFSRealtimeReader {
65    /// The header of the message
66    pub header: GTFSRealtimeHeader,
67    /// The entities in the message
68    pub entities: Vec<GTFSRealtimeEntity>,
69}
70impl GTFSRealtimeReader {
71    /// Create a new GTFSRealtimeReader
72    pub fn new(data: Vec<u8>, end: Option<usize>) -> Self {
73        let mut this = Self::default();
74        let mut pbf = Protobuf::from_input(data);
75        pbf.read_fields(&mut this, end);
76        this
77    }
78}
79/// Read in the contents of the GTFSRealtimeReader
80impl ProtoRead for GTFSRealtimeReader {
81    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
82        match tag {
83            1 => {
84                let mut header = GTFSRealtimeHeader::default();
85                pb.read_message(&mut header);
86                self.header = header;
87            }
88            2 => {
89                let mut entity = GTFSRealtimeEntity::default();
90                pb.read_message(&mut entity);
91                self.entities.push(entity);
92            }
93            _ => panic!("unknown tag {}", tag),
94        }
95    }
96}