use std::fmt;
use anybytes::view::ViewError;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
InvalidArgument(String),
InvalidMetadata(String),
MismatchedHintFlags,
Io(std::io::Error),
View(ViewError),
}
impl Error {
pub fn invalid_argument(msg: impl Into<String>) -> Self {
Self::InvalidArgument(msg.into())
}
pub fn invalid_metadata(msg: impl Into<String>) -> Self {
Self::InvalidMetadata(msg.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidArgument(msg) => write!(f, "{msg}"),
Error::InvalidMetadata(msg) => write!(f, "{msg}"),
Error::MismatchedHintFlags => write!(f, "mismatched hint flags"),
Error::Io(err) => write!(f, "I/O error: {err}"),
Error::View(err) => write!(f, "view error: {err}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::InvalidArgument(_) | Error::InvalidMetadata(_) | Error::MismatchedHintFlags => {
None
}
Error::Io(err) => Some(err),
Error::View(err) => Some(err),
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl From<ViewError> for Error {
fn from(err: ViewError) -> Self {
Error::View(err)
}
}