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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
extern crate chrono;
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;

use self::chrono::{DateTime, UTC};
use std::error::Error;
use std::str::FromStr;
use std::net::SocketAddrV4;
use std::fmt::{Display, Formatter};
use std::fmt;
use std::ops::Index;

// AWS doesn't version their log file format so these version numbers were
// selected by me to bring some sanity to the various formats.
const ELB_RECORD_V1_FIELD_COUNT: usize = 14;
const ELB_RECORD_V2_FIELD_COUNT: usize = 17;
const UNDEFINED_CHAR: &'static str = "-";

/// The product of parsing a single AWS ELB log record.
#[derive(Debug)]
pub struct ELBRecord<'a> {
    pub timestamp: DateTime<UTC>,
    pub elb_name: &'a str,
    pub client_address: SocketAddrV4,
    pub backend_address: SocketAddrV4,
    pub request_processing_time: f32,
    pub backend_processing_time: f32,
    pub response_processing_time: f32,
    pub elb_status_code: u16,
    pub backend_status_code: u16,
    pub received_bytes: u64,
    pub sent_bytes: u64,
    pub request_method: &'a str,
    pub request_url: &'a str,
    pub request_http_version: &'a str,
    pub user_agent: &'a str,
    pub ssl_cipher: &'a str,
    pub ssl_protocol: &'a str,
}

/// The result of an attempt to parse an ELB record.
pub type ParsingResult<'a> = Result<ELBRecord<'a>, ParsingErrors<'a>>;

/// The result of a failed attempt to parse an ELB record.
///
/// It is very possible that multiple fields of a record are not parsable.  An attempt is made to
/// parse all of the fields of an ELB record.  An error is returned for each field that was not
/// parsable to make it clear what fields of the record were faulty and allow the user to decide
/// how to handle the failure.
#[derive(Debug, PartialEq)]
pub struct ParsingErrors<'a> {
    /// The raw record.
    pub record: &'a str,
    /// A collection of parsing errors such as fields that could not be parsed or a failure to
    /// open an ELB log file.
    pub errors: Vec<ELBRecordParsingError>,
}

/// Specific parsing errors that are returned as part of the [`ParsingErrors::errors`]
/// (struct.ParsingErrors.html) collection.
#[derive(Debug, PartialEq)]
pub enum ELBRecordParsingError {
    /// Returned if the record does not have the correct number of fields.
    MalformedRecord,
    /// A failed attempt to parse a specific field of the ELB record.
    ParsingError {
        field_name: ELBRecordField,
        description: String,
    },
}

impl Display for ELBRecordParsingError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            ELBRecordParsingError::MalformedRecord => write!(f, "Record is malformed."),
            ELBRecordParsingError::ParsingError { ref field_name, ref description } => {
                write!(f,
                       "Parsing of field {} failed with the following error: {}.",
                       field_name,
                       description)
            }
        }
    }
}

impl Error for ELBRecordParsingError {
    fn description(&self) -> &str {
        match *self {
            ELBRecordParsingError::MalformedRecord => "malformed record",
            ELBRecordParsingError::ParsingError { .. } => "field parsing failed",
        }
    }
}

