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
use crate::core::fmt;
use crate::error::CompressionError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BZip2Error {
    DataError,
    DataErrorMagicFirst,
    DataErrorMagic,
    UnexpectedEof,
    Unexpected,
}

impl fmt::Display for BZip2Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.description_in())
    }
}

#[cfg(feature = "std")]
impl ::std::error::Error for BZip2Error {
    fn description(&self) -> &str {
        self.description_in()
    }

    fn cause(&self) -> Option<&dyn (::std::error::Error)> {
        None
    }
}

impl BZip2Error {
    fn description_in(&self) -> &str {
        match *self {
            BZip2Error::DataError => "data integrity (CRC) error in data",
            BZip2Error::DataErrorMagicFirst => {
                "bad magic number (file not created by bzip2)"
            }
            BZip2Error::DataErrorMagic => "trailing garbage after EOF ignored",
            BZip2Error::UnexpectedEof => "file ends unexpectedly",
            BZip2Error::Unexpected => "unexpected error",
        }
    }
}

impl From<BZip2Error> for CompressionError {
    fn from(error: BZip2Error) -> Self {
        match error {
            BZip2Error::UnexpectedEof => CompressionError::UnexpectedEof,
            BZip2Error::Unexpected => CompressionError::Unexpected,
            _ => CompressionError::DataError,
        }
    }
}