Skip to main content

libusb_wishbone_tool/
error.rs

1use std::fmt;
2use std::error::Error as StdError;
3use std::result::Result as StdResult;
4
5use libc::c_int;
6use libusb::*;
7
8/// A result of a function that may return a `Error`.
9pub type Result<T> = StdResult<T, Error>;
10
11
12/// Errors returned by the `libusb` library.
13#[derive(Debug, PartialEq)]
14pub enum Error {
15    /// Success (no error).
16    Success,
17
18    /// Input/output error.
19    Io,
20
21    /// Invalid parameter.
22    InvalidParam,
23
24    /// Access denied (insufficient permissions).
25    Access,
26
27    /// No such device (it may have been disconnected).
28    NoDevice,
29
30    /// Entity not found.
31    NotFound,
32
33    /// Resource busy.
34    Busy,
35
36    /// Operation timed out.
37    Timeout,
38
39    /// Overflow.
40    Overflow,
41
42    /// Pipe error.
43    Pipe,
44
45    /// System call interrupted (perhaps due to signal).
46    Interrupted,
47
48    /// Insufficient memory.
49    NoMem,
50
51    /// Operation not supported or unimplemented on this platform.
52    NotSupported,
53
54    /// Other error.
55    Other
56}
57
58impl Error {
59    /// Returns a description of an error suitable for display to an end user.
60    pub fn strerror(&self) -> &'static str {
61        match *self {
62            Error::Success      => "Success",
63            Error::Io           => "Input/Output Error",
64            Error::InvalidParam => "Invalid parameter",
65            Error::Access       => "Access denied (insufficient permissions)",
66            Error::NoDevice     => "No such device (it may have been disconnected)",
67            Error::NotFound     => "Entity not found",
68            Error::Busy         => "Resource busy",
69            Error::Timeout      => "Operation timed out",
70            Error::Overflow     => "Overflow",
71            Error::Pipe         => "Pipe error",
72            Error::Interrupted  => "System call interrupted (perhaps due to signal)",
73            Error::NoMem        => "Insufficient memory",
74            Error::NotSupported => "Operation not supported or unimplemented on this platform",
75            Error::Other        => "Other error",
76        }
77    }
78}
79
80impl fmt::Display for Error {
81    fn fmt(&self, fmt: &mut fmt::Formatter) -> StdResult<(), fmt::Error> {
82        fmt.write_str(self.strerror())
83    }
84}
85
86impl StdError for Error {
87    fn description(&self) -> &'static str {
88        self.strerror()
89    }
90}
91
92
93#[doc(hidden)]
94pub fn from_libusb(err: c_int) -> Error {
95    match err {
96        LIBUSB_SUCCESS             => Error::Success,
97        LIBUSB_ERROR_IO            => Error::Io,
98        LIBUSB_ERROR_INVALID_PARAM => Error::InvalidParam,
99        LIBUSB_ERROR_ACCESS        => Error::Access,
100        LIBUSB_ERROR_NO_DEVICE     => Error::NoDevice,
101        LIBUSB_ERROR_NOT_FOUND     => Error::NotFound,
102        LIBUSB_ERROR_BUSY          => Error::Busy,
103        LIBUSB_ERROR_TIMEOUT       => Error::Timeout,
104        LIBUSB_ERROR_OVERFLOW      => Error::Overflow,
105        LIBUSB_ERROR_PIPE          => Error::Pipe,
106        LIBUSB_ERROR_INTERRUPTED   => Error::Interrupted,
107        LIBUSB_ERROR_NO_MEM        => Error::NoMem,
108        LIBUSB_ERROR_NOT_SUPPORTED => Error::NotSupported,
109        _                               => Error::Other,
110    }
111}
112
113#[doc(hidden)]
114macro_rules! try_unsafe {
115    ($x:expr) => {
116        match unsafe { $x } {
117            0 => (),
118            err => return Err($crate::error::from_libusb(err)),
119        }
120    }
121}