1use std::str;
25use std::{error, fmt, io};
26use std::time::Duration;
27
28use futures::sync::{mpsc, oneshot};
29
30use codec;
31
32#[derive(Debug)]
33pub enum Error {
34 Internal(String),
36
37 IO(io::Error),
39
40 Value(String, Option<codec::Cmd>),
42
43 Unexpected(String),
45
46 EndOfStream,
48
49 NotConnected,
51
52 Disconnected,
54
55 Remote(String),
57
58 Processing(Duration, String),
60}
61
62pub fn internal<T: Into<String>>(msg: T) -> Error {
63 Error::Internal(msg.into())
64}
65
66pub fn value<T: Into<String>>(msg: T, val: codec::Cmd) -> Error {
67 Error::Value(msg.into(), Some(val))
68}
69
70impl From<io::Error> for Error {
71 fn from(err: io::Error) -> Error {
72 Error::IO(err)
73 }
74}
75
76impl From<oneshot::Canceled> for Error {
77 fn from(err: oneshot::Canceled) -> Error {
78 Error::Unexpected(format!("Oneshot was cancelled before use: {}", err))
79 }
80}
81
82impl<T: 'static + Send> From<mpsc::SendError<T>> for Error {
83 fn from(err: mpsc::SendError<T>) -> Error {
84 Error::Unexpected(format!("Cannot write to channel: {}", err))
85 }
86}
87
88impl error::Error for Error {
89 fn description(&self) -> &str {
90 match *self {
91 Error::IO(ref err) => err.description(),
92 Error::Value(ref s, _) => s,
93 Error::Unexpected(ref s) => s,
94 Error::Internal(ref s) => s,
95 Error::EndOfStream => "End of Stream",
96 Error::Remote(ref s) => s,
97 Error::NotConnected => "Not Connected",
98 Error::Disconnected => "Disconnected",
99 Error::Processing(_, ref s) => s,
100 }
101 }
102
103 fn cause(&self) -> Option<&error::Error> {
104 match *self {
105 Error::IO(ref err) => Some(err),
106 Error::Value(_, _) => None,
107 Error::Internal(_) => None,
108 Error::Unexpected(_) => None,
109 Error::EndOfStream => None,
110 Error::Remote(_) => None,
111 Error::NotConnected => None,
112 Error::Disconnected => None,
113 Error::Processing(_, _) => None,
114 }
115 }
116}
117
118impl fmt::Display for Error {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 use std::error::Error;
121 fmt::Display::fmt(self.description(), f)
122 }
123}