aboutsummaryrefslogtreecommitdiff
path: root/src/result.rs
blob: 9f35920505cd0f1c378b948993ace53a84c83956 (plain)
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
use std::error::Error;
use std::fmt::{Debug, Display};
use std::io;

pub type ArchiveResult<R> = Result<R, ArchiveError>;

#[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<io::Error> 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,
        }
    }
}