datasus_dbc/
error.rs

1#[derive(Debug)]
2pub enum Error {
3    /// An IO error
4    Io(std::io::Error),
5    /// Error while decompressing the content of the file
6    Decompression(explode::Error),
7    /// File without dbc header
8    MissingHeader,
9    /// Header size is greater than the file size
10    InvalidHeaderSize,
11}
12
13/// Result type from reading a dbc file
14pub type Result<T> = std::result::Result<T, Error>;
15
16impl std::convert::From<std::io::Error> for Error {
17    fn from(err: std::io::Error) -> Self {
18        Error::Io(err)
19    }
20}
21
22impl std::fmt::Display for Error {
23    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24        match self {
25            Error::Io(err) => write!(f, "{}", err),
26            Error::Decompression(err) => write!(f, "{}", err),
27            Error::MissingHeader => write!(f, "file does not contain dbc header or is empty"),
28            Error::InvalidHeaderSize => write!(f, "dbc header size is greater than the file size"),
29        }
30    }
31}
32
33impl std::error::Error for Error {
34    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35        match self {
36            Error::Io(err) => Some(err),
37            Error::Decompression(err) => Some(err),
38            _ => None,
39        }
40    }
41}