summary refs log tree commit diff
path: root/usb_util/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'usb_util/src/error.rs')
-rw-r--r--usb_util/src/error.rs103
1 files changed, 35 insertions, 68 deletions
diff --git a/usb_util/src/error.rs b/usb_util/src/error.rs
index b6dd91e..24d8036 100644
--- a/usb_util/src/error.rs
+++ b/usb_util/src/error.rs
@@ -1,85 +1,52 @@
-// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Copyright 2019 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+use libc::c_ulong;
+use remain::sorted;
 use std;
-use std::fmt;
+use std::fmt::{self, Display};
+use std::io;
+use std::num;
 
-use crate::bindings;
-
-/// Error type for libusb.
+#[sorted]
+#[derive(Debug)]
 pub enum Error {
-    Success(i32),
-    IO,
-    InvalidParam,
-    Access,
+    DescriptorParse,
+    DescriptorRead(io::Error),
+    FdCloneFailed(io::Error),
+    InvalidActualLength(num::TryFromIntError),
+    InvalidBufferLength(num::TryFromIntError),
+    IoctlFailed(c_ulong, sys_util::Error),
     NoDevice,
-    NotFound,
-    Busy,
-    Timeout,
-    Overflow,
-    Pipe,
-    Interrupted,
-    NoMem,
-    NotSupported,
-    Other,
+    NoSuchDescriptor,
+    RcGetMutFailed,
+    RcUnwrapFailed,
+    TransferAlreadyCompleted,
 }
 
-impl fmt::Debug for Error {
+impl Display for Error {
+    #[remain::check]
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match *self {
-            Error::Success(_v) => write!(f, "Success (no error)"),
-            Error::IO => write!(f, "Input/output error"),
-            Error::InvalidParam => write!(f, "Invalid parameter"),
-            Error::Access => write!(f, "Access denied (insufficient permissions)"),
-            Error::NoDevice => write!(f, "No such device (it may have been disconnected)"),
-            Error::NotFound => write!(f, "Entity not found"),
-            Error::Busy => write!(f, "Resource busy"),
-            Error::Timeout => write!(f, "Operation timed out"),
-            Error::Overflow => write!(f, "Overflow"),
-            Error::Pipe => write!(f, "Pipe error"),
-            Error::Interrupted => write!(f, "System call interrupted (perhaps due to signal)"),
-            Error::NoMem => write!(f, "Insufficient memory"),
-            Error::NotSupported => write!(
-                f,
-                "Operation not supported or unimplemented on this platform"
-            ),
-            Error::Other => write!(f, "Other error"),
-        }
-    }
-}
+        use self::Error::*;
 
-impl From<bindings::libusb_error> for Error {
-    fn from(e: bindings::libusb_error) -> Self {
-        match e {
-            bindings::LIBUSB_ERROR_IO => Error::IO,
-            bindings::LIBUSB_ERROR_INVALID_PARAM => Error::InvalidParam,
-            bindings::LIBUSB_ERROR_ACCESS => Error::Access,
-            bindings::LIBUSB_ERROR_NO_DEVICE => Error::NoDevice,
-            bindings::LIBUSB_ERROR_NOT_FOUND => Error::NotFound,
-            bindings::LIBUSB_ERROR_BUSY => Error::Busy,
-            bindings::LIBUSB_ERROR_TIMEOUT => Error::Timeout,
-            bindings::LIBUSB_ERROR_OVERFLOW => Error::Overflow,
-            bindings::LIBUSB_ERROR_PIPE => Error::Pipe,
-            bindings::LIBUSB_ERROR_INTERRUPTED => Error::Interrupted,
-            bindings::LIBUSB_ERROR_NO_MEM => Error::NoMem,
-            bindings::LIBUSB_ERROR_NOT_SUPPORTED => Error::NotSupported,
-            bindings::LIBUSB_ERROR_OTHER => Error::Other,
-            // All possible errors are defined above, other values mean success,
-            // see libusb_get_device_list for example.
-            _ => Error::Success(e),
+        #[sorted]
+        match self {
+            DescriptorParse => write!(f, "parsing descriptors failed"),
+            DescriptorRead(e) => write!(f, "reading descriptors from device failed: {}", e),
+            FdCloneFailed(e) => write!(f, "File::try_clone() failed: {}", e),
+            InvalidActualLength(e) => write!(f, "invalid actual_length in URB: {}", e),
+            InvalidBufferLength(e) => write!(f, "invalid transfer buffer length: {}", e),
+            IoctlFailed(nr, e) => write!(f, "USB ioctl 0x{:x} failed: {}", nr, e),
+            NoDevice => write!(f, "Device has been removed"),
+            NoSuchDescriptor => write!(f, "Requested descriptor not found"),
+            RcGetMutFailed => write!(f, "Rc::get_mut failed"),
+            RcUnwrapFailed => write!(f, "Rc::try_unwrap failed"),
+            TransferAlreadyCompleted => write!(f, "attempted to cancel already-completed transfer"),
         }
     }
 }
 
 pub type Result<T> = std::result::Result<T, Error>;
 
-#[macro_export]
-macro_rules! try_libusb {
-    ($x:expr) => {
-        match Error::from($x as i32) {
-            Error::Success(e) => e,
-            err => return Err(err),
-        }
-    };
-}
+impl std::error::Error for Error {}