From bccfc81b134090220b9339a0d2f91a6528c57c4f Mon Sep 17 00:00:00 2001 From: Caemor <11088935+caemor@users.noreply.github.com> Date: Sat, 23 Jan 2021 14:55:03 +0100 Subject: [PATCH] Improve Stuff mentioned by clippy: - use range.contains instead of >= && <= - Use better error type --- CHANGELOG.md | 13 +++++++++++++ src/color.rs | 15 ++++++++++++--- src/epd2in13_v2/command.rs | 14 +++++--------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6fba5f..af0425f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + + +### Changed + - Use specific ParseColorError instead of () + +### Fixed + + +## [v0.5.0] + +TODO! + ## [v0.4.0] - 2020-04-06 ### Added diff --git a/src/color.rs b/src/color.rs index 6767673..c3092ca 100644 --- a/src/color.rs +++ b/src/color.rs @@ -10,6 +10,15 @@ pub use BinaryColor::Off as White; #[cfg(feature = "graphics")] pub use BinaryColor::On as Black; +/// When trying to parse u8 to one of the color types +#[derive(Debug)] +pub struct OutOfColorRangeParseError(u8); +impl core::fmt::Display for OutOfColorRangeParseError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "Outside of possible Color Range: {}", self.0) + } +} + /// Only for the Black/White-Displays #[derive(Clone, Copy, PartialEq, Debug)] pub enum Color { @@ -73,7 +82,7 @@ impl OctColor { } ///Take the nibble (lower 4 bits) and convert to an OctColor if possible - pub fn from_nibble(nibble: u8) -> Result { + pub fn from_nibble(nibble: u8) -> Result { match nibble & 0xf { 0x00 => Ok(OctColor::Black), 0x01 => Ok(OctColor::White), @@ -83,11 +92,11 @@ impl OctColor { 0x05 => Ok(OctColor::Yellow), 0x06 => Ok(OctColor::Orange), 0x07 => Ok(OctColor::HiZ), - _ => Err(()), + e => Err(OutOfColorRangeParseError(e)), } } ///Split the nibbles of a single byte and convert both to an OctColor if possible - pub fn split_byte(byte: u8) -> Result<(OctColor, OctColor), ()> { + pub fn split_byte(byte: u8) -> Result<(OctColor, OctColor), OutOfColorRangeParseError> { let low = OctColor::from_nibble(byte & 0xf)?; let high = OctColor::from_nibble((byte >> 4) & 0xf)?; Ok((high, low)) diff --git a/src/epd2in13_v2/command.rs b/src/epd2in13_v2/command.rs index b7e1986..9ab4ddd 100644 --- a/src/epd2in13_v2/command.rs +++ b/src/epd2in13_v2/command.rs @@ -221,7 +221,7 @@ impl I32Ext for i32 { // This is really not very nice. Until I find something better, this will be // a placeholder. fn vcom(self) -> VCOM { - assert!(self >= -30 && self <= -2); + assert!((-30..=-2).contains(&self)); let u = match -self { 2 => 0x08, 3 => 0x0B, @@ -258,20 +258,16 @@ impl I32Ext for i32 { } fn gate_driving_decivolt(self) -> GateDrivingVoltage { - assert!(self >= 100 && self <= 210 && self % 5 == 0); + assert!((100..=210).contains(&self) && self % 5 == 0); GateDrivingVoltage(((self - 100) / 5 + 0x03) as u8) } fn source_driving_decivolt(self) -> SourceDrivingVoltage { - assert!( - (self >= 24 && self <= 88) - || (self >= 90 && self <= 180 && self % 5 == 0) - || (self >= -180 && self <= -90 && self % 5 == 0) - ); + assert!((24..=88).contains(&self) || (self % 5 == 0 && (90..=180).contains(&self.abs()))); - if self >= 24 && self <= 88 { + if (24..=88).contains(&self) { SourceDrivingVoltage(((self - 24) + 0x8E) as u8) - } else if self >= 90 && self <= 180 { + } else if (90..=180).contains(&self) { SourceDrivingVoltage(((self - 90) / 2 + 0x23) as u8) } else { SourceDrivingVoltage((((-self - 90) / 5) * 2 + 0x1A) as u8)