aboutsummaryrefslogtreecommitdiff
path: root/src/result.rs
blob: 715f10cb92e4131ccdeebf1a262714f0b29f043a (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
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),
    WrongSignature { expected: u32, received: u32 },
}

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::WrongSignature { expected, received } => {
                write!(
                    f,
                    "Wrong signature. Expected: {expected}, received: {received}"
                )
            }
        }
    }
}

impl Error for ArchiveError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::IO(source) => Some(source),
            _ => None,
        }
    }
}