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
// Copyright (C) 2018 Martin Mroz
//
// This software may be modified and distributed under the terms
// of the MIT license.  See the LICENSE file for details.

use bcbp;
use error::{Error, Result};
use de::field;

#[derive(Clone,Eq,PartialEq,Hash,Debug)]
struct Scanner<'a> {
    input: &'a str,
}

impl<'a> Scanner<'a> {

    /// Return a new intance of the receiver over the `input`.
    pub fn new(input: &'a str) -> Self {
        Scanner { input }
    }

    /// Returns `true` if no more input is available.
    #[inline]
    pub fn is_at_end(&self) -> bool {
        self.remaining_len() == 0
    }

    /// Returns the number of bytes of input remaining to process.
    #[inline]
    pub fn remaining_len(&self) -> usize {
        self.input.len()
    }

    /// Returns a scanner over a fixed-length sub-section of the input.
    /// The entire amount is consumed immediately if space is available whether or not
    /// any fields within the sub-section are invalid.
    /// 
    /// # Panics
    /// Will panic if `len` is `0`.
    pub fn scan_subsection(&mut self, len: usize) -> Result<Scanner<'a>> {
        assert!(len > 0, "Attempting to scan a zero-length sub-field list is not valid.");
        trace!("Scanning Subsection (Length {})", len);
        if self.remaining_len() < len {
            Err(Error::SubsectionTooLong)
        } else {
            let sub_fields = &self.input[ .. len ];
            self.input = &self.input[ len .. ];
            Ok(Scanner::new(sub_fields))
        }
    }

    /// Scans and returns the string underlying a field (variable or fixed-length)
    /// with a specified length value.
    /// 
    /// # Panics
    /// Will panic if `len` is `0`.
    /// Will panic if the fixed-length field intrinsic length is not equal to `len`.
    pub fn scan_str_field_len(&mut self, field: field::Field, len: usize) -> Result<&'a str> {
        assert!(len > 0, "Attempting to scan zero bytes of data.");
        assert!(field.len() == 0 || field.len() == len, "Length is not compatible the intrinsic length of the field.");
        if self.remaining_len() < len {
            trace!("Unexpected End of Input Scanning {} (Length {})", field, len);
            Err(Error::UnexpectedEndOfInput(field))
        } else {
            let substring = &self.input[ .. len ];
            self.input = &self.input[ len .. ];
            trace!("Scanning {} (Length {}) - '{}'", field, len, substring);
            Ok(substring)
        }
    }

    /// Scans and returns the string underlying a fixed-length field.
    /// Uses the intrinsic length.
    /// 
    /// # Panics
    /// Will panic if `field` is variable-length.
    pub fn scan_str_field(&mut self, field: field::Field) -> Result<&'a str> {
        assert!(field.len() != 0, "Attempting to scan a variable-length field as fixed-length.");
        self.scan_str_field_len(field, field.len())
    }

    /// Scans and returns an optional string underlying a fixed-length field.
    /// If there is no more input to process, returns `Ok(None)`.
    /// Uses the intrinsic length.
    /// 
    /// # Panics
    /// Will panic if `field` is variable-length.
    pub fn scan_optional_str_field(&mut self, field: field::Field) -> Result<Option<&'a str>> {
        assert!(field.len() != 0, "Attempting to scan a variable-length field as fixed-length.");
        if self.is_at_end() {
            Ok(None)
        } else {
            self.scan_str_field(field).map(|result| Some(result))
        }
    }

    /// Scans a fixed-length numeric field yielding the numeric value interpreted
    /// with the given `radix`.
    /// 
    /// # Panics
    /// Will panic if `field` is variable-length.
    /// 
    /// # Issues
    /// Should not advance the input until the numeric value is sucessfully scanned.
    pub fn scan_unsigned_field(&mut self, field: field::Field, radix: u32) -> Result<u64> {
        self.scan_str_field(field)
            .and_then(|str_value| {
                u64::from_str_radix(str_value, radix).map_err(|_| Error::ExpectedInteger(field))
            })
    }

    /// Scans and returns the character value underlying a fixed-length field.
    /// 
    /// # Panics
    /// Will panic if `field` is a length other than 1.
    pub fn scan_char_field(&mut self, field: field::Field) -> Result<char> {
        assert!(field.len() == 1, "Attempting to scan a single character out of a longer field.");
        self.scan_str_field(field)
            .map(|value| value.chars().next().unwrap())
    }

    /// Scans and returns an optional character value underlying a fixed-length field.
    /// If there is no more input to process, returns `Ok(None)`.
    /// 
    /// # Panics
    /// Will panic if `field` is a length other than 1.
    pub fn scan_optional_char_field(&mut self, field: field::Field) -> Result<Option<char>> {
        assert!(field.len() == 1, "Attempting to scan a single character out of a longer field.");
        if self.is_at_end() {
            Ok(None)
        } else {
            self.scan_char_field(field).map(|c| Some(c))
        }
    }

}

