#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Opcode {
Text,
Binary,
Close,
Ping,
Pong,
}
impl Opcode {
pub fn is_text(self) -> bool {
matches!(self, Self::Text)
}
pub fn is_control(self) -> bool {
matches!(self, Self::Close | Self::Ping | Self::Pong)
}
pub fn try_from(data: u8) -> Option<Self> {
let opcode = match data {
1 => Self::Text,
2 => Self::Binary,
8 => Self::Close,
9 => Self::Ping,
10 => Self::Pong,
_ => {
return None;
}
};
Some(opcode)
}
}
impl From<Opcode> for u8 {
fn from(opcode: Opcode) -> Self {
match opcode {
Opcode::Text => 1,
Opcode::Binary => 2,
Opcode::Close => 8,
Opcode::Ping => 9,
Opcode::Pong => 10,
}
}
}