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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::{error, fmt, io};
use derive_more::{Display, From};
use ntex::util::Either;
#[derive(Debug, Display)]
pub enum MqttError<E> {
#[display(fmt = "Service error")]
Service(E),
#[display(fmt = "Mqtt protocol error: {}", _0)]
Protocol(ProtocolError),
HandshakeTimeout,
#[display(fmt = "Peer is disconnected, error: {:?}", _0)]
Disconnected(Option<io::Error>),
#[display(fmt = "Server error: {}", _0)]
ServerError(&'static str),
}
impl<E: fmt::Debug> error::Error for MqttError<E> {}
#[derive(Debug, Display, From)]
pub enum ProtocolError {
#[display(fmt = "Decode error: {:?}", _0)]
Decode(DecodeError),
#[display(fmt = "Encode error: {:?}", _0)]
Encode(EncodeError),
#[display(fmt = "Unexpected packet {:?}, {}", _0, _1)]
Unexpected(u8, &'static str),
#[display(fmt = "Packet id of publish ack packet does not match of send publish packet")]
PacketIdMismatch,
#[display(fmt = "Topic alias is greater than max topic alias")]
MaxTopicAlias,
#[display(fmt = "Number of in-flight messages exceeded")]
ReceiveMaximumExceeded,
#[display(fmt = "Unknown topic alias")]
UnknownTopicAlias,
#[display(fmt = "Keep alive timeout")]
KeepAliveTimeout,
}
impl error::Error for ProtocolError {}
impl<E> From<ProtocolError> for MqttError<E> {
fn from(err: ProtocolError) -> Self {
MqttError::Protocol(err)
}
}
impl<E> From<io::Error> for MqttError<E> {
fn from(err: io::Error) -> Self {
MqttError::Disconnected(Some(err))
}
}
impl<E> From<Either<io::Error, io::Error>> for MqttError<E> {
fn from(err: Either<io::Error, io::Error>) -> Self {
MqttError::Disconnected(Some(err.into_inner()))
}
}
impl<E> From<Either<DecodeError, io::Error>> for MqttError<E> {
fn from(err: Either<DecodeError, io::Error>) -> Self {
match err {
Either::Left(err) => MqttError::Protocol(ProtocolError::Decode(err)),
Either::Right(err) => MqttError::Disconnected(Some(err)),
}
}
}
impl<E> From<Either<EncodeError, io::Error>> for MqttError<E> {
fn from(err: Either<EncodeError, io::Error>) -> Self {
match err {
Either::Left(err) => MqttError::Protocol(ProtocolError::Encode(err)),
Either::Right(err) => MqttError::Disconnected(Some(err)),
}
}
}
#[derive(Debug, Display, From)]
pub enum DecodeError {
InvalidProtocol,
InvalidLength,
MalformedPacket,
UnsupportedProtocolLevel,
ConnectReservedFlagSet,
ConnAckReservedFlagSet,
InvalidClientId,
UnsupportedPacketType,
PacketIdRequired,
MaxSizeExceeded,
Utf8Error,
}
impl error::Error for DecodeError {}
#[derive(Copy, Clone, Debug, Display, PartialEq, Eq, Hash)]
pub enum EncodeError {
InvalidLength,
MalformedPacket,
PacketIdRequired,
UnsupportedVersion,
}
impl error::Error for EncodeError {}
impl PartialEq for DecodeError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(DecodeError::InvalidProtocol, DecodeError::InvalidProtocol) => true,
(DecodeError::InvalidLength, DecodeError::InvalidLength) => true,
(DecodeError::UnsupportedProtocolLevel, DecodeError::UnsupportedProtocolLevel) => {
true
}
(DecodeError::ConnectReservedFlagSet, DecodeError::ConnectReservedFlagSet) => true,
(DecodeError::ConnAckReservedFlagSet, DecodeError::ConnAckReservedFlagSet) => true,
(DecodeError::InvalidClientId, DecodeError::InvalidClientId) => true,
(DecodeError::UnsupportedPacketType, DecodeError::UnsupportedPacketType) => true,
(DecodeError::PacketIdRequired, DecodeError::PacketIdRequired) => true,
(DecodeError::MaxSizeExceeded, DecodeError::MaxSizeExceeded) => true,
(DecodeError::MalformedPacket, DecodeError::MalformedPacket) => true,
(DecodeError::Utf8Error, DecodeError::Utf8Error) => true,
_ => false,
}
}
}
#[derive(Debug, Display, PartialEq)]
pub enum SendPacketError {
Encode(EncodeError),
#[display(fmt = "Provided packet id is in use")]
PacketIdInUse(u16),
#[display(fmt = "Peer disconnected")]
Disconnected,
}
impl error::Error for SendPacketError {}