/// Attempt to parse a single string into an ELB record.
///
/// This is the main parsing algorithm.  It will attempt to parse every field that is supposed to
/// be in an ELB Access log record.  If it successful it will return an `Ok(ELBRecord)`.  If not,
/// it will return a `Err(ParsingErrors)`.
pub fn parse_record(record: &str) -> ParsingResult {
    let mut errors: Vec<ELBRecordParsingError> = Vec::new();
    let split_record: Vec<&str> = record.split_record();
    let split_len = split_record.len();
    if split_len != ELB_RECORD_V1_FIELD_COUNT && split_len != ELB_RECORD_V2_FIELD_COUNT {
        errors.push(ELBRecordParsingError::MalformedRecord);
        return Err(ParsingErrors {
            record: record,
            errors: errors,
        });
    }

    let ts = split_record.parse_field(ELBRecordField::Timestamp, &mut errors);
    let clnt_addr = split_record.parse_field(ELBRecordField::ClientAddress, &mut errors);
    let be_addr = split_record.parse_field(ELBRecordField::BackendAddress, &mut errors);
    let req_proc_time =
        split_record.parse_field(ELBRecordField::RequestProcessingTime, &mut errors);
    let be_proc_time = split_record.parse_field(ELBRecordField::BackendProcessingTime, &mut errors);
    let res_proc_time =
        split_record.parse_field(ELBRecordField::ResponseProcessingTime, &mut errors);
    let elb_sc = split_record.parse_field(ELBRecordField::ELBStatusCode, &mut errors);
    let be_sc = split_record.parse_field(ELBRecordField::BackendStatusCode, &mut errors);
    let bytes_received = split_record.parse_field(ELBRecordField::ReceivedBytes, &mut errors);
    let bytes_sent = split_record.parse_field(ELBRecordField::SentBytes, &mut errors);
    let (user_agent, ssl_cipher, ssl_protocol) = if split_len == ELB_RECORD_V2_FIELD_COUNT {
        (split_record[ELBRecordField::UserAgent],
         split_record[ELBRecordField::SSLCipher],
         split_record[ELBRecordField::SSLProtocol])

    } else {
        (UNDEFINED_CHAR, UNDEFINED_CHAR, UNDEFINED_CHAR)
    };

    if errors.is_empty() {
        // If errors is empty it is more than likely parsing was successful and unwrap
        // is safe.
        Ok(ELBRecord {
            timestamp: ts.unwrap(),
            elb_name: split_record[ELBRecordField::ELBName],
            client_address: clnt_addr.unwrap(),
            backend_address: be_addr.unwrap(),
            request_processing_time: req_proc_time.unwrap(),
            backend_processing_time: be_proc_time.unwrap(),
            response_processing_time: res_proc_time.unwrap(),
            elb_status_code: elb_sc.unwrap(),
            backend_status_code: be_sc.unwrap(),
            received_bytes: bytes_received.unwrap(),
            sent_bytes: bytes_sent.unwrap(),
            request_method: split_record[ELBRecordField::RequestMethod],
            request_url: split_record[ELBRecordField::RequestURL],
            request_http_version: split_record[ELBRecordField::RequestHTTPVersion],
            user_agent: user_agent,
            ssl_cipher: ssl_cipher,
            ssl_protocol: ssl_protocol,
        })
    } else {
        Err(ParsingErrors {
            record: record,
            errors: errors,
        })
    }
}

trait RecordSplitter {
    fn split_record(&self) -> Vec<&str>;
}

impl RecordSplitter for str {
    fn split_record(&self) -> Vec<&str> {
        let mut split_record: Vec<&str> = Vec::with_capacity(ELB_RECORD_V2_FIELD_COUNT);
        let mut field_specs_idx = 0;
        let mut current_field_spec = &ORDERED_FIELD_SPECS[field_specs_idx];
        let mut current_start_delim = current_field_spec.start_delimiter;
        let mut start_of_field_index = 0;

        for (current_idx, current_char) in self.trim_left().char_indices() {
            match current_start_delim {
                None if current_char == current_field_spec.end_delimiter => {
                    split_record.push(&self[start_of_field_index..current_idx]);
                    start_of_field_index = current_idx + 1;
                    field_specs_idx += 1;
                    if field_specs_idx < ELB_RECORD_V2_FIELD_COUNT {
                        current_field_spec = &ORDERED_FIELD_SPECS[field_specs_idx];
                        current_start_delim = current_field_spec.start_delimiter;
                    }
                }
                Some(sd) if current_char == sd => {
                    start_of_field_index = current_idx + 1;
                    current_start_delim = None;
                }
                _ => {}
            }
        }

        let x = &self[start_of_field_index..];
        if !x.is_empty() {
            split_record.push(x);
        }

        debug!("{:?}", split_record);
        split_record
    }
}

