discord_rpc_client/
error.rs

1use std::{
2    borrow::Cow,
3    io::Error as IoError,
4    result::Result as StdResult,
5    sync::mpsc::RecvTimeoutError as ChannelTimeout,
6    fmt::{
7        self,
8        Display,
9        Formatter
10    },
11};
12use serde_json::Error as JsonError;
13
14
15#[derive(Debug)]
16pub enum Error {
17    IoError(IoError),
18    JsonError(JsonError),
19    Timeout(ChannelTimeout),
20    Conversion,
21    SubscriptionFailed,
22    ConnectionClosed,
23}
24
25impl Display for Error {
26    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
27        let msg = match self {
28            Error::Conversion => Cow::Borrowed("Failed to convert values"),
29            Error::SubscriptionFailed => Cow::Borrowed("Failed to subscribe to event"),
30            Error::ConnectionClosed => Cow::Borrowed("Connection closed"),
31            Error::IoError(ref err) => Cow::Owned(err.to_string()),
32            Error::JsonError(ref err) => Cow::Owned(err.to_string()),
33            Error::Timeout(ref err) => Cow::Owned(err.to_string()),
34        };
35
36        f.write_str(&msg)
37    }
38}
39
40impl From<IoError> for Error {
41    fn from(err: IoError) -> Self {
42        Error::IoError(err)
43    }
44}
45
46impl From<JsonError> for Error {
47    fn from(err: JsonError) -> Self {
48        Error::JsonError(err)
49    }
50}
51
52impl From<ChannelTimeout> for Error {
53    fn from(err: ChannelTimeout) -> Self {
54        Error::Timeout(err)
55    }
56}
57
58pub type Result<T> = StdResult<T, Error>;