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
/// Error indicating a frame parsing error
#[derive(Debug)]
pub enum FrameParseError {
    /// Indicates that the first 4 bits of the message are unsupported
    WrongFinRSV,
    /// Indicates that the message content is incomplete
    Incomplete {
        expected: usize,
        obtained: usize
    },
    /// Indicates that the message is malformed
    Malformed,
    /// Indicates that the message contains 0 bytes
    NullContent,
    /// The text sent through the message is not valid a utf-8
    InvalidUtf8(std::string::FromUtf8Error),
    /// Indicates an unsupported operation code contained in the frame
    UnsupportedOpCode
}

impl std::fmt::Display for FrameParseError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let content = match self {
            FrameParseError::WrongFinRSV => format!("first 4 bits of the message are malformed"),
            FrameParseError::Incomplete{expected, obtained} => format!("mismatching payload length in message (expected: {}, obtained: {})", expected, obtained),
            FrameParseError::Malformed => format!("the message does not have the corret structure or enough bytes"),
            FrameParseError::NullContent => format!("can't parse because the message has length 0"),
            FrameParseError::InvalidUtf8(e) => format!("invalid utf8 bytes, {}", e),
            FrameParseError::UnsupportedOpCode => format!("the op code received is not supported")
        };
        write!(formatter, "{}", content)
    }
}

impl std::error::Error for FrameParseError {}

/// Errors thrown by this library
#[derive(Debug)]
pub enum Error {
    /// Standard io error
    Io(std::io::Error),
    /// Could not parse properly a frame, the detail is contained inside
    FrameParse(FrameParseError),
    /// Indicates that the connection was closed abruptly
    ConnectionReset
}

impl std::fmt::Display for Error {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let content = match self {
            Error::Io(inner_error) => format!("io error: {}", inner_error),
            Error::FrameParse(fpe) => format!("frame parse error: {}", fpe),
            Error::ConnectionReset => format!("connection reset by peer")
        };
        write!(formatter, "{}", content)
    }
}

impl std::error::Error for Error {}