Skip to content

Commit

Permalink
Merge a3ba4bb into 9a00079
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvervaeke committed Sep 13, 2021
2 parents 9a00079 + a3ba4bb commit 7c48c56
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -111,6 +111,7 @@ jobs:
matrix:
rust: [stable, beta]
TARGET: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]
feature: ["", " --features std"]
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -124,7 +125,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --target=${{ matrix.TARGET }}
args: --target=${{ matrix.TARGET }}${{ matrix.feature }}

coverage:
name: Coverage
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

...
### Added

- Added `Display` and `Error` implementation for `Error<E>`

## [0.3.1] - 2021-07-14

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -30,3 +30,6 @@ embedded-hal-mock = "0.7"

[profile.release]
lto = true

[features]
std = []
49 changes: 49 additions & 0 deletions src/types.rs
@@ -1,5 +1,10 @@
use crate::config::Config;
use core::convert::TryFrom;
use core::fmt::{Display, Formatter};

#[cfg(feature = "std")]
extern crate std;

const DEVICE_BASE_ADDRESS: u8 = 0b100_0000;

/// PCA9685 PWM/Servo/LED controller.
Expand All @@ -22,6 +27,19 @@ pub enum Error<E> {
InvalidInputData,
}

// Implement Display for Error<E> if E also implements Display
impl<E: Display> Display for Error<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Error::I2C(e) => write!(f, "I²C bus error: {}", e),
Error::InvalidInputData => write!(f, "Invalid input data provided"),
}
}
}

#[cfg(feature = "std")]
impl<E: std::error::Error> std::error::Error for Error<E> {}

/// Output channel selection
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Channel {
Expand Down Expand Up @@ -233,3 +251,34 @@ mod tests {
assert_eq!(DEVICE_BASE_ADDRESS, addr.0);
}
}

#[cfg(all(test, feature = "std"))]
mod std_tests {
use super::*;
use std::format;

struct TestError;
impl Display for TestError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "test")
}
}

#[test]
fn test_display_implementation_invalid_input_data() {
let expected = "Invalid input data provided";
let error = Error::<TestError>::InvalidInputData;
let actual = format!("{}", error);

assert_eq!(expected, actual)
}

#[test]
fn test_display_implementation_i2c_error() {
let expected = "I²C bus error: test";
let error = Error::<TestError>::I2C(TestError);
let actual = format!("{}", error);

assert_eq!(expected, actual)
}
}

0 comments on commit 7c48c56

Please sign in to comment.