gistools/readers/gtfs/schedule/
calendar.rs

1use crate::{
2    readers::{csv::parse_csv_as_record, parse_gtfs_date},
3    util::Date,
4};
5use alloc::{string::String, vec::Vec};
6use s2json::MValueCompatible;
7
8/// Enumeration to represent day availability in the calendar.
9/// 0 = Not available, 1 = Available
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
11pub enum GTFSDayAvailability {
12    /// 0 - Service not available on this day.
13    NotAvailable = 0,
14    /// 1 - Service available on this day.
15    Available = 1,
16}
17impl From<i8> for GTFSDayAvailability {
18    fn from(i: i8) -> Self {
19        match i {
20            1 => GTFSDayAvailability::Available,
21            _ => GTFSDayAvailability::NotAvailable,
22        }
23    }
24}
25
26/// # Calendar Information
27///
28/// **Conditionally Required**
29/// Defines a set of dates when service is available for one or more routes.
30/// Required unless all dates of service are defined in `calendar_dates.txt`.
31#[derive(Debug, Default, Clone, PartialEq, MValueCompatible)]
32pub struct GTFSCalendar {
33    /// **Required**
34    /// Identifies a set of dates when service is available.
35    pub service_id: String,
36    /// **Required**
37    /// Service availability on Mondays: 0 or 1.
38    pub monday: i8,
39    /// **Required**
40    /// Service availability on Tuesdays: 0 or 1.
41    pub tuesday: i8,
42    /// **Required**
43    /// Service availability on Wednesdays: 0 or 1.
44    pub wednesday: i8,
45    /// **Required**
46    /// Service availability on Thursdays: 0 or 1.
47    pub thursday: i8,
48    /// **Required**
49    /// Service availability on Fridays: 0 or 1.
50    pub friday: i8,
51    /// **Required**
52    /// Service availability on Saturdays: 0 or 1.
53    pub saturday: i8,
54    /// **Required**
55    /// Service availability on Sundays: 0 or 1.
56    pub sunday: i8,
57    /// **Required**
58    /// Start service day (inclusive) for the interval. Format: YYYYMMDD
59    pub start_date: String,
60    /// **Required**
61    /// End service day (inclusive) for the interval. Format: YYYYMMDD
62    pub end_date: String,
63}
64impl GTFSCalendar {
65    /// Create a new GTFSCalendar
66    pub fn new(source: &str) -> Vec<GTFSCalendar> {
67        let mut res = Vec::new();
68        for record in parse_csv_as_record::<GTFSCalendar>(source, None, None) {
69            res.push(record);
70        }
71        res
72    }
73    /// Get the availability for Monday
74    pub fn monday(&self) -> GTFSDayAvailability {
75        self.monday.into()
76    }
77    /// Get the availability for Tuesday
78    pub fn tuesday(&self) -> GTFSDayAvailability {
79        self.tuesday.into()
80    }
81    /// Get the availability for Wednesday
82    pub fn wednesday(&self) -> GTFSDayAvailability {
83        self.wednesday.into()
84    }
85    /// Get the availability for Thursday
86    pub fn thursday(&self) -> GTFSDayAvailability {
87        self.thursday.into()
88    }
89    /// Get the availability for Friday
90    pub fn friday(&self) -> GTFSDayAvailability {
91        self.friday.into()
92    }
93    /// Get the availability for Saturday
94    pub fn saturday(&self) -> GTFSDayAvailability {
95        self.saturday.into()
96    }
97    /// Get the availability for Sunday
98    pub fn sunday(&self) -> GTFSDayAvailability {
99        self.sunday.into()
100    }
101    /// Get the start date
102    pub fn start_date(&self) -> Date {
103        parse_gtfs_date(&self.start_date).unwrap_or_default()
104    }
105    /// Get the end date
106    pub fn end_date(&self) -> Date {
107        parse_gtfs_date(&self.end_date).unwrap_or_default()
108    }
109}