Skip to content

Commit

Permalink
feat: avoid error out on frame with invalid manufacturer code
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonnn committed Nov 7, 2024
1 parent 1c73aff commit fe3a15a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 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
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 fe3a15a

Please sign in to comment.