/// Parses a boarding pass from `input_data` representable as a string reference.
pub fn from_str<I>(input_data: I) -> Result<bcbp::Bcbp> where I: AsRef<str> {
    let input = input_data.as_ref();
    if !input.is_ascii() {
        return Err(Error::InvalidCharacters)
    }

    let mut scanner = Scanner::new(input);

    // Check that the input string is likely an M-type BCBP string.
    if scanner.scan_str_field(field::Field::FormatCode)? != "M" {
        return Err(Error::UnsupportedFormat);
    }

    // The number of legs informs the breakdown of the various field iterators.
    let number_of_legs_encoded = scanner.scan_unsigned_field(field::Field::NumberOfLegsEncoded, 10)?;

    // Create a parser for the mandatory unique fields.
    let mut bcbp = bcbp::Bcbp::default();
    bcbp.passenger_name =
        scanner.scan_str_field(field::Field::PassengerName)?.into();
    bcbp.electronic_ticket_indicator =
        scanner.scan_char_field(field::Field::ElectronicTicketIndicator)?;

    for leg_index in 0 .. number_of_legs_encoded {
        let mut leg = bcbp::Leg::default();
        
        // Mandatory fields common to all legs.
        leg.operating_carrier_pnr_code =
            scanner.scan_str_field(field::Field::OperatingCarrierPnrCode)?.into();
        leg.from_city_airport_code =
            scanner.scan_str_field(field::Field::FromCityAirportCode)?.into();
        leg.to_city_airport_code =
            scanner.scan_str_field(field::Field::ToCityAirportCode)?.into();
        leg.operating_carrier_designator =
            scanner.scan_str_field(field::Field::OperatingCarrierDesignator)?.into();
        leg.flight_number =
            scanner.scan_str_field(field::Field::FlightNumber)?.into();
        leg.date_of_flight =
            scanner.scan_str_field(field::Field::DateOfFlight)?.into();
        leg.compartment_code =
            scanner.scan_char_field(field::Field::CompartmentCode)?;
        leg.seat_number =
            scanner.scan_str_field(field::Field::SeatNumber)?.into();
        leg.check_in_sequence_number =
            scanner.scan_str_field(field::Field::CheckInSequenceNumber)?.into();
        leg.passenger_status =
            scanner.scan_char_field(field::Field::PassengerStatus)?;

        // Field size of the variable size field that follows for the leg.
        let conditional_item_size = scanner.scan_unsigned_field(field::Field::FieldSizeOfVariableSizeField, 16)?;
        if conditional_item_size > 0 {
 
            // Scanner over the entire set of conditional fields.
            let mut conditional_item_scanner = scanner.scan_subsection(conditional_item_size as usize)?;

            // The first leg may contain some optional fields at the root level.
            if leg_index == 0 {

                // Validate the beginning of version number tag as a sanity check.
                if conditional_item_scanner.remaining_len() > 0 {
                    if conditional_item_scanner.scan_str_field(field::Field::BeginningOfVersionNumber)? != ">" {
                        return Err(Error::InvalidStartOfVersionNumber);
                    }
                }

                // The version number is part of the structure and must be consumed, but is not used.
                if conditional_item_scanner.remaining_len() > 0 {
                    let _ = conditional_item_scanner.scan_str_field(field::Field::VersionNumber)?;
                }

                // Conditional unique fields are embedded in their own variable-length wrapper.
                if conditional_item_scanner.remaining_len() > 0 {
                    let len = conditional_item_scanner.scan_unsigned_field(field::Field::FieldSizeOfStructuredMessageUnique, 16)?;
                    if len > 0 {
                        let mut unique_scanner = conditional_item_scanner.scan_subsection(len as usize)?;

                        bcbp.passenger_description =
                            unique_scanner.scan_optional_char_field(field::Field::PassengerDescription)?;
                        bcbp.source_of_check_in =
                            unique_scanner.scan_optional_char_field(field::Field::SourceOfCheckIn)?;
                        bcbp.source_of_boarding_pass_issuance =
                            unique_scanner.scan_optional_char_field(field::Field::SourceOfBoardingPassIssuance)?;
                        bcbp.date_of_issue_of_boarding_pass =
                            unique_scanner.scan_optional_str_field(field::Field::DateOfIssueOfBoardingPass)?.map(Into::into);
                        bcbp.document_type =
                            unique_scanner.scan_optional_char_field(field::Field::DocumentType)?;
                        bcbp.airline_designator_of_boarding_pass_issuer =
                            unique_scanner.scan_optional_str_field(field::Field::AirlineDesignatorOfBoardingPassIssuer)?.map(Into::into);
                        bcbp.baggage_tag_license_plate_numbers =
                            unique_scanner.scan_optional_str_field(field::Field::BaggageTagLicensePlateNumbers)?.map(Into::into);
                        bcbp.first_non_consecutive_baggage_tag_license_plate_numbers =
                            unique_scanner.scan_optional_str_field(field::Field::FirstNonConsecutiveBaggageTagLicensePlateNumbers)?.map(Into::into);
                        bcbp.second_non_consecutive_baggage_tag_license_plate_numbers =
                            unique_scanner.scan_optional_str_field(field::Field::SecondNonConsecutiveBaggageTagLicensePlateNumbers)?.map(Into::into);
                    }
                }
            }

            // Conditional fields common to all legs.
            if conditional_item_scanner.remaining_len() > 0 {
                let len = conditional_item_scanner.scan_unsigned_field(field::Field::FieldSizeOfStructuredMessageRepeated, 16)?;
                if len > 0 {
                    let mut repeated_scanner = conditional_item_scanner.scan_subsection(len as usize)?;

                    leg.airline_numeric_code =
                        repeated_scanner.scan_optional_str_field(field::Field::AirlineNumericCode)?.map(Into::into);
                    leg.document_form_serial_number =
                        repeated_scanner.scan_optional_str_field(field::Field::DocumentFormSerialNumber)?.map(Into::into);
                    leg.selectee_indicator =
                        repeated_scanner.scan_optional_char_field(field::Field::SelecteeIndicator)?;
                    leg.international_document_verification =
                        repeated_scanner.scan_optional_char_field(field::Field::InternationalDocumentVerification)?;
                    leg.marketing_carrier_designator =
                        repeated_scanner.scan_optional_str_field(field::Field::MarketingCarrierDesignator)?.map(Into::into);
                    leg.frequent_flyer_airline_designator =
                        repeated_scanner.scan_optional_str_field(field::Field::FrequentFlyerAirlineDesignator)?.map(Into::into);
                    leg.frequent_flyer_number =
                        repeated_scanner.scan_optional_str_field(field::Field::FrequentFlyerNumber)?.map(Into::into);
                    leg.id_ad_indicator =
                        repeated_scanner.scan_optional_char_field(field::Field::IdAdIndicator)?;
                    leg.free_baggage_allowance =
                        repeated_scanner.scan_optional_str_field(field::Field::FreeBaggageAllowance)?.map(Into::into);
                    leg.fast_track =
                        repeated_scanner.scan_optional_char_field(field::Field::FastTrack)?;
                }
            }

            // Any remaining text is ascribed to airline use.
            if conditional_item_scanner.remaining_len() > 0 {
                let remaining_len = conditional_item_scanner.remaining_len();
                let body = conditional_item_scanner.scan_str_field_len(field::Field::AirlineIndividualUse, remaining_len)?;
                leg.airline_individual_use = Some(body.into());
            }
        }

        bcbp.legs.push(leg);
    }

    // Remaining input is ascribed to Security Data.
    if scanner.remaining_len() > 0 {
        if scanner.scan_str_field(field::Field::BeginningOfSecurityData)? != "^" {
            return Err(Error::InvalidStartOfSecurityData);
        }

        let mut security_data = bcbp::SecurityData::default();

        // The security data type captured as a separate field set as the next field, data length, is discarded.
        security_data.type_of_security_data =
            scanner.scan_optional_char_field(field::Field::TypeOfSecurityData)?;

        // Scan the length of the security data.
        if scanner.remaining_len() > 0 {
            let len = scanner.scan_unsigned_field(field::Field::LengthOfSecurityData, 16)?;
            if len > 0 {
                let body = scanner.scan_str_field_len(field::Field::SecurityData, len as usize)?;
                security_data.security_data = Some(body.into());
            }
        }

        bcbp.security_data = security_data;
    }

    if !scanner.is_at_end() {
        Err(Error::TrailingCharacters)
    } else {
        Ok(bcbp)
    }
}