1#[derive(Debug)]
3pub enum FrameParseError {
4 WrongFinRSV,
6 Incomplete {
8 expected: usize,
9 obtained: usize
10 },
11 Malformed,
13 NullContent,
15 InvalidUtf8(std::string::FromUtf8Error),
17 UnsupportedOpCode
19}
20
21impl std::fmt::Display for FrameParseError {
22 fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
23 let content = match self {
24 FrameParseError::WrongFinRSV => format!("first 4 bits of the message are malformed"),
25 FrameParseError::Incomplete{expected, obtained} => format!("mismatching payload length in message (expected: {}, obtained: {})", expected, obtained),
26 FrameParseError::Malformed => format!("the message does not have the corret structure or enough bytes"),
27 FrameParseError::NullContent => format!("can't parse because the message has length 0"),
28 FrameParseError::InvalidUtf8(e) => format!("invalid utf8 bytes, {}", e),
29 FrameParseError::UnsupportedOpCode => format!("the op code received is not supported")
30 };
31 write!(formatter, "{}", content)
32 }
33}
34
35impl std::error::Error for FrameParseError {}
36
37#[derive(Debug)]
39pub enum Error {
40 Io(std::io::Error),
42 FrameParse(FrameParseError),
44 ConnectionReset
46}
47
48impl std::fmt::Display for Error {
49 fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
50 let content = match self {
51 Error::Io(inner_error) => format!("io error: {}", inner_error),
52 Error::FrameParse(fpe) => format!("frame parse error: {}", fpe),
53 Error::ConnectionReset => format!("connection reset by peer")
54 };
55 write!(formatter, "{}", content)
56 }
57}
58
59impl std::error::Error for Error {}