use crate::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReasonCode {
Success,
NormalDisconnection,
GrantedQos0,
GrantedQos1,
GrantedQos2,
DisconnectWithWillMessage,
NoMatchingSubscribers,
NoSubscriptionExisted,
ContinueAuthentication,
ReAuthenticate,
UnspecifiedError,
MalformedPacket,
ProtocolError,
ImplementationSpecificError,
UnsupportedProtocolVersion,
ClientIdentifierNotValid,
BadUserNameOrPassword,
NotAuthorized,
ServerUnavailable,
ServerBusy,
Banned,
ServerShuttingDown,
BadAuthenticationMethod,
KeepAliveTimeout,
SessionTakenOver,
TopicFilterInvalid,
TopicNameInvalid,
PacketIdentifierInUse,
PacketIdentifierNotFound,
ReceiveMaximumExceeded,
TopicAliasInvalid,
PacketTooLarge,
MessageRateTooHigh,
QuotaExceeded,
AdministrativeAction,
PayloadFormatInvalid,
RetainNotSupported,
QosNotSupported,
UseAnotherServer,
ServerMoved,
SharedSubscriptionsNotSupported,
ConnectionRateExceeded,
MaximumConnectTime,
SubscriptionIdentifiersNotSupported,
WildcardSubscriptionsNotSupported,
}
impl From<ReasonCode> for u8 {
fn from(value: ReasonCode) -> Self {
match value {
ReasonCode::Success => 0,
ReasonCode::NormalDisconnection => 0,
ReasonCode::GrantedQos0 => 0,
ReasonCode::GrantedQos1 => 1,
ReasonCode::GrantedQos2 => 2,
ReasonCode::DisconnectWithWillMessage => 4,
ReasonCode::NoMatchingSubscribers => 16,
ReasonCode::NoSubscriptionExisted => 17,
ReasonCode::ContinueAuthentication => 24,
ReasonCode::ReAuthenticate => 25,
ReasonCode::UnspecifiedError => 128,
ReasonCode::MalformedPacket => 129,
ReasonCode::ProtocolError => 130,
ReasonCode::ImplementationSpecificError => 131,
ReasonCode::UnsupportedProtocolVersion => 132,
ReasonCode::ClientIdentifierNotValid => 133,
ReasonCode::BadUserNameOrPassword => 134,
ReasonCode::NotAuthorized => 135,
ReasonCode::ServerUnavailable => 136,
ReasonCode::ServerBusy => 137,
ReasonCode::Banned => 138,
ReasonCode::ServerShuttingDown => 139,
ReasonCode::BadAuthenticationMethod => 140,
ReasonCode::KeepAliveTimeout => 141,
ReasonCode::SessionTakenOver => 142,
ReasonCode::TopicFilterInvalid => 143,
ReasonCode::TopicNameInvalid => 144,
ReasonCode::PacketIdentifierInUse => 145,
ReasonCode::PacketIdentifierNotFound => 146,
ReasonCode::ReceiveMaximumExceeded => 147,
ReasonCode::TopicAliasInvalid => 148,
ReasonCode::PacketTooLarge => 149,
ReasonCode::MessageRateTooHigh => 150,
ReasonCode::QuotaExceeded => 151,
ReasonCode::AdministrativeAction => 152,
ReasonCode::PayloadFormatInvalid => 153,
ReasonCode::RetainNotSupported => 154,
ReasonCode::QosNotSupported => 155,
ReasonCode::UseAnotherServer => 156,
ReasonCode::ServerMoved => 157,
ReasonCode::SharedSubscriptionsNotSupported => 158,
ReasonCode::ConnectionRateExceeded => 159,
ReasonCode::MaximumConnectTime => 160,
ReasonCode::SubscriptionIdentifiersNotSupported => 161,
ReasonCode::WildcardSubscriptionsNotSupported => 162,
}
}
}
impl TryFrom<u8> for ReasonCode {
type Error = Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
let code = match value {
0 => Self::Success,
1 => Self::GrantedQos1,
2 => Self::GrantedQos2,
4 => Self::DisconnectWithWillMessage,
16 => Self::NoMatchingSubscribers,
17 => Self::NoSubscriptionExisted,
24 => Self::ContinueAuthentication,
25 => Self::ReAuthenticate,
128 => Self::UnspecifiedError,
129 => Self::MalformedPacket,
130 => Self::ProtocolError,
131 => Self::ImplementationSpecificError,
132 => Self::UnsupportedProtocolVersion,
133 => Self::ClientIdentifierNotValid,
134 => Self::BadUserNameOrPassword,
135 => Self::NotAuthorized,
136 => Self::ServerUnavailable,
137 => Self::ServerBusy,
138 => Self::Banned,
139 => Self::ServerShuttingDown,
140 => Self::BadAuthenticationMethod,
141 => Self::KeepAliveTimeout,
142 => Self::SessionTakenOver,
143 => Self::TopicFilterInvalid,
144 => Self::TopicNameInvalid,
145 => Self::PacketIdentifierInUse,
146 => Self::PacketIdentifierNotFound,
147 => Self::ReceiveMaximumExceeded,
148 => Self::TopicAliasInvalid,
149 => Self::PacketTooLarge,
150 => Self::MessageRateTooHigh,
151 => Self::QuotaExceeded,
152 => Self::AdministrativeAction,
153 => Self::PayloadFormatInvalid,
154 => Self::RetainNotSupported,
155 => Self::QosNotSupported,
156 => Self::UseAnotherServer,
157 => Self::ServerMoved,
158 => Self::SharedSubscriptionsNotSupported,
159 => Self::ConnectionRateExceeded,
160 => Self::MaximumConnectTime,
161 => Self::SubscriptionIdentifiersNotSupported,
162 => Self::WildcardSubscriptionsNotSupported,
n => return Err(Error::InvalidReasonCode(n)),
};
Ok(code)
}
}
impl Display for ReasonCode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match *self {
ReasonCode::Success => write!(f, "Success"),
ReasonCode::NormalDisconnection => write!(f, "Normal disconnection"),
ReasonCode::GrantedQos0 => write!(f, "Granted QoS 0"),
ReasonCode::GrantedQos1 => write!(f, "Granted QoS 1"),
ReasonCode::GrantedQos2 => write!(f, "Granted QoS 2"),
ReasonCode::DisconnectWithWillMessage => write!(f, "Disconnect with Will Message"),
ReasonCode::NoMatchingSubscribers => write!(f, "No matching subscribers"),
ReasonCode::NoSubscriptionExisted => write!(f, "No subscription existed"),
ReasonCode::ContinueAuthentication => write!(f, "Continue authentication"),
ReasonCode::ReAuthenticate => write!(f, "Re authenticate"),
ReasonCode::UnspecifiedError => write!(f, "Unspecified error"),
ReasonCode::MalformedPacket => write!(f, "Malformed Packet"),
ReasonCode::ProtocolError => write!(f, "Protocol Error"),
ReasonCode::ImplementationSpecificError => write!(f, "Implementation specific error"),
ReasonCode::UnsupportedProtocolVersion => write!(f, "Unsupported Protocol Version"),
ReasonCode::ClientIdentifierNotValid => write!(f, "Client Identifier not valid"),
ReasonCode::BadUserNameOrPassword => write!(f, "Bad User Name or Password"),
ReasonCode::NotAuthorized => write!(f, "Not authorized"),
ReasonCode::ServerUnavailable => write!(f, "Server unavailable"),
ReasonCode::ServerBusy => write!(f, "Server busy"),
ReasonCode::Banned => write!(f, "Banned"),
ReasonCode::ServerShuttingDown => write!(f, "Server shutting down"),
ReasonCode::BadAuthenticationMethod => write!(f, "Bad authentication method"),
ReasonCode::KeepAliveTimeout => write!(f, "Keep Alive timeout"),
ReasonCode::SessionTakenOver => write!(f, "Session taken over"),
ReasonCode::TopicFilterInvalid => write!(f, "Topic Filter invalid"),
ReasonCode::TopicNameInvalid => write!(f, "Topic Name invalid"),
ReasonCode::PacketIdentifierInUse => write!(f, "Packet Identifier in use"),
ReasonCode::PacketIdentifierNotFound => write!(f, "Packet Identifier not found"),
ReasonCode::ReceiveMaximumExceeded => write!(f, "Receive Maximum exceeded"),
ReasonCode::TopicAliasInvalid => write!(f, "Topic Alias invalid"),
ReasonCode::PacketTooLarge => write!(f, "Packet too large"),
ReasonCode::MessageRateTooHigh => write!(f, "Message rate too high"),
ReasonCode::QuotaExceeded => write!(f, "Quota exceeded"),
ReasonCode::AdministrativeAction => write!(f, "Administrative action"),
ReasonCode::PayloadFormatInvalid => write!(f, "Payload format invalid"),
ReasonCode::RetainNotSupported => write!(f, "Retain not supported"),
ReasonCode::QosNotSupported => write!(f, "QoS not supported"),
ReasonCode::UseAnotherServer => write!(f, "Use another server"),
ReasonCode::ServerMoved => write!(f, "Server moved"),
ReasonCode::SharedSubscriptionsNotSupported => {
write!(f, "Shared Subscriptions not supported")
}
ReasonCode::ConnectionRateExceeded => write!(f, "Connection rate exceeded"),
ReasonCode::MaximumConnectTime => write!(f, "Maximum connect time"),
ReasonCode::SubscriptionIdentifiersNotSupported => {
write!(f, "Subscription Identifiers not supported")
}
ReasonCode::WildcardSubscriptionsNotSupported => {
write!(f, "Wildcard Subscriptions not supported")
}
}
}
}