#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Opcode {
Continuation = 0x0,
Text = 0x1,
Binary = 0x2,
Close = 0x8,
Ping = 0x9,
Pong = 0xA,
}
impl Opcode {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0x0 => Some(Self::Continuation),
0x1 => Some(Self::Text),
0x2 => Some(Self::Binary),
0x8 => Some(Self::Close),
0x9 => Some(Self::Ping),
0xA => Some(Self::Pong),
_ => None,
}
}
pub fn as_u8(self) -> u8 {
self as u8
}
pub fn is_control(self) -> bool {
matches!(self, Self::Close | Self::Ping | Self::Pong)
}
pub fn is_data(self) -> bool {
matches!(self, Self::Continuation | Self::Text | Self::Binary)
}
}
impl std::fmt::Display for Opcode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Continuation => write!(f, "Continuation"),
Self::Text => write!(f, "Text"),
Self::Binary => write!(f, "Binary"),
Self::Close => write!(f, "Close"),
Self::Ping => write!(f, "Ping"),
Self::Pong => write!(f, "Pong"),
}
}
}