gistools/readers/gtfs/schedule/
fare_media.rs

1use crate::readers::csv::parse_csv_as_record;
2use alloc::{collections::BTreeMap, string::String};
3use s2json::MValueCompatible;
4
5/// Describes the type of fare media used.
6/// 0 - None
7/// 1 - Physical paper ticket
8/// 2 - Physical transit card
9/// 3 - cEMV (contactless)
10/// 4 - Mobile app
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub enum GTFSFareMediaType {
13    /// 0 - None
14    None = 0,
15    /// 1 - Physical paper ticket
16    PhysicalPaperTicket = 1,
17    /// 2 - Physical transit card
18    PhysicalTransitCard = 2,
19    /// 3 - cEMV (contactless)
20    CEMV = 3,
21    /// 4 - Mobile app
22    MobileApp = 4,
23}
24impl From<u8> for GTFSFareMediaType {
25    fn from(value: u8) -> Self {
26        match value {
27            1 => GTFSFareMediaType::PhysicalPaperTicket,
28            2 => GTFSFareMediaType::PhysicalTransitCard,
29            3 => GTFSFareMediaType::CEMV,
30            4 => GTFSFareMediaType::MobileApp,
31            _ => GTFSFareMediaType::None,
32        }
33    }
34}
35
36/// # Fare Media
37///
38/// **Optional**
39/// Describes physical or virtual holders used for the representation and validation of a fare product.
40#[derive(Debug, Default, Clone, PartialEq, MValueCompatible)]
41pub struct GTFSFareMedia {
42    /// **Required**
43    /// Identifies a fare media (`fare_media_id`).
44    pub fare_media_id: String,
45    /// **Optional**
46    /// Rider-facing name for this fare media.
47    pub fare_media_name: Option<String>,
48    /// **Required**
49    /// Type of fare media. One of:
50    /// - 0 = None
51    /// - 1 = Physical paper ticket
52    /// - 2 = Physical transit card
53    /// - 3 = cEMV (contactless)
54    /// - 4 = Mobile app
55    pub fare_media_type: u8,
56}
57impl GTFSFareMedia {
58    /// Create a new GTFSFareMedia
59    pub fn new(source: &str) -> BTreeMap<String, GTFSFareMedia> {
60        let mut res = BTreeMap::new();
61        for record in parse_csv_as_record::<GTFSFareMedia>(source, None, None) {
62            res.insert(record.fare_media_id.clone(), record);
63        }
64        res
65    }
66    /// Get the media type
67    pub fn fare_media_type(&self) -> GTFSFareMediaType {
68        self.fare_media_type.into()
69    }
70}