Skip to main content

libxch/
error.rs

1#[derive(Debug)]
2pub enum Error {
3    Fs(::std::io::Error),
4    PlatformError(crate::platform::PlatformError),
5    LogicError(String),
6    ChainError(Box<Error>, Box<Error>),
7    NotImplemented,
8}
9
10impl ::std::error::Error for Error {
11    fn description(&self) -> &str {
12        match *self {
13            Error::Fs(_)
14            | Error::PlatformError(_)
15            | Error::ChainError(_, _) => "Could not exchange paths",
16            |
17            Error::LogicError(ref s) => s,
18            Error::NotImplemented => "Not supported on this platform"
19        }
20    }
21
22    fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
23        match *self {
24            Error::Fs(ref e) => Some(e),
25            Error::PlatformError(ref e) => Some(e),
26            Error::ChainError(ref e1, ref e2) => e1.source().or_else(|| e2.source()),
27            _ => None,
28        }
29    }
30}
31
32impl ::std::fmt::Display for Error {
33    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
34        use ::std::error::Error;
35        if let Some(cause) = self.source() {
36            write!(f, "{}: {}", self.description(), cause)
37        } else {
38            write!(f, "{}", self.description())
39        }
40    }
41}
42
43impl From<::std::io::Error> for Error {
44    fn from(e: ::std::io::Error) -> Self {
45        Error::Fs(e)
46    }
47}
48
49impl<'a> From<&'a str> for Error {
50    fn from(s: &'a str) -> Self {
51        Error::LogicError(s.to_string())
52    }
53}
54
55impl From<String> for Error {
56    fn from(e: String) -> Self {
57        Error::LogicError(e)
58    }
59}
60
61impl From<crate::platform::PlatformError> for Error {
62    fn from(e: crate::platform::PlatformError) -> Self {
63        Error::PlatformError(e)
64    }
65}
66
67pub type Result<T> = ::std::result::Result<T, Error>;