mqtt_async_client/
error.rs

1use std::{
2    convert::From,
3    fmt::{Debug, Display, Formatter, self},
4};
5
6/// Fallible result values returned by the library.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors returned by the library.
10#[derive(Debug)]
11pub enum Error {
12    /// The client is disconnected.
13    Disconnected,
14
15    /// An error represented by an implementation of std::error::Error.
16    StdError(Box<dyn std::error::Error + Send + Sync>),
17
18    /// An error represented as a String.
19    String(String),
20
21    #[doc(hidden)]
22    _NonExhaustive
23}
24
25impl Error {
26    /// Construct an error instance from an implementation of std::error::Error.
27    pub fn from_std_err<T: std::error::Error + Send + Sync + 'static>(e: T) -> Error {
28        Error::StdError(Box::new(e))
29    }
30}
31
32impl Display for Error {
33    fn fmt(&self, f: &mut Formatter) -> std::result::Result<(), fmt::Error> {
34        match self {
35            Error::Disconnected => write!(f, "Disconnected"),
36            Error::StdError(e) => write!(f, "{}", e),
37            Error::String(s) => write!(f, "{}", s),
38            Error::_NonExhaustive => panic!("Not reachable"),
39        }
40    }
41}
42
43impl std::error::Error for Error {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            Error::StdError(e) => Some(&**e),
47            _ => None,
48        }
49    }
50}
51
52impl From<String> for Error {
53    fn from(s: String) -> Error {
54        Error::String(s)
55    }
56}
57
58impl From<&str> for Error {
59    fn from(s: &str) -> Error {
60        Error::String(s.to_owned())
61    }
62}
63
64impl From<std::io::Error> for Error {
65    fn from(e: std::io::Error) -> Error {
66        Error::StdError(Box::new(e))
67    }
68}
69
70impl From<mqttrs::Error> for Error {
71    fn from(e: mqttrs::Error) -> Error {
72        Error::StdError(Box::new(e))
73    }
74}