freedesktop_categories_codegen/
error.rs

1//! Common error type used throughout the crate.
2
3use std::env::VarError;
4use std::error::Error as StdError;
5use std::fmt::{Display, Formatter, Result as FmtResult};
6use std::io::Error as IoError;
7
8use curl::Error as CurlError;
9
10/// Types of errors and their causes.
11#[derive(Debug)]
12pub enum Error {
13    /// Failed to download and/or cache the specification XML file.
14    Curl(CurlError),
15    /// Failed to read or write from a file descriptor.
16    Io(IoError),
17    /// Failed to load an environment variable.
18    Var(VarError),
19    /// Failed to parse the Freedesktop.org Desktop Menu spec.
20    Xml(Box<StdError>),
21}
22
23impl StdError for Error {
24    fn description(&self) -> &str {
25        match *self {
26            Error::Curl(_) => "Failed to download XML file from Freedesktop.org",
27            Error::Io(_) => "Failed to read or write from a file",
28            Error::Var(_) => "Failed to load an environment variable",
29            Error::Xml(_) => "Could not parse Freedesktop.org Desktop Menu spec",
30        }
31    }
32
33    fn cause(&self) -> Option<&StdError> {
34        match *self {
35            Error::Curl(ref e) => Some(e),
36            Error::Io(ref e) => Some(e),
37            Error::Var(ref e) => Some(e),
38            Error::Xml(ref e) => Some(e.as_ref()),
39        }
40    }
41}
42
43impl Display for Error {
44    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
45        match *self {
46            Error::Curl(ref e) => write!(fmt, "{}: {}", self.description(), e),
47            Error::Io(ref e) => write!(fmt, "{}: {}", self.description(), e),
48            Error::Var(ref e) => write!(fmt, "{}: {}", self.description(), e),
49            Error::Xml(ref e) => write!(fmt, "{}: {}", self.description(), e),
50        }
51    }
52}
53
54impl From<CurlError> for Error {
55    fn from(e: CurlError) -> Self {
56        Error::Curl(e)
57    }
58}
59
60impl From<IoError> for Error {
61    fn from(e: IoError) -> Self {
62        Error::Io(e)
63    }
64}
65
66impl From<VarError> for Error {
67    fn from(e: VarError) -> Self {
68        Error::Var(e)
69    }
70}
71
72impl From<Box<StdError>> for Error {
73    fn from(e: Box<StdError>) -> Self {
74        Error::Xml(e)
75    }
76}