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

/// The error type for any errors encountered with the engine.
#[derive(Debug)]
pub enum EngineError {
    /// Wrapper around any io errors encountered while trying to communicate with the engine.
    Io(io::Error),

    /// Engine doesn't recognize the specified option.
    UnknownOption(String),
}

impl fmt::Display for EngineError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            EngineError::Io(ref err) => write!(f, "IO error: {}", err),
            EngineError::UnknownOption(ref option) => write!(f, "No such option: '{}'", option.as_str())
        }
    }
}

impl std::error::Error for EngineError {
    fn description(&self) -> &str {
        match *self {
            EngineError::Io(ref err) => err.description(),
            EngineError::UnknownOption(..) => "Unknown option"
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            EngineError::Io(ref err) => Some(err),
            EngineError::UnknownOption(..) => None
        }
    }
}

impl From<io::Error> for EngineError {
    fn from(err: io::Error) -> EngineError {
        EngineError::Io(err)
    }
}

/// A Result type which uses [`EngineError`] for representing errors.
///
/// [`EngineError`]: enum.EngineError.html
pub type Result<T> = std::result::Result<T, EngineError>;