const SPACE: char = ' ';
const DOUBLE_QUOTE: char = '"';
lazy_static! {
    static ref ORDERED_FIELD_SPECS: Vec<ELBRecordFieldParsingSpec> = vec!(
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::Timestamp,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::ELBName,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::ClientAddress,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::BackendAddress,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::RequestProcessingTime,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::BackendProcessingTime,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::ResponseProcessingTime,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::ELBStatusCode,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::BackendStatusCode,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::ReceivedBytes,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::SentBytes,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::RequestMethod,
            start_delimiter: Some(DOUBLE_QUOTE),
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::RequestURL,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::RequestHTTPVersion,
            start_delimiter: None,
            end_delimiter: DOUBLE_QUOTE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::UserAgent,
            start_delimiter: Some(DOUBLE_QUOTE),
            end_delimiter: DOUBLE_QUOTE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::SSLCipher,
            start_delimiter: Some(SPACE),
            end_delimiter: SPACE,
        },
        ELBRecordFieldParsingSpec {
            field: ELBRecordField::SSLProtocol,
            start_delimiter: None,
            end_delimiter: SPACE,
        },
    );
}

#[derive(Debug)]
struct ELBRecordFieldParsingSpec {
    field: ELBRecordField,
    start_delimiter: Option<char>,
    end_delimiter: char,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ELBRecordField {
    Timestamp = 0,
    ELBName,
    ClientAddress,
    BackendAddress,
    RequestProcessingTime,
    BackendProcessingTime,
    ResponseProcessingTime,
    ELBStatusCode,
    BackendStatusCode,
    ReceivedBytes,
    SentBytes,
    RequestMethod,
    RequestURL,
    RequestHTTPVersion,
    UserAgent,
    SSLCipher,
    SSLProtocol,
}

impl<'a> Index<ELBRecordField> for Vec<&'a str> {
    type Output = &'a str;

    fn index(&self, idx: ELBRecordField) -> &&'a str {
        &self[idx as usize]
    }
}

impl Display for ELBRecordField {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            ELBRecordField::Timestamp => write!(f, "timestamp"),
            ELBRecordField::ELBName => write!(f, "ELB name"),
            ELBRecordField::ClientAddress => write!(f, "client address"),
            ELBRecordField::BackendAddress => write!(f, "backend address"),
            ELBRecordField::RequestProcessingTime => write!(f, "request processing time"),
            ELBRecordField::BackendProcessingTime => write!(f, "backend processing time"),
            ELBRecordField::ResponseProcessingTime => write!(f, "response processing time"),
            ELBRecordField::ELBStatusCode => write!(f, "ELB status code"),
            ELBRecordField::BackendStatusCode => write!(f, "backend status code"),
            ELBRecordField::ReceivedBytes => write!(f, "received bytes"),
            ELBRecordField::SentBytes => write!(f, "sent bytes"),
            ELBRecordField::RequestMethod => write!(f, "request method"),
            ELBRecordField::RequestURL => write!(f, "request URL"),
            ELBRecordField::RequestHTTPVersion => write!(f, "request HTTP version"),
            ELBRecordField::UserAgent => write!(f, "user agent"),
            ELBRecordField::SSLCipher => write!(f, "SSL cipher"),
            ELBRecordField::SSLProtocol => write!(f, "SSL protocol"),
        }
    }
}

trait ELBRecordFieldParser {
    fn parse_field<T>(&self,
                      field_name: ELBRecordField,
                      errors: &mut Vec<ELBRecordParsingError>)
                      -> Option<T>
        where T: FromStr,
              T::Err: Error + 'static;
}

