diff options
| author | diogo464 <[email protected]> | 2025-12-09 22:30:42 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-09 22:30:42 +0000 |
| commit | a5845673cf052b606f722be10d48c5d963958050 (patch) | |
| tree | e21bf5848163d07fce4bf8e3d7474bfeed5d1aff /src/mqtt/connect_code.rs | |
| parent | 6bb6d358f39c31b5486621b49da463f97226fea5 (diff) | |
moved embedded-mqtt crate to a module
Diffstat (limited to 'src/mqtt/connect_code.rs')
| -rw-r--r-- | src/mqtt/connect_code.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/mqtt/connect_code.rs b/src/mqtt/connect_code.rs new file mode 100644 index 0000000..570ce0f --- /dev/null +++ b/src/mqtt/connect_code.rs | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | use super::protocol; | ||
| 2 | |||
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum ConnectCode { | ||
| 5 | ConnectionAccepted, | ||
| 6 | UnacceptableProtocolVersion, | ||
| 7 | IdentifierRejected, | ||
| 8 | ServerUnavailable, | ||
| 9 | BadUsernamePassword, | ||
| 10 | NotAuthorized, | ||
| 11 | Unknown(u8), | ||
| 12 | } | ||
| 13 | |||
| 14 | impl core::fmt::Display for ConnectCode { | ||
| 15 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 16 | match self { | ||
| 17 | ConnectCode::ConnectionAccepted => write!(f, "Connection Accepted"), | ||
| 18 | ConnectCode::UnacceptableProtocolVersion => write!(f, "Unacceptable Protocol Version"), | ||
| 19 | ConnectCode::IdentifierRejected => write!(f, "Identifier Rejected"), | ||
| 20 | ConnectCode::ServerUnavailable => write!(f, "Server Unavailable"), | ||
| 21 | ConnectCode::BadUsernamePassword => write!(f, "Bad Username or Password"), | ||
| 22 | ConnectCode::NotAuthorized => write!(f, "Not Authorized"), | ||
| 23 | ConnectCode::Unknown(code) => write!(f, "Unknown({})", code), | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | impl From<u8> for ConnectCode { | ||
| 29 | fn from(value: u8) -> Self { | ||
| 30 | match value { | ||
| 31 | protocol::CONNACK_CODE_ACCEPTED => ConnectCode::ConnectionAccepted, | ||
| 32 | protocol::CONNACK_CODE_UNACCEPTABLE_PROTOCOL_VERSION => { | ||
| 33 | ConnectCode::UnacceptableProtocolVersion | ||
| 34 | } | ||
| 35 | protocol::CONNACK_CODE_IDENTIFIER_REJECTED => ConnectCode::IdentifierRejected, | ||
| 36 | protocol::CONNACK_CODE_SERVER_UNAVAILABLE => ConnectCode::ServerUnavailable, | ||
| 37 | protocol::CONNACK_CODE_BAD_USERNAME_PASSWORD => ConnectCode::BadUsernamePassword, | ||
| 38 | protocol::CONNACK_CODE_NOT_AUTHORIZED => ConnectCode::NotAuthorized, | ||
| 39 | code => ConnectCode::Unknown(code), | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | impl From<ConnectCode> for u8 { | ||
| 45 | fn from(value: ConnectCode) -> Self { | ||
| 46 | match value { | ||
| 47 | ConnectCode::ConnectionAccepted => protocol::CONNACK_CODE_ACCEPTED, | ||
| 48 | ConnectCode::UnacceptableProtocolVersion => { | ||
| 49 | protocol::CONNACK_CODE_UNACCEPTABLE_PROTOCOL_VERSION | ||
| 50 | } | ||
| 51 | ConnectCode::IdentifierRejected => protocol::CONNACK_CODE_IDENTIFIER_REJECTED, | ||
| 52 | ConnectCode::ServerUnavailable => protocol::CONNACK_CODE_SERVER_UNAVAILABLE, | ||
| 53 | ConnectCode::BadUsernamePassword => protocol::CONNACK_CODE_BAD_USERNAME_PASSWORD, | ||
| 54 | ConnectCode::NotAuthorized => protocol::CONNACK_CODE_NOT_AUTHORIZED, | ||
| 55 | ConnectCode::Unknown(code) => code, | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
