Skip to content

Commit 850cfc1

Browse files
committed
implement std error for all protocol errors
1 parent 09745da commit 850cfc1

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

src/cdev_pin.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+
use std::fmt;
6+
57
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
68
///
79
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
@@ -33,7 +35,7 @@ impl CdevPin {
3335
} else if self.1.is_open_source() {
3436
flags.insert(gpio_cdev::LineRequestFlags::OPEN_SOURCE);
3537
}
36-
return flags;
38+
flags
3739
}
3840

3941
/// Set this pin to input mode
@@ -113,6 +115,18 @@ impl From<gpio_cdev::errors::Error> for CdevPinError {
113115
}
114116
}
115117

118+
impl fmt::Display for CdevPinError {
119+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120+
write!(f, "{}", self.err)
121+
}
122+
}
123+
124+
impl std::error::Error for CdevPinError {
125+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126+
Some(&self.err)
127+
}
128+
}
129+
116130
impl embedded_hal::digital::Error for CdevPinError {
117131
fn kind(&self) -> embedded_hal::digital::ErrorKind {
118132
use embedded_hal::digital::ErrorKind;
@@ -125,7 +139,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
125139
}
126140

127141
impl embedded_hal::digital::OutputPin for CdevPin {
128-
fn set_low(&mut self) -> Result<(), CdevPinError> {
142+
fn set_low(&mut self) -> Result<(), Self::Error> {
129143
self.0
130144
.set_value(state_to_value(
131145
embedded_hal::digital::PinState::Low,
@@ -134,7 +148,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
134148
.map_err(CdevPinError::from)
135149
}
136150

137-
fn set_high(&mut self) -> Result<(), CdevPinError> {
151+
fn set_high(&mut self) -> Result<(), Self::Error> {
138152
self.0
139153
.set_value(state_to_value(
140154
embedded_hal::digital::PinState::High,
@@ -145,7 +159,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
145159
}
146160

147161
impl embedded_hal::digital::InputPin for CdevPin {
148-
fn is_high(&self) -> Result<bool, CdevPinError> {
162+
fn is_high(&self) -> Result<bool, Self::Error> {
149163
self.0
150164
.get_value()
151165
.map(|val| {
@@ -157,7 +171,7 @@ impl embedded_hal::digital::InputPin for CdevPin {
157171
.map_err(CdevPinError::from)
158172
}
159173

160-
fn is_low(&self) -> Result<bool, CdevPinError> {
174+
fn is_low(&self) -> Result<bool, Self::Error> {
161175
self.is_high().map(|val| !val)
162176
}
163177
}

src/i2c.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+
use std::fmt;
56
use std::ops;
67
use std::path::{Path, PathBuf};
78

@@ -119,6 +120,18 @@ impl From<i2cdev::linux::LinuxI2CError> for I2CError {
119120
}
120121
}
121122

123+
impl fmt::Display for I2CError {
124+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125+
write!(f, "{}", self.err)
126+
}
127+
}
128+
129+
impl std::error::Error for I2CError {
130+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
131+
Some(&self.err)
132+
}
133+
}
134+
122135
impl embedded_hal::i2c::Error for I2CError {
123136
fn kind(&self) -> embedded_hal::i2c::ErrorKind {
124137
use embedded_hal::i2c::ErrorKind;

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ mod cdev_pin;
3434

3535
#[cfg(feature = "gpio_cdev")]
3636
/// Cdev pin re-export
37-
pub use cdev_pin::CdevPin;
37+
pub use cdev_pin::{CdevPin, CdevPinError};
3838

3939
#[cfg(feature = "gpio_sysfs")]
4040
/// Sysfs pin re-export
41-
pub use sysfs_pin::SysfsPin;
41+
pub use sysfs_pin::{SysfsPin, SysfsPinError};
4242

4343
mod delay;
4444
#[cfg(feature = "i2c")]

src/serial.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5-
use nb;
65
use serialport::{SerialPortBuilder, TTYPort};
6+
use std::fmt;
77
use std::io::{ErrorKind as IoErrorKind, Read, Write};
88

99
/// Newtype around [`serialport::TTYPort`] that implements
@@ -72,6 +72,14 @@ impl SerialError {
7272
}
7373
}
7474

75+
impl fmt::Display for SerialError {
76+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77+
write!(f, "{}", self.err)
78+
}
79+
}
80+
81+
impl std::error::Error for SerialError {}
82+
7583
impl embedded_hal::serial::Error for SerialError {
7684
fn kind(&self) -> embedded_hal::serial::ErrorKind {
7785
use embedded_hal::serial::ErrorKind::*;

src/spi.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
//!
55
6+
use std::fmt;
67
use std::io;
78
use std::ops;
89
use std::path::Path;
@@ -87,8 +88,8 @@ mod embedded_hal_impl {
8788

8889
impl SpiDeviceRead for Spidev {
8990
fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
90-
for mut buf in operations {
91-
SpiBusRead::read(self, &mut buf)?;
91+
for buf in operations {
92+
SpiBusRead::read(self, buf)?;
9293
}
9394
self.flush()?;
9495
Ok(())
@@ -150,3 +151,15 @@ impl embedded_hal::spi::Error for SPIError {
150151
}
151152
}
152153
}
154+
155+
impl fmt::Display for SPIError {
156+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157+
write!(f, "{}", self.err)
158+
}
159+
}
160+
161+
impl std::error::Error for SPIError {
162+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
163+
Some(&self.err)
164+
}
165+
}

src/sysfs_pin.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+
use std::fmt;
56
use std::path::Path;
67

78
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
@@ -65,6 +66,18 @@ impl From<sysfs_gpio::Error> for SysfsPinError {
6566
}
6667
}
6768

69+
impl fmt::Display for SysfsPinError {
70+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71+
write!(f, "{}", self.err)
72+
}
73+
}
74+
75+
impl std::error::Error for SysfsPinError {
76+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
77+
Some(&self.err)
78+
}
79+
}
80+
6881
impl embedded_hal::digital::Error for SysfsPinError {
6982
fn kind(&self) -> embedded_hal::digital::ErrorKind {
7083
use embedded_hal::digital::ErrorKind;
@@ -77,15 +90,15 @@ impl embedded_hal::digital::ErrorType for SysfsPin {
7790
}
7891

7992
impl embedded_hal::digital::OutputPin for SysfsPin {
80-
fn set_low(&mut self) -> Result<(), SysfsPinError> {
93+
fn set_low(&mut self) -> Result<(), Self::Error> {
8194
if self.0.get_active_low().map_err(SysfsPinError::from)? {
8295
self.0.set_value(1).map_err(SysfsPinError::from)
8396
} else {
8497
self.0.set_value(0).map_err(SysfsPinError::from)
8598
}
8699
}
87100

88-
fn set_high(&mut self) -> Result<(), SysfsPinError> {
101+
fn set_high(&mut self) -> Result<(), Self::Error> {
89102
if self.0.get_active_low().map_err(SysfsPinError::from)? {
90103
self.0.set_value(0).map_err(SysfsPinError::from)
91104
} else {
@@ -95,7 +108,7 @@ impl embedded_hal::digital::OutputPin for SysfsPin {
95108
}
96109

97110
impl embedded_hal::digital::InputPin for SysfsPin {
98-
fn is_high(&self) -> Result<bool, SysfsPinError> {
111+
fn is_high(&self) -> Result<bool, Self::Error> {
99112
if !self.0.get_active_low().map_err(SysfsPinError::from)? {
100113
self.0
101114
.get_value()
@@ -109,7 +122,7 @@ impl embedded_hal::digital::InputPin for SysfsPin {
109122
}
110123
}
111124

112-
fn is_low(&self) -> Result<bool, SysfsPinError> {
125+
fn is_low(&self) -> Result<bool, Self::Error> {
113126
self.is_high().map(|val| !val).map_err(SysfsPinError::from)
114127
}
115128
}

0 commit comments

Comments
 (0)