fitparser 0.10.0

A utility to parse ANT .FIT files and allow their contents to be serialized with serde.
Documentation
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
//! # FitParser
//!
//! `fitparser` is a utility to parse an ANT FIT file based on a given profile into a more
//! useful form for consuming applications. To that end the [serde](https://github.com/serde-rs/serde)
//! framework is used to allow the data to be serialized into any format supported by serde. This
//! library currently does not support writing FIT files.
//!
//! ## Example
//! Open a file or pass in any other object that implements the Read
//! trait. A vector of data records is returned if deserialization is
//! successful. See the `fit_to_json` example for a command line utility
//! that parses FIT files and exports them as JSON.
//! ```
//! use fitparser;
//! use fitparser::de::{DecodeOption, from_reader_with_options};
//! use std::fs::File;
//! use std::io::prelude::*;
//!
//! println!("Parsing FIT files using Profile version: {:?}", fitparser::profile::VERSION);
//! let mut fp = File::open("tests/fixtures/Activity.fit")?;
//! for data in fitparser::from_reader(&mut fp)? {
//!     // print the data in FIT file
//!     println!("{:#?}", data);
//! }
//!
//! // Optionally ignore CRC validation
//! let opts = [DecodeOption::SkipHeaderCrcValidation,
//!             DecodeOption::SkipDataCrcValidation].iter().map(|o| *o).collect();
//! let mut fp = File::open("tests/fixtures/Activity.fit")?;
//! for data in from_reader_with_options(&mut fp, &opts)? {
//!     // print the data in FIT file
//!     println!("{:#?}", data);
//! }
//!
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
#![warn(missing_docs)]
use chrono::{DateTime, Local};
use profile::field_types::FitBaseType;
use serde::Serialize;
use std::collections::HashMap;
use std::convert;
use std::fmt;

pub mod de;
mod error;
pub mod profile;

pub use de::{from_bytes, from_reader};
pub use error::{Error, ErrorKind, Result};

/// Defines a set of data derived from a FIT Data message.
#[derive(Clone, Debug, Serialize)]
pub struct FitDataRecord {
    /// The kind of message the data came from, the FIT profile defines several messages and
    /// custom messages can be defined by altering the profile
    kind: profile::MesgNum,
    /// All the fields present in this message, a record may not have every possible field defined
    fields: Vec<FitDataField>,
}

impl FitDataRecord {
    /// Create an empty data record with a given kind
    pub fn new(kind: profile::MesgNum) -> Self {
        FitDataRecord {
            kind,
            fields: Vec::new(),
        }
    }

    /// Return the kind of FitDataRecord, this value is defined by the FIT profile.
    pub fn kind(&self) -> profile::MesgNum {
        self.kind
    }

    /// Get all fields as a slice
    pub fn fields(&self) -> &[FitDataField] {
        &self.fields
    }

    /// Add a field to the record
    pub fn push(&mut self, field: FitDataField) {
        self.fields.push(field)
    }

    /// Add multiple fields to the record
    pub fn extend(&mut self, fields: Vec<FitDataField>) {
        self.fields.extend(fields)
    }

    /// Consume the record and return the field vector for further processing
    pub fn into_vec(self) -> Vec<FitDataField> {
        self.fields
    }
}

/// Stores a value and it's defined units which are set by the FIT profile during decoding
#[derive(Clone, Debug, Serialize)]
pub struct FitDataField {
    name: String,
    number: u8,
    developer_data_index: Option<u8>, // None for built-in fields, for developer fields identifies which developer
    value: Value,
    units: String,
}

impl FitDataField {
    /// Create a new FitDataField
    pub fn new(
        name: String,
        number: u8,
        developer_data_index: Option<u8>,
        value: Value,
        units: String,
    ) -> Self {
        FitDataField {
            name,
            number,
            developer_data_index,
            value,
            units,
        }
    }

    /// Return the field name as defined in the FIT profile
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the field definition number
    pub fn number(&self) -> u8 {
        self.number
    }

    /// Return a reference to the stored value
    pub fn value(&self) -> &Value {
        &self.value
    }

    /// Return units associated with the value
    pub fn units(&self) -> &str {
        &self.units
    }

    /// Consume the field and return the value
    pub fn into_value(self) -> Value {
        self.value
    }
}

