|
1 | 1 | use std::convert::From; |
2 | | -use std::error; |
3 | | -use std::fmt; |
4 | 2 | use std::io; |
5 | | -use std::result; |
6 | 3 |
|
7 | | -pub type Result<T> = result::Result<T, Error>; |
8 | | - |
9 | | -#[derive(Debug)] |
10 | | -pub struct Error { |
11 | | - error_kind: ErrorKind, |
12 | | -} |
13 | | - |
14 | | -#[derive(Debug)] |
15 | | -pub enum ErrorKind { |
16 | | - Xdg(XdgError), |
17 | | - Io(io::Error) |
18 | | -} |
19 | | - |
20 | | -#[derive(Debug)] |
21 | | -pub enum XdgError { |
22 | | - NoHomeDir, |
23 | | - InvalidPath, |
24 | | - IncorrectPermissions, |
25 | | - IncorrectOwner, |
26 | | -} |
27 | | - |
28 | | -impl Error { |
29 | | - pub fn new(error_kind: ErrorKind) -> Error { |
30 | | - Error { error_kind: error_kind } |
| 4 | +error_chain! { |
| 5 | + types { |
| 6 | + Error, ErrorKind, ChainErr, Result; |
31 | 7 | } |
32 | | -} |
33 | 8 |
|
34 | | -impl fmt::Display for Error { |
35 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
36 | | - match self.error_kind { |
37 | | - ErrorKind::Xdg(ref e) => write!(f, "Xdg error: {:?}", e), |
38 | | - ErrorKind::Io(ref e) => e.fmt(f), |
39 | | - } |
| 9 | + links { |
40 | 10 | } |
41 | | -} |
42 | 11 |
|
43 | | -impl error::Error for Error { |
44 | | - fn description(&self) -> &str { |
45 | | - match self.error_kind { |
46 | | - ErrorKind::Xdg(_) => "Xdg error", |
47 | | - ErrorKind::Io(ref e) => e.description(), |
48 | | - } |
| 12 | + foreign_links { |
| 13 | + io::Error, Io, "IO error"; |
49 | 14 | } |
50 | | -} |
51 | | - |
52 | | -impl From<XdgError> for Error { |
53 | | - fn from(error: XdgError) -> Error { |
54 | | - Error { error_kind: ErrorKind::Xdg(error) } |
55 | | - } |
56 | | -} |
57 | 15 |
|
58 | | -impl From<io::Error> for Error { |
59 | | - fn from(error: io::Error) -> Error { |
60 | | - Error { error_kind: ErrorKind::Io(error) } |
61 | | - } |
62 | | -} |
63 | | - |
64 | | -impl From<XdgError> for Result<()> { |
65 | | - fn from(error: XdgError) -> Result<()> { |
66 | | - Err(Error::from(error)) |
| 16 | + errors { |
| 17 | + NoHomeDir { |
| 18 | + description("Could not find home dir") |
| 19 | + display("$HOME not defined or is not valid") |
| 20 | + } |
67 | 21 | } |
68 | 22 | } |
0 commit comments