#[derive(Debug)]
pub enum FrameParseError {
WrongFinRSV,
Incomplete {
expected: usize,
obtained: usize
},
Malformed,
NullContent,
InvalidUtf8(std::string::FromUtf8Error),
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 {}
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
FrameParse(FrameParseError),
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 {}