1use super::common::*;
2#[cfg(feature = "serde_support")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, PartialEq, Clone)]
6#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
7pub struct ReasonCode {}
8
9pub trait MqttCode<T> {
10 fn from_byte(byte: u8) -> Res<T>;
11 fn to_byte(&self) -> u8;
12}
13
14#[derive(Debug, PartialEq, Clone)]
15#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
16pub enum SubscriptionReasonCode {
18 GrantedQoS0,
20 GrantedQoS1,
22 GrantedQoS2,
24 UnspecifiedError,
26 ImplementationSpecificError,
28 NotAuthorized,
30 TopicFilterInvalid,
32 PacketIdentifierInUse,
34 QuotaExceeded,
36 SharedSubscriptionsNotSupported,
38 SubscriptionIdentifiersNotSupported,
40 WildcardSubscriptionsNotSupported,
42}
43
44impl MqttCode<SubscriptionReasonCode> for SubscriptionReasonCode {
45 fn from_byte(byte: u8) -> Res<SubscriptionReasonCode> {
46 match byte {
47 0x00 => Ok(SubscriptionReasonCode::GrantedQoS0),
48 0x01 => Ok(SubscriptionReasonCode::GrantedQoS1),
49 0x02 => Ok(SubscriptionReasonCode::GrantedQoS2),
50 0x80 => Ok(SubscriptionReasonCode::UnspecifiedError),
51 0x83 => Ok(SubscriptionReasonCode::ImplementationSpecificError),
52 0x87 => Ok(SubscriptionReasonCode::NotAuthorized),
53 0x8F => Ok(SubscriptionReasonCode::TopicFilterInvalid),
54 0x91 => Ok(SubscriptionReasonCode::PacketIdentifierInUse),
55 0x97 => Ok(SubscriptionReasonCode::QuotaExceeded),
56 0x9E => Ok(SubscriptionReasonCode::SharedSubscriptionsNotSupported),
57 0xA1 => Ok(SubscriptionReasonCode::SubscriptionIdentifiersNotSupported),
58 0xA2 => Ok(SubscriptionReasonCode::WildcardSubscriptionsNotSupported),
59 _ => Err(format!("Invalid suback code {}", byte)),
61 }
62 }
63
64 fn to_byte(&self) -> u8 {
65 match self {
66 SubscriptionReasonCode::GrantedQoS0 => 0x00,
67 SubscriptionReasonCode::GrantedQoS1 => 0x01,
68 SubscriptionReasonCode::GrantedQoS2 => 0x02,
69 SubscriptionReasonCode::UnspecifiedError => 0x80,
70 SubscriptionReasonCode::ImplementationSpecificError => 0x83,
71 SubscriptionReasonCode::NotAuthorized => 0x87,
72 SubscriptionReasonCode::TopicFilterInvalid => 0x8F,
73 SubscriptionReasonCode::PacketIdentifierInUse => 0x91,
74 SubscriptionReasonCode::QuotaExceeded => 0x97,
75 SubscriptionReasonCode::SharedSubscriptionsNotSupported => 0x9E,
76 SubscriptionReasonCode::SubscriptionIdentifiersNotSupported => 0xA1,
77 SubscriptionReasonCode::WildcardSubscriptionsNotSupported => 0xA2,
78 }
79 }
80}
81
82#[derive(Debug, PartialEq, Clone)]
83#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
84pub enum DisconnectCode {
85 NormalDisconnection, DisconnectWithWillMessage, UnspecifiedError, MalformedPacket, ProtocolError, ImplementationSpecificError, NotAuthorized, ServerBusy, ServerShuttingDown, KeepAliveTimeout, SessionTakenVver, TopicFilterInvalid, TopicNameInvalid, ReceiveMaximumExceeded, TopicAliasInvalid, PacketTooLarge, MessageRateTooHigh, QuotaExceeded, AdministrativeAction, PayloadFormatInvalid, RetainNotSupported, QoSNotSupported, UseAnotherServer, ServerMoved, SharedSubscriptionsNotSupported, ConnectionRateExceeded, MaximumConnectTime, SubscriptionIdentifiersNotSupported, WildcardSubscriptionsNotSupported, }
115
116impl MqttCode<DisconnectCode> for DisconnectCode {
117 fn from_byte(code: u8) -> Res<DisconnectCode> {
118 Ok(match code {
119 0x00 => DisconnectCode::NormalDisconnection, 0x04 => DisconnectCode::DisconnectWithWillMessage, 0x80 => DisconnectCode::UnspecifiedError, 0x81 => DisconnectCode::MalformedPacket, 0x82 => DisconnectCode::ProtocolError, 0x83 => DisconnectCode::ImplementationSpecificError, 0x87 => DisconnectCode::NotAuthorized, 0x89 => DisconnectCode::ServerBusy, 0x8B => DisconnectCode::ServerShuttingDown, 0x8D => DisconnectCode::KeepAliveTimeout, 0x8E => DisconnectCode::SessionTakenVver, 0x8F => DisconnectCode::TopicFilterInvalid, 0x90 => DisconnectCode::TopicNameInvalid, 0x93 => DisconnectCode::ReceiveMaximumExceeded, 0x94 => DisconnectCode::TopicAliasInvalid, 0x95 => DisconnectCode::PacketTooLarge, 0x96 => DisconnectCode::MessageRateTooHigh, 0x97 => DisconnectCode::QuotaExceeded, 0x98 => DisconnectCode::AdministrativeAction, 0x99 => DisconnectCode::PayloadFormatInvalid, 0x9A => DisconnectCode::RetainNotSupported, 0x9B => DisconnectCode::QoSNotSupported, 0x9C => DisconnectCode::UseAnotherServer, 0x9D => DisconnectCode::ServerMoved, 0x9E => DisconnectCode::SharedSubscriptionsNotSupported, 0x9F => DisconnectCode::ConnectionRateExceeded, 0xA0 => DisconnectCode::MaximumConnectTime, 0xA1 => DisconnectCode::SubscriptionIdentifiersNotSupported, 0xA2 => DisconnectCode::WildcardSubscriptionsNotSupported, _ => return Err(format!("Invalid disconnect code {}", code)),
149 })
150 }
151
152 fn to_byte(&self) -> u8 {
153 match self {
154 DisconnectCode::NormalDisconnection => 0x00, DisconnectCode::DisconnectWithWillMessage => 0x04, DisconnectCode::UnspecifiedError => 0x80, DisconnectCode::MalformedPacket => 0x81, DisconnectCode::ProtocolError => 0x82, DisconnectCode::ImplementationSpecificError => 0x83, DisconnectCode::NotAuthorized => 0x87, DisconnectCode::ServerBusy => 0x89, DisconnectCode::ServerShuttingDown => 0x8B, DisconnectCode::KeepAliveTimeout => 0x8D, DisconnectCode::SessionTakenVver => 0x8E, DisconnectCode::TopicFilterInvalid => 0x8F, DisconnectCode::TopicNameInvalid => 0x90, DisconnectCode::ReceiveMaximumExceeded => 0x93, DisconnectCode::TopicAliasInvalid => 0x94, DisconnectCode::PacketTooLarge => 0x95, DisconnectCode::MessageRateTooHigh => 0x96, DisconnectCode::QuotaExceeded => 0x97, DisconnectCode::AdministrativeAction => 0x98, DisconnectCode::PayloadFormatInvalid => 0x99, DisconnectCode::RetainNotSupported => 0x9A, DisconnectCode::QoSNotSupported => 0x9B, DisconnectCode::UseAnotherServer => 0x9C, DisconnectCode::ServerMoved => 0x9D, DisconnectCode::SharedSubscriptionsNotSupported => 0x9E, DisconnectCode::ConnectionRateExceeded => 0x9F, DisconnectCode::MaximumConnectTime => 0xA0, DisconnectCode::SubscriptionIdentifiersNotSupported => 0xA1, DisconnectCode::WildcardSubscriptionsNotSupported => 0xA2, }
184 }
185}
186
187#[derive(Debug, PartialEq, Clone)]
188#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
189pub enum UnsubackCode {
190 Success,
191 NoSubscriptionExisted,
192 UnspecifiedError,
193 ImplementationSpecificError,
194 NotAuthorized,
195 TopicFilterInvalid,
196 PacketIdentifierInUse,
197}
198
199impl MqttCode<UnsubackCode> for UnsubackCode {
200 fn from_byte(code: u8) -> Res<UnsubackCode> {
201 let c = match code {
202 0x00 => UnsubackCode::Success, 0x11 => UnsubackCode::NoSubscriptionExisted, 0x80 => UnsubackCode::UnspecifiedError, 0x83 => UnsubackCode::ImplementationSpecificError, 0x87 => UnsubackCode::NotAuthorized, 0x8F => UnsubackCode::TopicFilterInvalid, 0x91 => UnsubackCode::PacketIdentifierInUse, _ => return Err(format!("Invalid unsuback code {}", code)),
210 };
211 Ok(c)
212 }
213
214 fn to_byte(&self) -> u8 {
215 match self {
216 UnsubackCode::Success => 0x00,
217 UnsubackCode::NoSubscriptionExisted => 0x11,
218 UnsubackCode::UnspecifiedError => 0x80,
219 UnsubackCode::ImplementationSpecificError => 0x83,
220 UnsubackCode::NotAuthorized => 0x87,
221 UnsubackCode::TopicFilterInvalid => 0x8F,
222 UnsubackCode::PacketIdentifierInUse => 0x91,
223 }
224 }
225}
226
227#[derive(Debug, PartialEq, Clone)]
228#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
229pub enum AuthCode {
230 Success, ContinueAuthentication, ReAuthenticate, }
234
235impl MqttCode<AuthCode> for AuthCode {
236 fn from_byte(byte: u8) -> Res<AuthCode> {
237 Ok(match byte {
238 0x00 => AuthCode::Success,
239 0x18 => AuthCode::ContinueAuthentication,
240 0x19 => AuthCode::ReAuthenticate,
241 _ => return Err(format!("Invalid auth code {}", byte)),
242 })
243 }
244
245 fn to_byte(&self) -> u8 {
246 match self {
247 AuthCode::Success => 0x00,
248 AuthCode::ContinueAuthentication => 0x18,
249 AuthCode::ReAuthenticate => 0x19,
250 }
251 }
252}
253
254#[derive(Debug, PartialEq, Clone)]
255#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
256pub enum PubcompPubrelCode {
258 Success, PacketIdentifierNotFound, }
261
262impl MqttCode<PubcompPubrelCode> for PubcompPubrelCode {
263 fn from_byte(byte: u8) -> Res<PubcompPubrelCode> {
264 Ok(match byte {
265 0x00 => PubcompPubrelCode::Success,
266 0x92 => PubcompPubrelCode::PacketIdentifierNotFound,
267 _ => return Err(format!("Invalid pubcomp/pubrel code {}", byte)),
268 })
269 }
270
271 fn to_byte(&self) -> u8 {
272 match self {
273 PubcompPubrelCode::Success => 0x00,
274 PubcompPubrelCode::PacketIdentifierNotFound => 0x92,
275 }
276 }
277}
278
279#[derive(Debug, PartialEq, Clone)]
280#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
281pub enum PubackPubrecCode {
283 Success,
284 NoMatchingSubscribers,
285 UnspecifiedError,
286 ImplementationSpecificError,
287 NotAuthorized,
288 TopicNameInvalid,
289 PacketIdentifierInUse,
290 QuotaExceeded,
291 PayloadFormatInvalid,
292}
293
294impl MqttCode<PubackPubrecCode> for PubackPubrecCode {
295 fn from_byte(byte: u8) -> Res<PubackPubrecCode> {
296 Ok(match byte {
297 0x00 => PubackPubrecCode::Success,
298 0x10 => PubackPubrecCode::NoMatchingSubscribers,
299 0x80 => PubackPubrecCode::UnspecifiedError,
300 0x83 => PubackPubrecCode::ImplementationSpecificError,
301 0x87 => PubackPubrecCode::NotAuthorized,
302 0x90 => PubackPubrecCode::TopicNameInvalid,
303 0x91 => PubackPubrecCode::PacketIdentifierInUse,
304 0x97 => PubackPubrecCode::QuotaExceeded,
305 0x99 => PubackPubrecCode::PayloadFormatInvalid,
306 _ => return Err(format!("Invalid puback/pubrec code {}", byte)),
307 })
308 }
309
310 fn to_byte(&self) -> u8 {
311 match self {
312 PubackPubrecCode::Success => 0x00,
313 PubackPubrecCode::NoMatchingSubscribers => 0x10,
314 PubackPubrecCode::UnspecifiedError => 0x80,
315 PubackPubrecCode::ImplementationSpecificError => 0x83,
316 PubackPubrecCode::NotAuthorized => 0x87,
317 PubackPubrecCode::TopicNameInvalid => 0x90,
318 PubackPubrecCode::PacketIdentifierInUse => 0x91,
319 PubackPubrecCode::QuotaExceeded => 0x97,
320 PubackPubrecCode::PayloadFormatInvalid => 0x99,
321 }
322 }
323}