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
#[derive(Debug)]

/// Indicates whether a PeriodPass or Ticket uses the old-style fares and zones, or the new.
/// 2010 is the old-style, while 2014 is the new-style.
pub enum ProductCode {
    FaresFor2010(u16), // Code type = 0
    FaresFor2014(u16), // Code type = 1
}

impl ProductCode {
    pub const FARES_2010_TYPE: u8 = 0;
    pub const FARES_2014_TYPE: u8 = 1;

    pub(crate) fn new(code_type: u8, value: u16) -> ProductCode {
        if code_type == ProductCode::FARES_2010_TYPE {
            ProductCode::FaresFor2010(value)
        } else {
            ProductCode::FaresFor2014(value)
        }
    }
}

impl From<&ProductCode> for u16 {
    fn from(val: &ProductCode) -> Self {
        match val {
            ProductCode::FaresFor2010(v) | ProductCode::FaresFor2014(v) => *v,
        }
    }
}

/// The number of a boarded element.
#[derive(Debug)]
pub enum BoardingLocation {
    NoneOrReserved,
    BusNumber(u16),
    TrainNumber(u16),
    PlatformNumber(u16),
}

impl BoardingLocation {
    pub(crate) fn new(boarding_area_type: u8, boarding_area_value: u16) -> BoardingLocation {
        match boarding_area_type {
            0 => BoardingLocation::NoneOrReserved,
            1 => BoardingLocation::BusNumber(boarding_area_value),
            2 => BoardingLocation::TrainNumber(boarding_area_value),
            3 => BoardingLocation::PlatformNumber(boarding_area_value),
            e => panic!("Given value ({}) for BoardingLocation not supported.", e),
        }
    }
}

impl From<&BoardingLocation> for u16 {
    fn from(val: &BoardingLocation) -> Self {
        match val {
            BoardingLocation::NoneOrReserved => 0,
            BoardingLocation::BusNumber(num)
            | BoardingLocation::TrainNumber(num)
            | BoardingLocation::PlatformNumber(num) => *num,
        }
    }
}

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
/// This enum is pure speculation--the underlying value is a single bit. What else _could_ it mean?
pub enum BoardingDirection {
    /// Indicates that at the time of boarding, the transit medium  was headed toward the end of its route.
    TowardEnd = 0,
    /// Indicates that at the time of boarding, the transit medium was headed toward the start of its route.
    TowardStart = 1,
}

impl From<u8> for BoardingDirection {
    fn from(value: u8) -> Self {
        match value {
            0 => BoardingDirection::TowardEnd,
            1 => BoardingDirection::TowardStart,
            e => panic!("Given value ({}) for BoardingDirection not supported.", e),
        }
    }
}

/// Represents an area in which, or a vehicle for which, a ticket is valid.
#[derive(Debug)]
pub enum ValidityArea {
    OldZone(u8),
    Zone(Vec<ValidityZone>),
    Vehicle(VehicleType),
}

impl ValidityArea {
    pub const OLD_ZONE_TYPE: u8 = 0;
    pub const VEHICLE_TYPE: u8 = 1;
    pub const NEW_ZONE_TYPE: u8 = 2; // The docs LIE, and don't include this value. But it's there!

    pub(crate) fn new(area_type: u8, area_value: u8) -> ValidityArea {
        let mut zones: Vec<ValidityZone> = Vec::new();
        match area_type {
            // TODO: Wrap this a bit more nicely. It represents an old zone (i.e. Zone 1, Zone 2, Region etc)
            ValidityArea::OLD_ZONE_TYPE => ValidityArea::OldZone(area_value),
            ValidityArea::VEHICLE_TYPE => ValidityArea::Vehicle(VehicleType::from(area_value)),
            ValidityArea::NEW_ZONE_TYPE => {
                let from_zone = (area_value & 0b0011_1000) >> 3; // leftmost 3 bits
                let to_zone = area_value & 0b0000_0111; // 3 bits to the right of that
                for val in from_zone..=to_zone {
                    zones.push(ValidityZone::from(val));
                }
                ValidityArea::Zone(zones)
            }
            e => panic!("Unsupported area type: {}", e),
        }
    }
}

/// The HSL fare zone(s) in which a ticket is valid.
#[derive(Clone, Debug)]
pub enum ValidityZone {
    ZoneA = 0,
    ZoneB = 1,
    ZoneC = 2,
    ZoneD = 3,
    ZoneE = 4,
    ZoneF = 5,
    ZoneG = 6,
    ZoneH = 7,
}

impl From<u8> for ValidityZone {
    fn from(value: u8) -> Self {
        match value {
            0 => ValidityZone::ZoneA,
            1 => ValidityZone::ZoneB,
            2 => ValidityZone::ZoneC,
            3 => ValidityZone::ZoneD,
            4 => ValidityZone::ZoneE,
            5 => ValidityZone::ZoneF,
            6 => ValidityZone::ZoneG,
            7 => ValidityZone::ZoneH,
            e => panic!("Given value ({}) for ValidityZone not supported.", e),
        }
    }
}

impl From<&ValidityZone> for u8 {
    fn from(value: &ValidityZone) -> Self {
        match value {
            ValidityZone::ZoneA => 0,
            ValidityZone::ZoneB => 1,
            ValidityZone::ZoneC => 2,
            ValidityZone::ZoneD => 3,
            ValidityZone::ZoneE => 4,
            ValidityZone::ZoneF => 5,
            ValidityZone::ZoneG => 6,
            ValidityZone::ZoneH => 7,
        }
    }
}

