use crate::{
eio::{Read, Write},
io::{
err::{ReadError, WriteError},
read::Readable,
write::Writable,
},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum ReasonCode {
Success = 0x00,
GrantedQoS1 = 0x01,
GrantedQoS2 = 0x02,
DisconnectWithWillMessage = 0x04,
NoMatchingSubscribers = 0x10,
NoSubscriptionExisted = 0x11,
ContinueAuthentication = 0x18,
ReAuthenticate = 0x19,
#[default]
UnspecifiedError = 0x80,
MalformedPacket = 0x81,
ProtocolError = 0x82,
ImplementationSpecificError = 0x83,
UnsupportedProtocolVersion = 0x84,
ClientIdentifierNotValid = 0x85,
BadUserNameOrPassword = 0x86,
NotAuthorized = 0x87,
ServerUnavailable = 0x88,
ServerBusy = 0x89,
Banned = 0x8A,
ServerShuttingDown = 0x8B,
BadAuthenticationMethod = 0x8C,
KeepAliveTimeout = 0x8D,
SessionTakenOver = 0x8E,
TopicFilterInvalid = 0x8F,
TopicNameInvalid = 0x90,
PacketIdentifierInUse = 0x91,
PacketIdentifierNotFound = 0x92,
ReceiveMaximumExceeded = 0x93,
TopicAliasInvalid = 0x94,
PacketTooLarge = 0x95,
MessageRateTooHigh = 0x96,
QuotaExceeded = 0x97,
AdministrativeAction = 0x98,
PayloadFormatInvalid = 0x99,
RetainNotSupported = 0x9A,
QoSNotSupported = 0x9B,
UseAnotherServer = 0x9C,
ServerMoved = 0x9D,
SharedSubscriptionsNotSupported = 0x9E,
ConnectionRateExceeded = 0x9F,
MaximumConnectTime = 0xA0,
SubscriptionIdentifiersNotSupported = 0xA1,
WildcardSubscriptionsNotSupported = 0xA2,
}
impl ReasonCode {
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
#[must_use]
pub const fn is_success(&self) -> bool {
self.value() < 0x80
}
#[must_use]
pub const fn is_erroneous(&self) -> bool {
self.value() >= 0x80
}
}
impl<R: Read> Readable<R> for ReasonCode {
async fn read(net: &mut R) -> Result<Self, ReadError<R::Error>> {
let value = u8::read(net).await?;
Ok(match value {
0x00 => Self::Success, 0x01 => Self::GrantedQoS1,
0x02 => Self::GrantedQoS2,
0x04 => Self::DisconnectWithWillMessage,
0x10 => Self::NoMatchingSubscribers,
0x11 => Self::NoSubscriptionExisted,
0x18 => Self::ContinueAuthentication,
0x19 => Self::ReAuthenticate,
0x80 => Self::UnspecifiedError,
0x81 => Self::MalformedPacket,
0x82 => Self::ProtocolError,
0x83 => Self::ImplementationSpecificError,
0x84 => Self::UnsupportedProtocolVersion,
0x85 => Self::ClientIdentifierNotValid,
0x86 => Self::BadUserNameOrPassword,
0x87 => Self::NotAuthorized,
0x88 => Self::ServerUnavailable,
0x89 => Self::ServerBusy,
0x8A => Self::Banned,
0x8B => Self::ServerShuttingDown,
0x8C => Self::BadAuthenticationMethod,
0x8D => Self::KeepAliveTimeout,
0x8E => Self::SessionTakenOver,
0x8F => Self::TopicFilterInvalid,
0x90 => Self::TopicNameInvalid,
0x91 => Self::PacketIdentifierInUse,
0x92 => Self::PacketIdentifierNotFound,
0x93 => Self::ReceiveMaximumExceeded,
0x94 => Self::TopicAliasInvalid,
0x95 => Self::PacketTooLarge,
0x96 => Self::MessageRateTooHigh,
0x97 => Self::QuotaExceeded,
0x98 => Self::AdministrativeAction,
0x99 => Self::PayloadFormatInvalid,
0x9A => Self::RetainNotSupported,
0x9B => Self::QoSNotSupported,
0x9C => Self::UseAnotherServer,
0x9D => Self::ServerMoved,
0x9E => Self::SharedSubscriptionsNotSupported,
0x9F => Self::ConnectionRateExceeded,
0xA0 => Self::MaximumConnectTime,
0xA1 => Self::SubscriptionIdentifiersNotSupported,
0xA2 => Self::WildcardSubscriptionsNotSupported,
_ => return Err(ReadError::ProtocolError),
})
}
}
impl Writable for ReasonCode {
fn written_len(&self) -> usize {
1
}
async fn write<W: Write>(&self, write: &mut W) -> Result<(), WriteError<W::Error>> {
self.value().write(write).await
}
}