use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DaemonCommand {
Ping,
Show,
Hide,
Toggle,
Quit,
}
impl DaemonCommand {
const PING: u8 = b'P';
const SHOW: u8 = b'S';
const HIDE: u8 = b'H';
const TOGGLE: u8 = b'T';
const QUIT: u8 = b'Q';
pub const fn encode(self) -> u8 {
match self {
Self::Ping => Self::PING,
Self::Show => Self::SHOW,
Self::Hide => Self::HIDE,
Self::Toggle => Self::TOGGLE,
Self::Quit => Self::QUIT,
}
}
pub const fn decode(byte: u8) -> Option<Self> {
match byte {
Self::PING => Some(Self::Ping),
Self::SHOW => Some(Self::Show),
Self::HIDE => Some(Self::Hide),
Self::TOGGLE => Some(Self::Toggle),
Self::QUIT => Some(Self::Quit),
_ => None,
}
}
pub const fn name(self) -> &'static str {
match self {
Self::Ping => "ping",
Self::Show => "show",
Self::Hide => "hide",
Self::Toggle => "toggle",
Self::Quit => "quit",
}
}
}
impl fmt::Display for DaemonCommand {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.name())
}
}