Skip to content

Commit

Permalink
Merge pull request #37 from trnila/pid-without-assert
Browse files Browse the repository at this point in the history
Pid::new returns Result instead of asserting in case of an error
  • Loading branch information
psachs authored Oct 24, 2024
2 parents fb30ce2 + d3baaa0 commit ce51f58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This project follows [semantic versioning](https://semver.org/).

## [Unreleased]
* breaking: `Pid::new` returns `Result` instead of asserting in case of an error.
([#37](https://github.com/Sensirion/lin-bus-rs/pull/37))

## [0.4.0] (2021-12-16)

* changed: `PID::new`, `PID::from_id`, `PCI::new_sf` and `PCI::get_type` are
Expand Down
15 changes: 8 additions & 7 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ pub struct PID(u8);

impl PID {
/// Creates a new PID object with given PID
pub const fn new(pid: u8) -> PID {
// check that the given PID has valid parity bits
pub const fn new(pid: u8) -> Result<PID, &'static str> {
let correct_pid = PID::from_id(pid & 0b0011_1111);
assert!(correct_pid.0 == pid, "Invalid PID");
correct_pid
if correct_pid.0 == pid {
Ok(correct_pid)
} else {
Err("Invalid parity bits")
}
}

/// Calculate the PID from an ID.
Expand Down Expand Up @@ -414,14 +416,13 @@ mod tests {
];

for d in &test_data {
assert_eq!(d.0, d.1.get());
assert_eq!(d.0, d.1.unwrap().get());
}
}

#[test]
#[should_panic]
fn test_invalid_pid_new() {
PID::new(0x07);
assert_eq!(Err("Invalid parity bits"), PID::new(0x07));
}

#[test]
Expand Down
19 changes: 11 additions & 8 deletions src/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ mod tests {
#[test]
fn test_frame_from_data() {
let test_data = [FrameTestData {
pid: PID::new(0xDD),
pid: PID::new(0xDD).unwrap(),
data: &[0x01],
frame: Frame {
pid: PID::new(0xDD),
pid: PID::new(0xDD).unwrap(),
buffer: [0x01, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
data_length: 1,
},
Expand All @@ -84,22 +84,25 @@ mod tests {
fn test_data_decode() {
let test_data = [
(
Frame::from_data(PID::new(80), &[254, 251, 239, 255]),
Frame::from_data(PID::new(80).unwrap(), &[254, 251, 239, 255]),
[1022, 1022, 2046],
),
(
Frame::from_data(PID::new(80), &[3, 12, 240, 182]),
Frame::from_data(PID::new(80).unwrap(), &[3, 12, 240, 182]),
[3, 3, 879],
),
(
Frame::from_data(PID::new(80), &[3, 12, 0, 183]),
Frame::from_data(PID::new(80).unwrap(), &[3, 12, 0, 183]),
[3, 3, 880],
),
(
Frame::from_data(PID::new(80), &[2, 12, 240, 182]),
Frame::from_data(PID::new(80).unwrap(), &[2, 12, 240, 182]),
[2, 3, 879],
),
(Frame::from_data(PID::new(80), &[2, 8, 0, 183]), [2, 2, 880]),
(
Frame::from_data(PID::new(80).unwrap(), &[2, 8, 0, 183]),
[2, 2, 880],
),
];

for d in &test_data {
Expand All @@ -111,7 +114,7 @@ mod tests {

#[test]
fn test_data_decode_all_bits() {
let frame = Frame::from_data(PID::new(80), &[0x55, 0xDD]);
let frame = Frame::from_data(PID::new(80).unwrap(), &[0x55, 0xDD]);
assert_eq!(frame.decode::<u16>(0, 16), 0xdd55);
}
}

0 comments on commit ce51f58

Please sign in to comment.