Skip to content

Commit

Permalink
messages: Collect unknown fields in message type
Browse files Browse the repository at this point in the history
In the initial implementation, we had a config option to determine
whether unknown fields should return an error.  Based on the discussion
in [0], I think it is better to just collect unexpected fields in the
message type and let the user decide how to handle them.

[0] #13
  • Loading branch information
robinkrahl committed Feb 23, 2024
1 parent 5177185 commit 82d4d65
Show file tree
Hide file tree
Showing 4 changed files with 1,029 additions and 1,173 deletions.
44 changes: 25 additions & 19 deletions fitparser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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)]
#[derive(Clone, Debug, PartialEq, PartialOrd, 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
Expand Down Expand Up @@ -93,7 +93,7 @@ impl FitDataRecord {
}

/// Stores a value and it's defined units which are set by the FIT profile during decoding
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize)]
pub struct FitDataField {
name: String,
number: u8,
Expand Down Expand Up @@ -357,7 +357,7 @@ impl fmt::Display for ValueWithUnits {

#[cfg(test)]
mod tests {
use super::profile::{MesgNum, Message, MessageParseOptions};
use super::profile::{MesgNum, Message};
use super::*;
use std::collections::HashSet;

Expand All @@ -367,7 +367,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 22);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -377,7 +378,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 6);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -387,7 +389,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 355);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -397,7 +400,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 3);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -407,7 +411,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 7);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -417,7 +422,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 6);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -427,7 +433,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 6);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -437,7 +444,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 6);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -447,7 +455,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 7);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -457,7 +466,8 @@ mod tests {
let fit_data = from_bytes(&data).unwrap();
assert_eq!(fit_data.len(), 7);
for record in fit_data {
assert!(Message::parse(record).is_ok());
let message = Message::parse(record).unwrap();
assert!(message.unknown_fields().is_empty());
}
}

Expand All @@ -468,11 +478,9 @@ mod tests {
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);
let mut options = MessageParseOptions::default();
options.ignore_unexpected_fields = true;
for record in fit_data {
if MesgNum::is_named_variant(record.kind().as_i64()) {
assert!(Message::parse_with_options(record, options).is_ok());
assert!(Message::parse(record).is_ok());
}
}
}
Expand All @@ -483,11 +491,9 @@ mod tests {
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);
let mut options = MessageParseOptions::default();
options.ignore_unexpected_fields = true;
for record in fit_data {
if MesgNum::is_named_variant(record.kind().as_i64()) {
assert!(Message::parse_with_options(record, options).is_ok());
assert!(Message::parse(record).is_ok());
}
}
}
Expand Down
Loading

0 comments on commit 82d4d65

Please sign in to comment.