use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Command {
Sync = 0x434e_5953,
Connect = 0x4e58_4e43,
Auth = 0x4854_5541,
Open = 0x4e45_504f,
Okay = 0x5941_4b4f,
Close = 0x4553_4c43,
Write = 0x4554_5257,
StartTls = 0x534c_5453,
}
impl Command {
pub const fn code(self) -> u32 {
self as u32
}
pub const fn mnemonic(self) -> &'static str {
match self {
Self::Sync => "SYNC",
Self::Connect => "CNXN",
Self::Auth => "AUTH",
Self::Open => "OPEN",
Self::Okay => "OKAY",
Self::Close => "CLSE",
Self::Write => "WRTE",
Self::StartTls => "STLS",
}
}
pub const fn from_code(code: u32) -> Option<Self> {
Some(match code {
0x434e_5953 => Self::Sync,
0x4e58_4e43 => Self::Connect,
0x4854_5541 => Self::Auth,
0x4e45_504f => Self::Open,
0x5941_4b4f => Self::Okay,
0x4553_4c43 => Self::Close,
0x4554_5257 => Self::Write,
0x534c_5453 => Self::StartTls,
_ => return None,
})
}
pub const ALL: [Self; 8] = [
Self::Sync,
Self::Connect,
Self::Auth,
Self::Open,
Self::Okay,
Self::Close,
Self::Write,
Self::StartTls,
];
}
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.mnemonic())
}
}
#[cfg(test)]
mod tests {
use super::Command;
#[test]
fn mnemonic_bytes_match_codes() {
for cmd in Command::ALL {
let bytes: [u8; 4] = cmd
.mnemonic()
.as_bytes()
.try_into()
.unwrap_or_else(|_| unreachable!("mnemonics are four bytes"));
assert_eq!(u32::from_le_bytes(bytes), cmd.code(), "{cmd}");
assert_eq!(Command::from_code(cmd.code()), Some(cmd));
}
assert_eq!(Command::from_code(0), None);
}
}