1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
use std::{ error::Error, fmt, io }; #[derive(Debug)] pub enum CacheError { Io(io::Error), Read(ReadError), Compression(CompressionError), } macro_rules! impl_from { ($ty:path, $var:ident) => { impl From<$ty> for CacheError { #[inline] fn from(err: $ty) -> Self { Self::$var(err) } } }; } impl_from!(io::Error, Io); impl_from!(ReadError, Read); impl_from!(CompressionError, Compression); impl Error for CacheError { #[inline] fn source(&self) -> Option<&(dyn Error + 'static)> { match self { Self::Io(err) => Some(err), Self::Read(err) => Some(err), Self::Compression(err) => Some(err), } } } impl fmt::Display for CacheError { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::Io(err) => err.fmt(f), Self::Read(err) => err.fmt(f), Self::Compression(err) => err.fmt(f), } } } #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub enum ReadError { IndexNotFound(u8), ArchiveNotFound(u8, u16), IndexRefNotFound(u16), WhirlpoolUnsupported(), RefTblEntryNotFound(u8), } impl Error for ReadError {} impl fmt::Display for ReadError { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::IndexNotFound(id) => write!(f, "Index {} was not found.", id), Self::ArchiveNotFound(index_id, archive_id) => write!(f, "Index {} does not contain archive {}.", index_id, archive_id), Self::IndexRefNotFound(index_id) => write!(f, "Index reference with id {} not found.", index_id), Self::WhirlpoolUnsupported() => write!(f, "Whirlpool is currently unsupported."), Self::RefTblEntryNotFound(id) => write!(f, "Reference Table Entry {} not found.", id), } } } #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub enum CompressionError { Unsupported(u8), LengthMismatch(usize, usize), } impl Error for CompressionError {} impl fmt::Display for CompressionError { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::Unsupported(compression) => write!(f, "Invalid compression: {} is unsupported.", compression), Self::LengthMismatch(expected, actual) => write!(f, "Uncompressed length mismatch: expected length {} but length was {}.", expected, actual), } } }