• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std;
6 use std::fmt;
7 
8 use crate::bindings;
9 
10 /// Error type for libusb.
11 pub enum Error {
12     Success(i32),
13     IO,
14     InvalidParam,
15     Access,
16     NoDevice,
17     NotFound,
18     Busy,
19     Timeout,
20     Overflow,
21     Pipe,
22     Interrupted,
23     NoMem,
24     NotSupported,
25     Other,
26 }
27 
28 impl fmt::Debug for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result29     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30         match *self {
31             Error::Success(_v) => write!(f, "Success (no error)"),
32             Error::IO => write!(f, "Input/output error"),
33             Error::InvalidParam => write!(f, "Invalid parameter"),
34             Error::Access => write!(f, "Access denied (insufficient permissions)"),
35             Error::NoDevice => write!(f, "No such device (it may have been disconnected)"),
36             Error::NotFound => write!(f, "Entity not found"),
37             Error::Busy => write!(f, "Resource busy"),
38             Error::Timeout => write!(f, "Operation timed out"),
39             Error::Overflow => write!(f, "Overflow"),
40             Error::Pipe => write!(f, "Pipe error"),
41             Error::Interrupted => write!(f, "System call interrupted (perhaps due to signal)"),
42             Error::NoMem => write!(f, "Insufficient memory"),
43             Error::NotSupported => write!(
44                 f,
45                 "Operation not supported or unimplemented on this platform"
46             ),
47             Error::Other => write!(f, "Other error"),
48         }
49     }
50 }
51 
52 impl From<bindings::libusb_error> for Error {
from(e: bindings::libusb_error) -> Self53     fn from(e: bindings::libusb_error) -> Self {
54         match e {
55             bindings::LIBUSB_ERROR_IO => Error::IO,
56             bindings::LIBUSB_ERROR_INVALID_PARAM => Error::InvalidParam,
57             bindings::LIBUSB_ERROR_ACCESS => Error::Access,
58             bindings::LIBUSB_ERROR_NO_DEVICE => Error::NoDevice,
59             bindings::LIBUSB_ERROR_NOT_FOUND => Error::NotFound,
60             bindings::LIBUSB_ERROR_BUSY => Error::Busy,
61             bindings::LIBUSB_ERROR_TIMEOUT => Error::Timeout,
62             bindings::LIBUSB_ERROR_OVERFLOW => Error::Overflow,
63             bindings::LIBUSB_ERROR_PIPE => Error::Pipe,
64             bindings::LIBUSB_ERROR_INTERRUPTED => Error::Interrupted,
65             bindings::LIBUSB_ERROR_NO_MEM => Error::NoMem,
66             bindings::LIBUSB_ERROR_NOT_SUPPORTED => Error::NotSupported,
67             bindings::LIBUSB_ERROR_OTHER => Error::Other,
68             // All possible errors are defined above, other values mean success,
69             // see libusb_get_device_list for example.
70             _ => Error::Success(e),
71         }
72     }
73 }
74 
75 pub type Result<T> = std::result::Result<T, Error>;
76 
77 #[macro_export]
78 macro_rules! try_libusb {
79     ($x:expr) => {
80         match Error::from($x as i32) {
81             Error::Success(e) => e,
82             err => return Err(err),
83         }
84     };
85 }
86