1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#[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,
}
}
}