rusty_x/
error.rs

1extern crate toml;
2use std::error::Error as StdError;
3use std::fmt;
4use std::io;
5use std::string::FromUtf8Error;
6
7#[derive(Debug)]
8pub enum Error {
9    FileError(io::Error),
10    InternalError(String),
11}
12
13/// Implement display for error type
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match *self {
17            Error::FileError(ref e) => write!(f, "FileError: {}", e),
18            Error::InternalError(ref s) => write!(f, "Internal error: {}", s),
19        }
20    }
21}
22
23/// This makes it an actual error
24impl StdError for Error {
25    fn description(&self) -> &str {
26        match *self {
27            Error::FileError(ref e) => e.description(),
28            Error::InternalError(ref _s) => "Internal processing error",
29        }
30    }
31}
32
33/// Conversion from default error to custom ones
34impl From<io::Error> for Error {
35    fn from(err: io::Error) -> Error {
36        Error::FileError(err)
37    }
38}
39
40impl From<toml::ser::Error> for Error {
41    fn from(err: toml::ser::Error) -> Error {
42        Error::InternalError(err.to_string())
43    }
44}
45
46impl From<FromUtf8Error> for Error {
47    fn from(err: FromUtf8Error) -> Error {
48        Error::InternalError(err.to_string())
49    }
50}
51
52impl From<toml::de::Error> for Error {
53    fn from(err: toml::de::Error) -> Error {
54        Error::InternalError(err.to_string())
55    }
56}