use std::error::Error; use std::fmt::{Debug, Display}; use std::io; pub type ArchiveResult = Result; #[derive(Debug)] pub enum ArchiveError { IO(io::Error), BadArchive { reason: &'static str }, IncorrectSignature { expected: u32, received: u32 }, IncorrectString { location: &'static str }, IncorrectDate { year: u16, month: u16, day: u16 }, IncorrectTime { hour: u16, min: u16, sec: u16 }, UnsupportedCompressionMethod { method: u16 }, } impl From for ArchiveError { fn from(value: io::Error) -> Self { Self::IO(value) } } impl Display for ArchiveError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::IO(err) => write!(f, "{err}"), Self::BadArchive { reason } => { write!(f, "Bad archive because {reason}") } Self::IncorrectSignature { expected, received } => { write!( f, "Wrong signature (expected: {expected}, received: {received})" ) } Self::IncorrectString { location } => { write!(f, "Incorrect string in {location}") } Self::IncorrectDate { year, month, day } => { write!( f, "Incorrect date (year: {year}, month: {month}, day: {day})" ) } Self::IncorrectTime { hour, min, sec } => { write!(f, "Incorrect time (hour: {hour}, min: {min}, sec: {sec})") } Self::UnsupportedCompressionMethod { method } => { write!(f, "Unsupported compression method (method: {method})") } } } } impl Error for ArchiveError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { Self::IO(source) => Some(source), _ => None, } } }