1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::error;
use std::fmt;
use std::io;
use std::str::{ Utf8Error};
use std::convert::From;
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum ErrorKind {
QosError,
TypeError,
PersistenceError,
General,
IoError,
}
pub struct MqttError {
repr: ErrorRepr,
}
#[derive(Debug)]
enum ErrorRepr {
WithDescription(ErrorKind, i32, &'static str),
WithDescriptionAndDetail(ErrorKind, i32, &'static str, String),
IoError(io::Error),
}
impl From<io::Error> for MqttError {
fn from(err: io::Error) -> MqttError {
MqttError { repr: ErrorRepr::IoError(err) }
}
}
impl From<Utf8Error> for MqttError {
fn from(_: Utf8Error) -> MqttError {
MqttError { repr: ErrorRepr::WithDescription(ErrorKind::TypeError, -1, "Invalid UTF-8") }
}
}
impl From<(ErrorKind, &'static str)> for MqttError {
fn from((kind, desc): (ErrorKind, &'static str)) -> MqttError {
MqttError { repr: ErrorRepr::WithDescription(kind, -1, desc) }
}
}
impl From<(ErrorKind, i32, &'static str)> for MqttError {
fn from((kind, err, desc): (ErrorKind, i32, &'static str)) -> MqttError {
MqttError { repr: ErrorRepr::WithDescription(kind, err, desc) }
}
}
impl From<(ErrorKind, &'static str, String)> for MqttError {
fn from((kind, desc, detail): (ErrorKind, &'static str, String)) -> MqttError {
MqttError { repr: ErrorRepr::WithDescriptionAndDetail(kind, -1, desc, detail) }
}
}
impl From<(ErrorKind, i32, &'static str, String)> for MqttError {
fn from((kind, err, desc, detail): (ErrorKind, i32, &'static str, String)) -> MqttError {
MqttError { repr: ErrorRepr::WithDescriptionAndDetail(kind, err, desc, detail) }
}
}
impl error::Error for MqttError {
fn description(&self) -> &str {
match self.repr {
ErrorRepr::WithDescription(_, _, desc) => desc,
ErrorRepr::WithDescriptionAndDetail(_, _, desc, _) => desc,
ErrorRepr::IoError(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&error::Error> {
match self.repr {
ErrorRepr::IoError(ref err) => Some(err as &error::Error),
_ => None,
}
}
}
impl fmt::Display for MqttError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.repr {
ErrorRepr::WithDescription(_, _err, desc) => desc.fmt(f),
ErrorRepr::WithDescriptionAndDetail(_, _err, desc, ref detail) => {
desc.fmt(f)?;
f.write_str(": ")?;
detail.fmt(f)
}
ErrorRepr::IoError(ref err) => err.fmt(f),
}
}
}
impl fmt::Debug for MqttError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
fmt::Display::fmt(self, f)
}
}
pub const PERSISTENCE_ERROR: MqttError = MqttError {
repr: ErrorRepr::WithDescription(ErrorKind::PersistenceError, -2, "Persistence Error"),
};
pub type MqttResult<T> = Result<T, MqttError>;