use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ErrorKind {
NotXisf,
Truncated,
BadHeader,
BadAttribute,
Unsupported,
ChecksumMismatch,
Compression,
NotFound,
InvalidArgument,
Io,
}
impl ErrorKind {
pub fn name(self) -> &'static str {
match self {
ErrorKind::NotXisf => "not-xisf",
ErrorKind::Truncated => "truncated",
ErrorKind::BadHeader => "bad-header",
ErrorKind::BadAttribute => "bad-attribute",
ErrorKind::Unsupported => "unsupported",
ErrorKind::ChecksumMismatch => "checksum-mismatch",
ErrorKind::Compression => "compression",
ErrorKind::NotFound => "not-found",
ErrorKind::InvalidArgument => "invalid-argument",
ErrorKind::Io => "io",
}
}
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
message: String,
}
impl Error {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self { kind, message: message.into() }
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind.name(), self.message)
}
}
impl core::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::new(ErrorKind::Io, e.to_string())
}
}
pub type Result<T> = core::result::Result<T, Error>;
#[macro_export]
macro_rules! err {
($kind:ident, $($arg:tt)*) => {
$crate::error::Error::new($crate::error::ErrorKind::$kind, format!($($arg)*))
};
}