1
2use std::error::Error;
3use std::io::Error as IoError;
4
5use futures::Canceled;
6use futures::sync::mpsc::SendError;
7
8use std::fmt;
9
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub enum WomboErrorKind {
12 SendError,
14 Canceled,
16 NotInitialized,
18 Unknown
20}
21
22impl WomboErrorKind {
23
24 pub fn to_string(&self) -> &'static str {
25 match *self {
26 WomboErrorKind::Canceled => "Canceled",
27 WomboErrorKind::Unknown => "Unknown",
28 WomboErrorKind::SendError => "Send Error",
29 WomboErrorKind::NotInitialized => "Not Initialized"
30 }
31 }
32
33}
34
35#[derive(Clone, Debug, Eq, PartialEq)]
37pub struct WomboError {
38 kind: WomboErrorKind,
39 details: String
40}
41
42impl WomboError {
43
44 pub fn new<S: Into<String>>(kind: WomboErrorKind, details: S) -> WomboError {
45 WomboError { kind, details: details.into() }
46 }
47
48 pub fn to_string(&self) -> String {
49 format!("{}: {}", self.kind.to_string(), self.details)
50 }
51
52 pub fn new_canceled() -> WomboError {
53 WomboError {
54 kind: WomboErrorKind::Canceled,
55 details: String::new()
56 }
57 }
58
59 pub fn new_not_initialized() -> WomboError {
60 WomboError {
61 kind: WomboErrorKind::NotInitialized,
62 details: String::new()
63 }
64 }
65
66 pub fn kind(&self) -> &WomboErrorKind {
67 &self.kind
68 }
69
70}
71
72impl fmt::Display for WomboError {
73
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 write!(f, "{}: {}", self.kind.to_string(), self.details)
76 }
77
78}
79
80impl Error for WomboError {
81
82 fn description(&self) -> &str {
83 &self.details
84 }
85
86}
87
88impl From<()> for WomboError {
89 fn from(_: ()) -> Self {
90 WomboError::new(WomboErrorKind::Unknown, "Empty error.")
91 }
92}
93
94impl From<Canceled> for WomboError {
95 fn from(e: Canceled) -> Self {
96 WomboError::new(WomboErrorKind::Canceled, format!("{}", e))
97 }
98}
99
100impl<T: Into<WomboError>> From<SendError<T>> for WomboError {
101 fn from(e: SendError<T>) -> Self {
102 WomboError::new(WomboErrorKind::SendError, format!("{}", e))
103 }
104}
105
106impl From<IoError> for WomboError {
107 fn from(e: IoError) -> Self {
108 WomboError::new(WomboErrorKind::Unknown, format!("{}", e))
109 }
110}