tool: Use From<T> for slightly neater error handling

This commit is contained in:
Ian Douglas Scott 2020-12-02 14:24:57 -08:00 committed by Jeremy Soller
parent 43d31ca0c3
commit 802bf417cc
3 changed files with 21 additions and 7 deletions

View File

@ -38,12 +38,12 @@ impl AccessHid {
hid_data[HID_DATA + i] = data[i];
}
let count = self.device.write(&hid_data).map_err(Error::Hid)?;
let count = self.device.write(&hid_data)?;
if count != hid_data.len() {
return Err(Error::Verify);
}
let count = self.device.read_timeout(&mut hid_data[1..], self.timeout).map_err(Error::Hid)?;
let count = self.device.read_timeout(&mut hid_data[1..], self.timeout)?;
if count == hid_data.len() - 1 {
for i in 0..data.len() {
data[i] = hid_data[HID_DATA + i];

View File

@ -111,8 +111,8 @@ impl AccessLpcLinux {
)));
}
let cmd = PortLock::new(SMFI_CMD_BASE, SMFI_CMD_BASE + SMFI_CMD_SIZE as u16 - 1).map_err(Error::Io)?;
let dbg = PortLock::new(SMFI_DBG_BASE, SMFI_DBG_BASE + SMFI_DBG_SIZE as u16 - 1).map_err(Error::Io)?;
let cmd = PortLock::new(SMFI_CMD_BASE, SMFI_CMD_BASE + SMFI_CMD_SIZE as u16 - 1)?;
let dbg = PortLock::new(SMFI_DBG_BASE, SMFI_DBG_BASE + SMFI_DBG_SIZE as u16 - 1)?;
Ok(Self {
cmd,
dbg,
@ -122,18 +122,18 @@ impl AccessLpcLinux {
/// Read from the command space
unsafe fn read_cmd(&mut self, addr: u8) -> Result<u8, Error> {
self.cmd.read(addr as u16).map_err(Error::Io)
Ok(self.cmd.read(addr as u16)?)
}
/// Write to the command space
unsafe fn write_cmd(&mut self, addr: u8, data: u8) -> Result<(), Error> {
self.cmd.write(addr as u16, data).map_err(Error::Io)
Ok(self.cmd.write(addr as u16, data)?)
}
/// Read from the debug space
//TODO: better public interface
pub unsafe fn read_debug(&mut self, addr: u8) -> Result<u8, Error> {
self.dbg.read(addr as u16).map_err(Error::Io)
Ok(self.dbg.read(addr as u16)?)
}
/// Returns Ok if a command can be sent

View File

@ -26,3 +26,17 @@ pub enum Error {
#[cfg(feature = "hidapi")]
Hid(hidapi::HidError),
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Io(error)
}
}
#[cfg(feature = "hidapi")]
impl From<hidapi::HidError> for Error {
fn from(error: hidapi::HidError) -> Self {
Self::Hid(error)
}
}