Skip to content

Commit

Permalink
Merge pull request #27 from dragonnn/feature/parse-frame-with-invalid…
Browse files Browse the repository at this point in the history
…-manufacturer

feat: avoid error out on frame with invalid manufacturer code
  • Loading branch information
maebli authored Nov 21, 2024
2 parents 1a0b5a7 + 0a82cf4 commit becbe50
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ fn parse_to_table(input: &str) -> std::string::String {
}) => {
table.add_row(row![
fixed_data_header.identification_number,
fixed_data_header.manufacturer,
fixed_data_header
.manufacturer
.map(|i| i.to_string())
.unwrap_or_else(|_| "invalid".to_string()),
fixed_data_header.access_number,
fixed_data_header.status,
fixed_data_header.signature,
Expand Down
6 changes: 3 additions & 3 deletions src/user_data/data_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> DataInformationFieldExtensions<'a> {
}
}

impl<'a> Iterator for DataInformationFieldExtensions<'a> {
impl Iterator for DataInformationFieldExtensions<'_> {
type Item = DataInformationFieldExtension;
fn next(&mut self) -> Option<Self::Item> {
let (head, tail) = self.0.split_first()?;
Expand All @@ -69,8 +69,8 @@ impl<'a> Iterator for DataInformationFieldExtensions<'a> {
(self.0.len(), Some(self.0.len()))
}
}
impl<'a> ExactSizeIterator for DataInformationFieldExtensions<'a> {}
impl<'a> DoubleEndedIterator for DataInformationFieldExtensions<'a> {
impl ExactSizeIterator for DataInformationFieldExtensions<'_> {}
impl DoubleEndedIterator for DataInformationFieldExtensions<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
let (end, start) = self.0.split_last()?;
self.0 = start;
Expand Down
2 changes: 1 addition & 1 deletion src/user_data/data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'a> TryFrom<&'a [u8]> for RawDataRecordHeader<'a> {
}
}

impl<'a> TryFrom<&RawDataRecordHeader<'a>> for ProcessedDataRecordHeader {
impl TryFrom<&RawDataRecordHeader<'_>> for ProcessedDataRecordHeader {
type Error = DataRecordError;
fn try_from(raw_data_record_header: &RawDataRecordHeader) -> Result<Self, DataRecordError> {
let mut value_information = None;
Expand Down
4 changes: 2 additions & 2 deletions src/user_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl fmt::Display for Medium {
#[derive(Debug, PartialEq)]
pub struct FixedDataHeader {
pub identification_number: IdentificationNumber,
pub manufacturer: ManufacturerCode,
pub manufacturer: Result<ManufacturerCode, ApplicationLayerError>,
pub version: u8,
pub medium: Medium,
pub access_number: u8,
Expand Down Expand Up @@ -613,7 +613,7 @@ impl<'a> TryFrom<&'a [u8]> for UserDataBlock<'a> {
manufacturer: ManufacturerCode::from_id(u16::from_le_bytes([
*iter.next().ok_or(ApplicationLayerError::InsufficientData)?,
*iter.next().ok_or(ApplicationLayerError::InsufficientData)?,
]))?,
])),
version: *iter.next().ok_or(ApplicationLayerError::InsufficientData)?,
medium: MeasuredMedium::new(
*iter.next().ok_or(ApplicationLayerError::InsufficientData)?,
Expand Down
16 changes: 4 additions & 12 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,10 @@ mod tests {
.manufacturer
.unwrap()
.into_bytes();
assert_eq!(
fixed_data_header.manufacturer.code[0],
expected_manufacturer[0] as char
);
assert_eq!(
fixed_data_header.manufacturer.code[1],
expected_manufacturer[1] as char
);
assert_eq!(
fixed_data_header.manufacturer.code[2],
expected_manufacturer[2] as char
);
let manufacturer = fixed_data_header.manufacturer.unwrap();
assert_eq!(manufacturer.code[0], expected_manufacturer[0] as char);
assert_eq!(manufacturer.code[1], expected_manufacturer[1] as char);
assert_eq!(manufacturer.code[2], expected_manufacturer[2] as char);
assert_eq!(
fixed_data_header.access_number,
mbus_data.slave_information.access_number as u8
Expand Down

0 comments on commit becbe50

Please sign in to comment.