btle/
error.rs

1//! Generic Error Trait. Similar to `std::error::Error`.
2
3/// Generic Error type. Similar to `std::error::Error` but supports `no_std`. If the `std` feature
4/// is enabled, `Error` will implement `std::error::Error`. Automatically implements `fmt::Display`
5/// by using the `Debug` implementation (`"{:?}"`).
6pub trait Error: core::fmt::Debug {
7    /// The lower-level source of this error, if any.
8    fn source(&self) -> Option<&(dyn Error + 'static)> {
9        None
10    }
11}
12
13impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
14    fn from(e: E) -> Self {
15        Box::new(e)
16    }
17}
18#[cfg(feature = "std")]
19impl std::error::Error for Box<dyn Error> {}
20impl core::fmt::Display for Box<dyn Error> {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        write!(f, "{:?}", self.as_ref())
23    }
24}
25pub struct StdError<E: Error + ?Sized>(pub E);
26impl<E: Error> From<E> for StdError<E> {
27    fn from(e: E) -> Self {
28        Self(e)
29    }
30}
31impl<E: Error> core::fmt::Debug for StdError<E> {
32    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
33        write!(f, "{:?}", self.0)
34    }
35}
36impl<T: Error> core::fmt::Display for StdError<T> {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
38        write!(f, "{:?}", self.0)
39    }
40}
41
42impl<E: Error> Error for StdError<E> {}
43#[cfg(feature = "std")]
44impl<E: Error> std::error::Error for StdError<E> {}
45#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
46pub enum IOError {
47    Unknown,
48    TimedOut,
49    NotFound,
50    OperationAborted,
51    InvalidArgument,
52    InvalidHandlePointer,
53    InvalidData,
54    AccessDenied,
55    OutOfMemory,
56    PermissionDenied,
57    Closed,
58    NotImplemented,
59    NotConnected,
60    Interrupted,
61    IllegalCall,
62    AlreadyExists,
63    Refused,
64    Pipe,
65    Overflow,
66    Other,
67    Code(i32),
68}
69impl core::fmt::Display for IOError {
70    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71        write!(f, "{:?}", self)
72    }
73}
74impl Error for IOError {}
75
76#[cfg(feature = "std")]
77impl From<std::io::ErrorKind> for IOError {
78    fn from(e: std::io::ErrorKind) -> Self {
79        match e {
80            std::io::ErrorKind::NotFound => IOError::NotFound,
81            std::io::ErrorKind::PermissionDenied => IOError::NotFound,
82            std::io::ErrorKind::ConnectionRefused => IOError::Refused,
83            std::io::ErrorKind::ConnectionReset => IOError::NotConnected,
84            std::io::ErrorKind::ConnectionAborted => IOError::NotConnected,
85            std::io::ErrorKind::NotConnected => IOError::NotConnected,
86            std::io::ErrorKind::AddrInUse => IOError::AlreadyExists,
87            std::io::ErrorKind::AddrNotAvailable => IOError::Refused,
88            std::io::ErrorKind::BrokenPipe => IOError::Closed,
89            std::io::ErrorKind::AlreadyExists => IOError::AlreadyExists,
90            std::io::ErrorKind::WouldBlock => IOError::Other,
91            std::io::ErrorKind::InvalidInput => IOError::InvalidArgument,
92            std::io::ErrorKind::InvalidData => IOError::InvalidData,
93            std::io::ErrorKind::TimedOut => IOError::TimedOut,
94            std::io::ErrorKind::WriteZero => IOError::InvalidArgument,
95            std::io::ErrorKind::Interrupted => IOError::Interrupted,
96            std::io::ErrorKind::Other => IOError::Other,
97            std::io::ErrorKind::UnexpectedEof => IOError::InvalidData,
98            _ => IOError::Other,
99        }
100    }
101}
102#[cfg(feature = "std")]
103impl From<&std::io::Error> for IOError {
104    fn from(e: &std::io::Error) -> Self {
105        e.kind().into()
106    }
107}
108
109#[cfg(feature = "std")]
110impl From<std::io::Error> for IOError {
111    fn from(e: std::io::Error) -> Self {
112        e.kind().into()
113    }
114}
115
116#[cfg(feature = "std")]
117impl std::error::Error for IOError {}