#[derive(Debug)]
pub enum ValidityLength {
    Minutes(u8),
    Hours(u8),
    TwentyFourHourPeriods(u8),
    Days(u8),
}

impl ValidityLength {
    pub(crate) fn new(length_type: u8, length_value: u8) -> ValidityLength {
        match length_type {
            0 => ValidityLength::Minutes(length_value),
            1 => ValidityLength::Hours(length_value),
            2 => ValidityLength::TwentyFourHourPeriods(length_value),
            3 => ValidityLength::Days(length_value),
            e => panic!("Given value ({}) for ValidityLength type not supported.", e),
        }
    }
}

impl From<&ValidityLength> for u8 {
    fn from(value: &ValidityLength) -> Self {
        match value {
            ValidityLength::Minutes(num) => *num,
            ValidityLength::Hours(num) => *num,
            ValidityLength::TwentyFourHourPeriods(num) => *num,
            ValidityLength::Days(num) => *num,
        }
    }
}

/// The vehicle type on which this ticket is valid.
#[derive(Debug)]
pub enum VehicleType {
    Undefined = 0,
    Bus = 1,
    Tram = 5,
    Metro = 6,
    Train = 7,
    Ferry = 8,
    ULine = 9,
}

impl From<u8> for VehicleType {
    fn from(value: u8) -> Self {
        match value {
            0 => VehicleType::Undefined,
            1 => VehicleType::Bus,
            5 => VehicleType::Tram,
            6 => VehicleType::Metro,
            7 => VehicleType::Train,
            8 => VehicleType::Ferry,
            9 => VehicleType::ULine,
            e => panic!("Given value ('{:?}') for VehicleType not supported.", e),
        }
    }
}

impl From<&VehicleType> for u8 {
    fn from(value: &VehicleType) -> Self {
        match value {
            VehicleType::Undefined => 0,
            VehicleType::Bus => 1,
            VehicleType::Tram => 5,
            VehicleType::Metro => 6,
            VehicleType::Train => 7,
            VehicleType::Ferry => 8,
            VehicleType::ULine => 9,
        }
    }
}

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Language {
    Finnish = 0,
    Swedish = 1,
    English = 2,
}

impl From<u8> for Language {
    fn from(value: u8) -> Self {
        match value {
            0 => Language::Finnish,
            1 => Language::Swedish,
            2 => Language::English,
            e => panic!("Given value ({}) for Language not supported.", e),
        }
    }
}

/// The type of device that sold the ticket, or recharged the card.
#[derive(Debug)]
pub enum SaleDevice {
    ServicePointSalesDevice(u16),
    DriverTicketMachine(u16),
    CardReader(u16),
    TicketMachine(u16),
    Server(u16),
    HSLSmallEquipment(u16),
    ExternalServiceEquipment(u16),
    Reserved(u16),
}

impl SaleDevice {
    pub(crate) fn new(device_type: u8, device_number: u16) -> SaleDevice {
        match device_type {
            0 => SaleDevice::ServicePointSalesDevice(device_number),
            1 => SaleDevice::DriverTicketMachine(device_number),
            2 => SaleDevice::CardReader(device_number),
            3 => SaleDevice::TicketMachine(device_number),
            4 => SaleDevice::Server(device_number),
            5 => SaleDevice::HSLSmallEquipment(device_number),
            6 => SaleDevice::ExternalServiceEquipment(device_number),
            7 => SaleDevice::Reserved(device_number),
            e => panic!("Given value ({}) for SaleDeviceType not supported.", e),
        }
    }
}

impl From<&SaleDevice> for u16 {
    fn from(val: &SaleDevice) -> Self {
        match val {
            SaleDevice::ServicePointSalesDevice(num) => *num,
            SaleDevice::DriverTicketMachine(num) => *num,
            SaleDevice::CardReader(num) => *num,
            SaleDevice::TicketMachine(num) => *num,
            SaleDevice::Server(num) => *num,
            SaleDevice::HSLSmallEquipment(num) => *num,
            SaleDevice::ExternalServiceEquipment(num) => *num,
            SaleDevice::Reserved(num) => *num,
        }
    }
}

/// The type and value of area where boarding last happened.
#[derive(Debug)]
pub enum BoardingArea {
    Zone(ValidityZone),
    Vehicle(VehicleType),
    ZoneCircle(u8), // Not sure what this is. One of the old-style regions?
}

impl BoardingArea {
    pub(crate) fn new(area_type: u8, area_value: u8) -> BoardingArea {
        match area_type {
            0 => BoardingArea::Zone(ValidityZone::from(area_value)),
            1 => BoardingArea::Vehicle(VehicleType::from(area_type)),
            2 => BoardingArea::ZoneCircle(area_value),
            e => panic!("Given value ({}) for BoardingArea type not supported.", e),
        }
    }
}

impl From<&BoardingArea> for u8 {
    fn from(val: &BoardingArea) -> Self {
        match val {
            BoardingArea::Zone(zone) => u8::from(zone),
            BoardingArea::Vehicle(vehicle_type) => u8::from(vehicle_type),
            BoardingArea::ZoneCircle(zone_value) => *zone_value,
        }
    }
}