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
53
54
55
56
extern crate toml;
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::string::FromUtf8Error;

#[derive(Debug)]
pub enum Error {
    FileError(io::Error),
    InternalError(String),
}

/// Implement display for error type
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::FileError(ref e) => write!(f, "FileError: {}", e),
            Error::InternalError(ref s) => write!(f, "Internal error: {}", s),
        }
    }
}

/// This makes it an actual error
impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::FileError(ref e) => e.description(),
            Error::InternalError(ref _s) => "Internal processing error",
        }
    }
}

/// Conversion from default error to custom ones
impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::FileError(err)
    }
}

impl From<toml::ser::Error> for Error {
    fn from(err: toml::ser::Error) -> Error {
        Error::InternalError(err.to_string())
    }
}

impl From<FromUtf8Error> for Error {
    fn from(err: FromUtf8Error) -> Error {
        Error::InternalError(err.to_string())
    }
}

impl From<toml::de::Error> for Error {
    fn from(err: toml::de::Error) -> Error {
        Error::InternalError(err.to_string())
    }
}