impl<'a> ELBRecordFieldParser for Vec<&'a str> {
    fn parse_field<T>(&self,
                      field_name: ELBRecordField,
                      errors: &mut Vec<ELBRecordParsingError>)
                      -> Option<T>
        where T: FromStr,
              T::Err: Error + 'static
    {
        let raw_prop = self[field_name];
        match raw_prop.parse::<T>() {
            Ok(parsed) => Some(parsed),

            Err(e) => {
                errors.push(ELBRecordParsingError::ParsingError {
                    field_name: field_name,
                    description: e.description().to_owned(),
                });
                None
            }
        }
    }
}

#[cfg(test)]
mod parse_record_tests {
    use super::parse_record;
    use super::ELBRecordParsingError;
    use super::ELBRecordField;
    use super::UNDEFINED_CHAR;

    const V1_TEST_RECORD: &'static str = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 \
    172.16.1.5:9000 0.000039 0.145507 0.00003 200 200 0 7582 \
    \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"\
    ";

    const V2_TEST_RECORD: &'static str =
        "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 0.000039 0.145507 \
         0.00003 200 200 0 7582 \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 \
         HTTP/1.1\" \"Mozilla/5.0 (cloud; like Mac OS X; en-us) AppleWebKit/537.36.0 (KHTML, like \
         Gecko) Version/4.0.4 Mobile/7B334b Safari/537.36.0\" some_ssl_cipher some_ssl_protocol";

