slate/
errors.rs

1use serde_json;
2use std::error;
3use std::fmt;
4use std::io;
5
6#[derive(Debug)]
7pub enum SlateError {
8    IO(io::Error),
9    JSON(serde_json::Error),
10}
11
12impl fmt::Display for SlateError {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        match *self {
15            SlateError::IO(ref err) => write!(f, "{}", err),
16            SlateError::JSON(ref err) => write!(f, "{}", err),
17        }
18    }
19}
20
21impl error::Error for SlateError {
22    fn description(&self) -> &str {
23        match *self {
24            SlateError::IO(ref err) => err.description(),
25            SlateError::JSON(ref err) => err.description(),
26        }
27    }
28
29    fn cause(&self) -> Option<&error::Error> {
30        match *self {
31            SlateError::IO(ref err) => Some(err),
32            SlateError::JSON(ref err) => Some(err),
33        }
34    }
35}
36
37impl From<io::Error> for SlateError {
38    fn from(err: io::Error) -> SlateError {
39        SlateError::IO(err)
40    }
41}
42
43impl From<serde_json::Error> for SlateError {
44    fn from(err: serde_json::Error) -> SlateError {
45        SlateError::JSON(err)
46    }
47}
48
49#[derive(Debug)]
50pub enum CommandError {
51    IO(io::Error),
52    Slate(SlateError),
53    Argument(String),
54}
55
56impl fmt::Display for CommandError {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        match *self {
59            CommandError::IO(ref err) => write!(f, "{}", err),
60            CommandError::Slate(ref err) => write!(f, "{}", err),
61            CommandError::Argument(ref string) => write!(f, "{}", string),
62        }
63    }
64}
65
66impl error::Error for CommandError {
67    fn description(&self) -> &str {
68        match *self {
69            CommandError::IO(ref err) => err.description(),
70            CommandError::Slate(ref err) => err.description(),
71            CommandError::Argument(ref string) => string,
72        }
73    }
74
75    fn cause(&self) -> Option<&error::Error> {
76        match *self {
77            CommandError::IO(ref err) => Some(err),
78            CommandError::Slate(ref err) => Some(err),
79            CommandError::Argument(_) => None,
80        }
81    }
82}
83
84impl From<io::Error> for CommandError {
85    fn from(err: io::Error) -> CommandError {
86        CommandError::IO(err)
87    }
88}
89
90impl From<SlateError> for CommandError {
91    fn from(err: SlateError) -> CommandError {
92        CommandError::Slate(err)
93    }
94}
95
96impl From<String> for CommandError {
97    fn from(err: String) -> CommandError {
98        CommandError::Argument(err)
99    }
100}