impl fmt::Display for FitDataField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.units.is_empty() {
            write!(f, "{}", self.value)
        } else {
            write!(f, "{} {}", self.value, self.units)
        }
    }
}

/// Contains arbitrary data in the defined format.
#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize)]
#[serde(untagged)]
pub enum Value {
    /// Timestamp field converted to the local timezone
    Timestamp(DateTime<Local>),
    /// Unsigned 8bit integer data
    Byte(u8), // TODO: I think this should actually be a Vec<u8> type
    /// Unsigned 8bit integer that gets mapped to a FieldType enum
    Enum(u8),
    /// Signed 8bit integer data
    SInt8(i8),
    /// Unsigned 8bit integer data
    UInt8(u8),
    /// Signed 16bit integer data
    SInt16(i16),
    /// Unsigned 16bit integer data
    UInt16(u16),
    /// Signed 32bit integer data
    SInt32(i32),
    /// Unsigned 32bit integer data
    UInt32(u32),
    /// UTF-8 format string data
    String(String),
    /// 32bit floating point data
    Float32(f32),
    /// 64bit floating point data
    Float64(f64),
    /// Unsigned 8bit integer data where the invalid value is `0x0` instead of `0xFF`
    UInt8z(u8),
    /// Unsigned 16bit integer data where the invalid value is `0x0` instead of `0xFFFF`
    UInt16z(u16),
    /// Unsigned 16bit integer data where the invalid value is `0x0` instead of `0xFFFFFFFF`
    UInt32z(u32),
    /// Signed 64bit integer data
    SInt64(i64),
    /// Unsigned 64bit integer data
    UInt64(u64),
    /// Unsigned 64bit integer data where the invalid value is `0x0` instead of `0xFFFFFFFFFFFFFFFF`
    UInt64z(u64),
    /// Array of Values, while this allows nested arrays and mixed types this is not possible
    /// in a properly formatted FIT file
    Array(Vec<Self>),
    /// Placeholder for invalid values in output
    Invalid,
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            Value::Timestamp(val) => write!(f, "{}", val),
            Value::Byte(val) => write!(f, "{}", val),
            Value::Enum(val) => write!(f, "{}", val),
            Value::SInt8(val) => write!(f, "{}", val),
            Value::UInt8(val) => write!(f, "{}", val),
            Value::UInt8z(val) => write!(f, "{}", val),
            Value::SInt16(val) => write!(f, "{}", val),
            Value::UInt16(val) => write!(f, "{}", val),
            Value::UInt16z(val) => write!(f, "{}", val),
            Value::SInt32(val) => write!(f, "{}", val),
            Value::UInt32(val) => write!(f, "{}", val),
            Value::UInt32z(val) => write!(f, "{}", val),
            Value::SInt64(val) => write!(f, "{}", val),
            Value::UInt64(val) => write!(f, "{}", val),
            Value::UInt64z(val) => write!(f, "{}", val),
            Value::Float32(val) => write!(f, "{}", val),
            Value::Float64(val) => write!(f, "{}", val),
            Value::String(val) => write!(f, "{}", val),
            Value::Array(vals) => write!(f, "{:?}", vals), // printing arrays is hard,
            Value::Invalid => write!(f, ""),
        }
    }
}

impl convert::TryInto<f64> for Value {
    type Error = error::Error;

    fn try_into(self) -> Result<f64> {
        match self {
            Value::Timestamp(val) => Ok(val.timestamp() as f64),
            Value::Byte(val) => Ok(val as f64),
            Value::Enum(val) => Ok(val as f64),
            Value::SInt8(val) => Ok(val as f64),
            Value::UInt8(val) => Ok(val as f64),
            Value::UInt8z(val) => Ok(val as f64),
            Value::SInt16(val) => Ok(val as f64),
            Value::UInt16(val) => Ok(val as f64),
            Value::UInt16z(val) => Ok(val as f64),
            Value::SInt32(val) => Ok(val as f64),
            Value::UInt32(val) => Ok(val as f64),
            Value::UInt32z(val) => Ok(val as f64),
            Value::SInt64(val) => Ok(val as f64),
            Value::UInt64(val) => Ok(val as f64),
            Value::UInt64z(val) => Ok(val as f64),
            Value::Float32(val) => Ok(val as f64),
            Value::Float64(val) => Ok(val),
            Value::String(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an f64", self)).into())
            }
            Value::Array(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an f64", self)).into())
            }
            Value::Invalid => Err(ErrorKind::ValueError(
                "cannot convert an invalid value into an f64".to_string(),
            )
            .into()),
        }
    }
}

