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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::{convert::TryFrom, fmt};
use crate::connection::CloseReason;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Incoming<'a> {
Data(Data),
Pong(&'a [u8]),
Closed(CloseReason),
}
impl Incoming<'_> {
pub fn is_data(&self) -> bool {
if let Incoming::Data(_) = self {
true
} else {
false
}
}
pub fn is_pong(&self) -> bool {
if let Incoming::Pong(_) = self {
true
} else {
false
}
}
pub fn is_text(&self) -> bool {
if let Incoming::Data(d) = self {
d.is_text()
} else {
false
}
}
pub fn is_binary(&self) -> bool {
if let Incoming::Data(d) = self {
d.is_binary()
} else {
false
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Data {
Text(usize),
Binary(usize),
}
impl Data {
pub fn is_text(&self) -> bool {
if let Data::Text(_) = self {
true
} else {
false
}
}
pub fn is_binary(&self) -> bool {
if let Data::Binary(_) = self {
true
} else {
false
}
}
pub fn len(&self) -> usize {
match self {
Data::Text(n) => *n,
Data::Binary(n) => *n,
}
}
}
#[derive(Debug)]
pub struct ByteSlice125<'a>(&'a [u8]);
#[derive(Clone, Debug)]
pub struct SliceTooLarge(());
impl fmt::Display for SliceTooLarge {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Slice larger than 125 bytes")
}
}
impl std::error::Error for SliceTooLarge {}
impl<'a> TryFrom<&'a [u8]> for ByteSlice125<'a> {
type Error = SliceTooLarge;
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
if value.len() > 125 {
Err(SliceTooLarge(()))
} else {
Ok(ByteSlice125(value))
}
}
}
impl AsRef<[u8]> for ByteSlice125<'_> {
fn as_ref(&self) -> &[u8] {
self.0
}
}