use std::fmt;
#[derive(Debug)]
pub enum ProtocolError {
Truncated,
UnknownTag(u8),
InvalidField(&'static str),
MessageTooLarge(usize),
Io(std::io::Error),
}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Truncated => write!(f, "truncated message"),
Self::UnknownTag(tag) => write!(f, "unknown message tag: {tag}"),
Self::InvalidField(field) => write!(f, "invalid field: {field}"),
Self::MessageTooLarge(size) => write!(f, "message too large: {size} bytes"),
Self::Io(e) => write!(f, "I/O error: {e}"),
}
}
}
impl std::error::Error for ProtocolError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for ProtocolError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}