1use std::fmt;
4use std::io;
5
6#[derive(Debug)]
7pub enum RarError {
8 InvalidSignature,
9 InvalidHeaderType(u8),
10 DecompressionNotSupported(u8),
11 EncryptedNotSupported,
12 BufferTooSmall { needed: usize, have: usize },
13 InvalidOffset { offset: u64, length: u64 },
14 Io(io::Error),
15 NoFilesFound,
16}
17
18impl fmt::Display for RarError {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Self::InvalidSignature => write!(f, "Invalid RAR signature"),
22 Self::InvalidHeaderType(t) => write!(f, "Invalid header type: {}", t),
23 Self::DecompressionNotSupported(m) => write!(f, "Decompression not supported (method: 0x{:02x})", m),
24 Self::EncryptedNotSupported => write!(f, "Encrypted archives not supported"),
25 Self::BufferTooSmall { needed, have } => write!(f, "Buffer too small: need {} bytes, have {}", needed, have),
26 Self::InvalidOffset { offset, length } => write!(f, "Invalid offset: {} (file length: {})", offset, length),
27 Self::Io(e) => write!(f, "IO error: {}", e),
28 Self::NoFilesFound => write!(f, "No files found in archive"),
29 }
30 }
31}
32
33impl std::error::Error for RarError {
34 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35 match self {
36 Self::Io(e) => Some(e),
37 _ => None,
38 }
39 }
40}
41
42impl From<io::Error> for RarError {
43 fn from(e: io::Error) -> Self {
44 Self::Io(e)
45 }
46}
47
48impl From<crate::decompress::DecompressError> for RarError {
49 fn from(e: crate::decompress::DecompressError) -> Self {
50 match e {
51 crate::decompress::DecompressError::UnsupportedMethod(m) => Self::DecompressionNotSupported(m),
52 _ => Self::Io(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
53 }
54 }
55}
56
57pub type Result<T> = std::result::Result<T, RarError>;