websocket_codec/
opcode.rs1#[derive(Copy, Clone, Debug, PartialEq)]
3pub enum Opcode {
4 Text,
6 Binary,
8 Close,
10 Ping,
12 Pong,
14}
15
16impl Opcode {
17 pub fn is_text(self) -> bool {
19 matches!(self, Self::Text)
20 }
21
22 pub fn is_control(self) -> bool {
24 matches!(self, Self::Close | Self::Ping | Self::Pong)
25 }
26
27 pub fn try_from(data: u8) -> Option<Self> {
31 let opcode = match data {
32 1 => Self::Text,
33 2 => Self::Binary,
34 8 => Self::Close,
35 9 => Self::Ping,
36 10 => Self::Pong,
37 _ => {
38 return None;
39 }
40 };
41
42 Some(opcode)
43 }
44}
45
46impl From<Opcode> for u8 {
47 fn from(opcode: Opcode) -> Self {
48 match opcode {
49 Opcode::Text => 1,
50 Opcode::Binary => 2,
51 Opcode::Close => 8,
52 Opcode::Ping => 9,
53 Opcode::Pong => 10,
54 }
55 }
56}