xdg_basedir/
error.rs

1use std::convert::From;
2use std::error;
3use std::fmt;
4use std::io;
5use std::result;
6
7pub type Result<T> = result::Result<T, Error>;
8
9#[derive(Debug)]
10pub struct Error {
11    error_kind: ErrorKind,
12}
13
14#[derive(Debug)]
15pub enum ErrorKind {
16    Xdg(XdgError),
17    Io(io::Error)
18}
19
20#[derive(Debug)]
21pub enum XdgError {
22    NoHomeDir,
23    InvalidPath,
24    IncorrectPermissions,
25    IncorrectOwner,
26}
27
28impl Error {
29    pub fn new(error_kind: ErrorKind) -> Error {
30        Error { error_kind: error_kind }
31    }
32}
33
34impl 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        }
40    }
41}
42
43impl 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        }
49    }
50}
51
52impl From<XdgError> for Error {
53    fn from(error: XdgError) -> Error {
54        Error { error_kind: ErrorKind::Xdg(error) }
55    }
56}
57
58impl From<io::Error> for Error {
59    fn from(error: io::Error) -> Error {
60        Error { error_kind: ErrorKind::Io(error) }
61    }
62}
63
64impl From<XdgError> for Result<()> {
65    fn from(error: XdgError) -> Result<()> {
66        Err(Error::from(error))
67    }
68}