    #[test]
    fn returns_a_record_with_the_ssl_protocol_set_to_a_not_available_symbol_when_it_is_not_present
        () {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.ssl_protocol, UNDEFINED_CHAR)
    }

    #[test]
    fn returns_a_record_with_the_ssl_protocol_when_it_is_present() {
        let elb_record = parse_record(V2_TEST_RECORD).unwrap();

        assert_eq!(elb_record.ssl_protocol, "some_ssl_protocol")
    }

    #[test]
    fn returns_a_record_with_the_ssl_cipher_set_to_a_not_available_symbol_when_it_is_not_present
        () {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.ssl_cipher, UNDEFINED_CHAR)
    }

    #[test]
    fn returns_a_record_with_the_ssl_cipher_when_it_is_present() {
        let elb_record = parse_record(V2_TEST_RECORD).unwrap();

        assert_eq!(elb_record.ssl_cipher, "some_ssl_cipher")
    }

    #[test]
    fn returns_a_record_with_the_user_agent_set_to_a_not_available_symbol_when_it_is_not_present
        () {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.user_agent, UNDEFINED_CHAR)
    }

    #[test]
    fn returns_a_record_with_the_user_agent_when_it_is_present() {
        let elb_record = parse_record(V2_TEST_RECORD).unwrap();

        assert_eq!(elb_record.user_agent,
                   "Mozilla/5.0 (cloud; like Mac OS X; en-us) AppleWebKit/537.36.0 (KHTML, like \
                    Gecko) Version/4.0.4 Mobile/7B334b Safari/537.36.0")
    }

    #[test]
    fn returns_a_malformed_record_error_for_records_short_on_values() {
        let short_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 \
        172.16.1.5:9000 0.000039 200 200 0 7582 \
        \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\" \
        ";

        let malformed_error = parse_record(short_record).unwrap_err().errors.pop();

        assert_eq!(malformed_error,
                   Some(ELBRecordParsingError::MalformedRecord))
    }

    #[test]
    fn returns_a_record_with_the_request_http_version() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.request_http_version, "HTTP/1.1")
    }

    #[test]
    fn returns_a_record_with_the_request_url() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.request_url,
                   "http://some.domain.com:80/path0/path1?param0=p0&param1=p1")
    }

    #[test]
    fn returns_a_record_with_the_request_method() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.request_method, "GET")
    }

    #[test]
    fn returns_a_record_with_the_sent_bytes() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.sent_bytes, 7582)
    }

    #[test]
    fn returns_a_parsing_error_referencing_the_sent_bytes_when_the_sent_bytes_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 0.145507 0.00003 200 200 0 bad_sent_bytes \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::SentBytes)
    }

    #[test]
    fn returns_a_record_with_the_received_bytes() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.received_bytes, 0)
    }

    #[test]
    fn returns_a_parsing_error_referencing_the_received_bytes_when_the_received_bytes_is_malformed
        () {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 0.145507 0.00003 200 200 bad_received_bytes 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::ReceivedBytes)
    }

    #[test]
    fn returns_a_record_with_the_backend_status_code() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.backend_status_code, 200)
    }

    #[test]
    fn returns_a_parsing_error_when_the_backend_status_code_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 0.145507 0.00003 200 bad_backend_status_code 0 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::BackendStatusCode)
    }

    #[test]
    fn returns_a_record_with_the_elb_status_code() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.elb_status_code, 200)
    }

    #[test]
    fn returns_a_parsing_error_when_the_elb_status_code_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 0.145507 0.00003 bad_elb_status_code 200 0 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::ELBStatusCode)
    }

    #[test]
    fn returns_a_record_with_the_response_processing_time() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.response_processing_time, 0.00003)
    }

    #[test]
    fn returns_a_parsing_error_when_the_response_processing_time_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 0.145507 bad_response_processing_time 200 200 0 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::ResponseProcessingTime)
    }

    #[test]
    fn returns_a_record_with_the_backend_processing_time() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.backend_processing_time, 0.145507)
    }

    #[test]
    fn returns_a_parsing_error_when_the_backend_processing_time_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          0.000039 bad_backend_processing_time 0.00003 200 200 0 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::BackendProcessingTime)
    }

    #[test]
    fn returns_a_record_with_the_request_processing_time() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.request_processing_time, 0.000039)
    }

    #[test]
    fn returns_a_parsing_error_when_the_request_processing_time_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 172.16.1.5:9000 \
                          bad_request_processing_time 0.145507 0.00003 200 200 0 7582 \"GET \
                          http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::RequestProcessingTime)
    }

    #[test]
    fn returns_a_record_with_the_backend_address() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.backend_address,
                   "172.16.1.5:9000".parse().unwrap())
    }

    #[test]
    fn returns_a_parsing_error_when_the_backend_address_is_malformed() {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name 172.16.1.6:54814 \
        bad_backend_address 0.000039 0.145507 0.00003 200 200 0 7582 \
        \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"\
        ";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::BackendAddress)
    }

    #[test]
    fn returns_a_record_with_the_client_address() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.client_address,
                   "172.16.1.6:54814".parse().unwrap())
    }

    #[test]
    fn returns_a_parsing_error_referencing_the_client_address_when_the_client_address_is_malformed
        () {
        let bad_record = "2015-08-15T23:43:05.302180Z elb-name bad_client_address \
        172.16.1.5:9000 0.000039 0.145507 0.00003 200 200 0 7582 \
        \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"\
        ";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::ClientAddress)
    }

    #[test]
    fn returns_a_record_with_the_timestamp() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(format!("{:?}", elb_record.timestamp),
                   "2015-08-15T23:43:05.302180Z")
    }

    #[test]
    fn returns_a_parsing_error_referencing_the_timestamp_when_the_timestamp_is_malformed() {
        let bad_record = "bad_timestamp elb-name 172.16.1.6:54814 \
        172.16.1.5:9000 0.000039 0.145507 0.00003 200 200 0 7582 \
        \"GET http://some.domain.com:80/path0/path1?param0=p0&param1=p1 HTTP/1.1\"\
        ";

        let error_field_name = match parse_record(bad_record).unwrap_err().errors.pop().unwrap() {
            ELBRecordParsingError::ParsingError { field_name, .. } => field_name,
            _ => panic!(),
        };

        assert_eq!(error_field_name, ELBRecordField::Timestamp)
    }

    #[test]
    fn returns_a_record_with_the_elb_name() {
        let elb_record = parse_record(V1_TEST_RECORD).unwrap();

        assert_eq!(elb_record.elb_name, "elb-name")
    }
}