impl convert::TryInto<i64> for Value {
    type Error = error::Error;

    fn try_into(self) -> Result<i64> {
        match self {
            Value::Timestamp(val) => Ok(val.timestamp()),
            Value::Byte(val) => Ok(val as i64),
            Value::Enum(val) => Ok(val as i64),
            Value::SInt8(val) => Ok(val as i64),
            Value::UInt8(val) => Ok(val as i64),
            Value::UInt8z(val) => Ok(val as i64),
            Value::SInt16(val) => Ok(val as i64),
            Value::UInt16(val) => Ok(val as i64),
            Value::UInt16z(val) => Ok(val as i64),
            Value::SInt32(val) => Ok(val as i64),
            Value::UInt32(val) => Ok(val as i64),
            Value::UInt32z(val) => Ok(val as i64),
            Value::SInt64(val) => Ok(val),
            Value::UInt64(val) => Ok(val as i64),
            Value::UInt64z(val) => Ok(val as i64),
            Value::Float32(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Float64(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::String(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Array(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Invalid => Err(ErrorKind::ValueError(
                "cannot convert an invalid value into an i64".to_string(),
            )
            .into()),
        }
    }
}

impl convert::TryInto<i64> for &Value {
    type Error = error::Error;

    fn try_into(self) -> Result<i64> {
        match self {
            Value::Timestamp(val) => Ok(val.timestamp()),
            Value::Byte(val) => Ok(*val as i64),
            Value::Enum(val) => Ok(*val as i64),
            Value::SInt8(val) => Ok(*val as i64),
            Value::UInt8(val) => Ok(*val as i64),
            Value::UInt8z(val) => Ok(*val as i64),
            Value::SInt16(val) => Ok(*val as i64),
            Value::UInt16(val) => Ok(*val as i64),
            Value::UInt16z(val) => Ok(*val as i64),
            Value::SInt32(val) => Ok(*val as i64),
            Value::UInt32(val) => Ok(*val as i64),
            Value::UInt32z(val) => Ok(*val as i64),
            Value::SInt64(val) => Ok(*val),
            Value::UInt64(val) => Ok(*val as i64),
            Value::UInt64z(val) => Ok(*val as i64),
            Value::Float32(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Float64(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::String(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Array(_) => {
                Err(ErrorKind::ValueError(format!("cannot convert {} into an i64", self)).into())
            }
            Value::Invalid => Err(ErrorKind::ValueError(
                "cannot convert an invalid value into an i64".to_string(),
            )
            .into()),
        }
    }
}

/// Describes a field value along with its defined units (if any), this struct is useful for
/// serializing data in a key-value store where the key is either the name or definition number
/// since it can be created from a `FitDataField` with minimal data cloning.
#[derive(Clone, Debug, Serialize)]
pub struct ValueWithUnits {
    value: Value,
    units: String,
}

impl ValueWithUnits {
    /// Create a new value with the given information
    pub fn new(value: Value, units: String) -> Self {
        ValueWithUnits { value, units }
    }
}

impl convert::From<FitDataField> for ValueWithUnits {
    fn from(field: FitDataField) -> Self {
        ValueWithUnits::new(field.value, field.units)
    }
}

impl fmt::Display for ValueWithUnits {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.units.is_empty() {
            write!(f, "{}", self.value)
        } else {
            write!(f, "{} {}", self.value, self.units)
        }
    }
}

/// Developer fields are fields with properties that are not already defined
/// by the SDK, but instead the definition is part of the fit file, for more
/// flexiblity. Since their properties are dynamically defined and cannot be
/// looked up statically we need to store these properties. This is what the
/// DeveloperFieldDescription struct is for.
///
#[derive(Debug)]
pub struct DeveloperFieldDescription {
    developer_data_index: u8,
    field_definition_number: u8,
    fit_base_type_id: FitBaseType,
    field_name: String,
    scale: f64,
    offset: f64,
    units: String,
}

impl TryFrom<&Vec<FitDataField>> for DeveloperFieldDescription {
    type Error = ErrorKind;

    /// Create DeveloperFieldDescription from FitDataFields
    fn try_from(
        fields: &Vec<FitDataField>,
    ) -> std::result::Result<DeveloperFieldDescription, error::ErrorKind> {
        let mut name_to_value = HashMap::new();
        for field in fields {
            name_to_value.insert(field.name.clone(), field.value.clone());
        }
        let developer_data_index = if let Value::UInt8(developer_data_index) = name_to_value
            .get("developer_data_index")
            .ok_or(ErrorKind::ValueError(
                "developer_data_index is mandatory".to_string(),
            ))? {
            *developer_data_index
        } else {
            return Err(ErrorKind::ValueError(
                "developer_data_index must be u8".to_string(),
            ));
        };
        let field_definition_number = if let Value::UInt8(field_definition_number) = name_to_value
            .get("field_definition_number")
            .ok_or(ErrorKind::ValueError(
                "field_definition_number is mandatory".to_string(),
            ))? {
            *field_definition_number
        } else {
            return Err(ErrorKind::ValueError(
                "field_definition_number must be u8".to_string(),
            ));
        };
        let fit_base_type_id = if let Value::String(fit_base_type_id) = name_to_value
            .get("fit_base_type_id")
            .ok_or(ErrorKind::ValueError(
                "fit_base_type_id is mandatory".to_string(),
            ))? {
            // Since the decoder turns enums to string, we need to undo it to get FitBaseType back
            FitBaseType::from(fit_base_type_id as &str)
        } else {
            return Err(ErrorKind::ValueError(
                "fit_base_type_id must be string".to_string(),
            ));
        };
        let field_name = if let Value::String(field_name) = name_to_value
            .get("field_name")
            .unwrap_or(&Value::String(format!(
                "unknown_developer_field_{}",
                field_definition_number
            ))) {
            field_name.clone()
        } else {
            return Err(ErrorKind::ValueError(
                "field_name must be string".to_string(),
            ));
        };
        let scale =
            if let Value::UInt8(scale) = name_to_value.get("scale").unwrap_or(&Value::UInt8(1u8)) {
                *scale as f64
            } else {
                return Err(ErrorKind::ValueError("scale must be u8".to_string()));
            };
        let offset = if let Value::SInt8(offset) =
            name_to_value.get("offset").unwrap_or(&Value::SInt8(0i8))
        {
            *offset as f64
        } else {
            return Err(ErrorKind::ValueError("offset must be i8".to_string()));
        };
        let units = if let Value::String(units) = name_to_value
            .get("units")
            .unwrap_or(&Value::String(String::new()))
        {
            units.clone()
        } else {
            return Err(ErrorKind::ValueError("units must be string".to_string()));
        };

        Ok(DeveloperFieldDescription {
            developer_data_index,
            field_definition_number,
            fit_base_type_id,
            field_name,
            scale,
            offset,
            units,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    #[test]
    fn parse_activity() {
        let data = include_bytes!("../tests/fixtures/Activity.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 22);
    }

    #[test]
    fn parse_developer_data() {
        let data = include_bytes!("../tests/fixtures/DeveloperData.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 6);
        // make sure we correctly parsed the dev data
        assert_eq!(fit_data[3].fields().len(), 5);
        assert_eq!(fit_data[3].fields()[4].name(), "doughnuts_earned");
        assert_eq!(fit_data[3].fields()[4].value(), &Value::SInt8(1));
        assert_eq!(fit_data[3].fields()[4].units(), "doughnuts");
    }

    #[test]
    fn parse_monitoring_file() {
        let data = include_bytes!("../tests/fixtures/MonitoringFile.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 355);
    }

    #[test]
    fn parse_settings() {
        let data = include_bytes!("../tests/fixtures/Settings.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 3);
    }

    #[test]
    fn parse_weight_scale_multi_user() {
        let data = include_bytes!("../tests/fixtures/WeightScaleMultiUser.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 7);
    }

    #[test]
    fn parse_weight_scale_single_user() {
        let data = include_bytes!("../tests/fixtures/WeightScaleSingleUser.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 6);
    }

    #[test]
    fn parse_workout_custom_target_values() {
        let data = include_bytes!("../tests/fixtures/WorkoutCustomTargetValues.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 6);
    }

    #[test]
    fn parse_workout_individual_steps() {
        let data = include_bytes!("../tests/fixtures/WorkoutIndividualSteps.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 6);
    }

    #[test]
    fn parse_workout_repeat_greater_than_step() {
        let data = include_bytes!("../tests/fixtures/WorkoutRepeatGreaterThanStep.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 7);
    }

    #[test]
    fn parse_workout_repeat_steps() {
        let data = include_bytes!("../tests/fixtures/WorkoutRepeatSteps.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 7);
    }

    #[test]
    fn parse_garmin_fenix_5_bike() {
        // this test case includes a FIT file with a string field, which was broken in v0.1.0
        // and fixed in v0.1.1
        let data = include_bytes!("../tests/fixtures/garmin-fenix-5-bike.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 143);
    }

    #[test]
    fn parse_sample_mulitple_header() {
        // this test case includes a chained FIT file
        let data = include_bytes!("../tests/fixtures/sample_mulitple_header.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 3023);
    }

    #[test]
    fn parse_activity_with_hrv() {
        // this test case includes a chained FIT file
        let data = include_bytes!("../tests/fixtures/hrv-activity.fit").to_vec();
        let fit_data = from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 2260);
        // find the first HRV message and check the array
        // we should see a partial set of bytes
        let field = fit_data
            .into_iter()
            .find(|rec| rec.kind == profile::MesgNum::Hrv)
            .map(|rec| rec.fields)
            .expect("We should have at least one HRV message")
            .into_iter()
            .find(|fld| fld.name == "time")
            .expect("We should have at least one time field");
        assert_eq!(
            field.value,
            Value::Array(vec![
                Value::Float64(0.467),
                Value::Float64(0.464),
                Value::Invalid,
                Value::Invalid,
                Value::Invalid
            ])
        );
    }

    #[test]
    fn parse_with_header_crc_set_to_zero() {
        // Set header CRC to zero so that the CRC at the EOF includes all bytes
        let mut data = include_bytes!("../tests/fixtures/garmin-fenix-5-bike.fit").to_vec();
        let leng = data.len();
        data[12] = 0x00;
        data[13] = 0x00;
        match de::from_bytes(&data) {
            Ok(_) => panic!(
                "This test should fail without the data CRC value being recomputed to include the header."
            ),
            Err(e) => match *e {
                ErrorKind::InvalidCrc(..) => {}
                _ => panic!("Incorrect error returned {:?}", e),
            },
        }

        // update data CRC value so that it includes the header
        data[leng - 2] = 0x58;
        data[leng - 1] = 0x65;
        let fit_data = de::from_bytes(&data).unwrap();
        assert_eq!(fit_data.len(), 143);
    }

    #[test]
    fn parse_with_invalid_header_crc_with_options() {
        // force header CRC to be an invalid, non-zero value
        let mut data = include_bytes!("../tests/fixtures/MonitoringFile.fit").to_vec();
        data[12] = 0xFF;
        data[13] = 0xFF;
        let mut options = HashSet::new();
        match de::from_bytes_with_options(&data, &options) {
            Ok(_) => panic!("This test should fail without the SkipHeaderCrcValidation option."),
            Err(e) => match *e {
                ErrorKind::InvalidCrc(..) => {}
                _ => panic!("Incorrect error returned {:?}", e),
            },
        }

        // add proper option to decode file
        options.insert(de::DecodeOption::SkipHeaderCrcValidation);
        let fit_data = de::from_bytes_with_options(&data, &options).unwrap();
        assert_eq!(fit_data.len(), 355);
    }

    #[test]
    fn parse_with_invalid_data_crc_with_options() {
        // force data CRC to be an invalid value
        let mut data = include_bytes!("../tests/fixtures/MonitoringFile.fit").to_vec();
        let leng = data.len();
        data[leng - 2] = 0xFF;
        data[leng - 1] = 0xFF;
        let mut options = HashSet::new();
        match de::from_bytes_with_options(&data, &options) {
            Ok(_) => panic!("This test should fail without the SkipDataCrcValidation option."),
            Err(e) => match *e {
                ErrorKind::InvalidCrc(..) => {}
                _ => panic!("Incorrect error returned {:?}", e),
            },
        }

        // add proper option to decode file
        options.insert(de::DecodeOption::SkipDataCrcValidation);
        let fit_data = de::from_bytes_with_options(&data, &options).unwrap();
        assert_eq!(fit_data.len(), 355);
    }
}