Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crc8: Use explicit error type #18

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This project follows [semantic versioning](https://semver.org/).

## [Unreleased]

### Changed

* Add a separate error type for the crc8 module to allow for
`From<crc8::Error>` implementations.
([#18](https://github.com/Sensirion/sensirion-i2c-rs/pull/18/))

## [0.1.1] (2020-09-02)

### Changed
Expand Down
16 changes: 13 additions & 3 deletions src/crc8.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! Helper functions for CRC8 checksum validation

/// Errors which can happen in the crc8 module
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
/// CRC validation failed
CrcError,
}

/// Calculate the CRC8 checksum.
pub fn calculate(data: &[u8]) -> u8 {
const CRC8_POLYNOMIAL: u8 = 0x31;
Expand Down Expand Up @@ -27,11 +34,11 @@ pub fn calculate(data: &[u8]) -> u8 {
///
/// This method will consider every third byte a checksum byte. If the buffer size is not a
/// multiple of 3, then it will panic.
pub fn validate(buf: &[u8]) -> Result<(), ()> {
pub fn validate(buf: &[u8]) -> Result<(), Error> {
assert!(buf.len() % 3 == 0, "Buffer must be a multiple of 3");
for chunk in buf.chunks(3) {
if calculate(&[chunk[0], chunk[1]]) != chunk[2] {
return Err(());
return Err(Error::CrcError);
}
}
Ok(())
Expand Down Expand Up @@ -66,6 +73,9 @@ mod tests {
crc8::validate(&[0xbe, 0xef, 0x92]).unwrap();

// Invalid CRC
assert_eq!(crc8::validate(&[0xbe, 0xef, 0x91]), Err(()));
assert_eq!(
crc8::validate(&[0xbe, 0xef, 0x91]),
Err(crc8::Error::CrcError)
);
}
}
11 changes: 10 additions & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ pub enum Error<I2cWrite: i2c::Write, I2cRead: i2c::Read> {
Crc,
}

impl<W: i2c::Write, R: i2c::Read> From<crc8::Error> for Error<W, R> {
fn from(err: crc8::Error) -> Error<W, R> {
match err {
crc8::Error::CrcError => Error::Crc,
}
}
}

/// Write an u16 command to the I²C bus.
pub fn write_command<I2cWrite: i2c::Write>(
i2c: &mut I2cWrite,
Expand Down Expand Up @@ -38,7 +46,8 @@ pub fn read_words_with_crc<I2c: i2c::Read + i2c::Write>(
"Buffer must hold a multiple of 3 bytes"
);
i2c.read(addr, data).map_err(Error::I2cRead)?;
crc8::validate(data).map_err(|_| Error::Crc)
crc8::validate(data)?;
Ok(())
}

#[cfg(test)]
Expand Down