use std::fmt;
#[derive(Debug)]
pub enum Error {
Os(std::io::Error),
Truncated,
BufferTooSmall {
needed: usize,
},
Interrupted,
ConnectionClosed,
Overrun,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Os(e) => write!(f, "system call error: {e}"),
Error::Truncated => write!(f, "truncated message"),
Error::BufferTooSmall { needed } => {
write!(f, "buffer too small, need at least {needed} bytes")
}
Error::Interrupted => write!(f, "interrupted by signal"),
Error::ConnectionClosed => write!(f, "connection closed"),
Error::Overrun => write!(f, "message overrun, events may have been dropped"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Os(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Os(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;