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
use std::error;
use std::fmt;
use std::io;

#[derive(Debug)]
pub enum ProbeError {
    /// IO error when opening file or command described in
    /// second field of the error
    IO(io::Error, String),
    /// Unexpected content in file or output
    UnexpectedContent(String),
    /// Input into a calculation function is invalid
    InvalidInput(String),
    /// Command failed
    StatusFailure(String),
}

impl fmt::Display for ProbeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ProbeError::IO(ref err, ref path) => write!(f, "{} for {}", err, path),
            ProbeError::UnexpectedContent(ref err) => write!(f, "{}", err),
            ProbeError::InvalidInput(ref err) => write!(f, "{}", err),
            ProbeError::StatusFailure(ref err) => write!(f, "{}", err),
        }
    }
}

impl error::Error for ProbeError {
    fn description(&self) -> &str {
        match *self {
            #[allow(deprecated)]
            ProbeError::IO(ref err, ref _path) => err.description(),
            ProbeError::UnexpectedContent(ref err) => err,
            ProbeError::InvalidInput(ref err) => err,
            ProbeError::StatusFailure(ref err) => err,
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            ProbeError::IO(ref err, ref _path) => Some(err),
            ProbeError::UnexpectedContent(_) => None,
            ProbeError::InvalidInput(_) => None,
            ProbeError::StatusFailure(_) => None,
        }
    }
}