aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 6d7aba4b1f93848aae2cf66743b2d923e9ee58de (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
use std::error::Error;
use std::fmt::Display;
use std::io;

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

#[derive(Debug)]
pub enum ArchiveError<E: Error> {
    IO(io::Error),
    Driver { name: &'static str, error: E },
}

impl<E: Error> From<io::Error> for ArchiveError<E> {
    fn from(value: io::Error) -> Self {
        Self::IO(value)
    }
}

impl<E: Error> Display for ArchiveError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArchiveError::IO(error) => write!(f, "{error}"),
            ArchiveError::Driver { name, error } => write!(f, "{name}: {error}"),
        }
    }
}

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