gistools/readers/gtfs/realtime/
header.rs

1use crate::util::Date;
2use alloc::string::String;
3use pbf::{BitCast, ProtoRead, Protobuf};
4
5/// Determines whether the current fetch is incremental. Currently,
6/// DIFFERENTIAL mode is unsupported and behavior is unspecified for feeds
7/// that use this mode.  There are discussions on the GTFS Realtime mailing
8/// list around fully specifying the behavior of DIFFERENTIAL mode and the
9/// documentation will be updated when those discussions are finalized.
10#[repr(u8)]
11#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, BitCast)]
12pub enum GTFSIncrementality {
13    /// Full dataset
14    #[default]
15    FullDataset = 0,
16    /// Differential
17    Differential = 1,
18}
19
20/// Metadata about a feed, included in feed messages.
21#[derive(Debug, Clone, PartialEq)]
22pub struct GTFSRealtimeHeader {
23    /// Version of the feed specification.
24    /// The current version is 2.0.  Valid versions are "2.0", "1.0".
25    pub gtfs_realtime_version: String, // 1 [string]
26    /// Determines whether the current fetch is incremental. Currently,
27    /// DIFFERENTIAL mode is unsupported and behavior is unspecified for feeds
28    /// that use this mode.  There are discussions on the GTFS Realtime mailing
29    /// list around fully specifying the behavior of DIFFERENTIAL mode and the
30    /// documentation will be updated when those discussions are finalized.
31    pub incrementality: GTFSIncrementality, // 2 [enum]
32    /// This timestamp identifies the moment when the content of this feed has been
33    /// created (in server time). In POSIX time (i.e., number of seconds since
34    /// January 1st 1970 00:00:00 UTC).
35    pub timestamp: Option<Date>, // 3 [uint64]
36    /// String that matches the feed_info.feed_version from the GTFS feed that the real
37    /// time data is based on. Consumers can use this to identify which GTFS feed is
38    /// currently active or when a new one is available to download.
39    pub feed_version: Option<String>, // 4 [string]
40}
41impl Default for GTFSRealtimeHeader {
42    fn default() -> Self {
43        Self {
44            gtfs_realtime_version: "2.0".into(),
45            incrementality: GTFSIncrementality::FullDataset,
46            timestamp: None,
47            feed_version: None,
48        }
49    }
50}
51/// Read in the contents of the GTFSRealtimeHeader
52impl ProtoRead for GTFSRealtimeHeader {
53    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
54        match tag {
55            1 => self.gtfs_realtime_version = pb.read_string(),
56            2 => self.incrementality = pb.read_varint(),
57            3 => self.timestamp = Some(Date::from_time(pb.read_varint::<u64>() as i64 * 1000)),
58            4 => self.feed_version = Some(pb.read_string()),
59            _ => panic!("unknown tag {}", tag),
60        }
61    }
62}