1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// An error returned by this crate
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
    /// A UTF-8 decoding/encoding error
    Utf8(std::str::Utf8Error),
    /// An IO error
    Io(std::io::Error),
    /// An error when trying to parse a Message
    Decode(crate::decode::ParseError),
    /// Tried to stop a non-running client
    NotRunning,
    /// Tried to start an already running client
    AlreadyRunning,
    /// An invalid channel was provided
    InvalidChannel(crate::ChannelError),
    /// The client has been disconnected
    ClientDisconnected,
}

impl From<std::str::Utf8Error> for Error {
    fn from(err: std::str::Utf8Error) -> Self {
        Self::Utf8(err)
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<crate::decode::ParseError> for Error {
    fn from(err: crate::decode::ParseError) -> Self {
        Self::Decode(err)
    }
}

impl From<crate::ChannelError> for Error {
    fn from(err: crate::ChannelError) -> Self {
        Self::InvalidChannel(err)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Utf8(err) => write!(f, "utf8 error: {}", err),
            Self::Io(err) => write!(f, "io error: {}", err),
            Self::Decode(err) => write!(f, "decode error: {}", err),
            Self::NotRunning => write!(f, "tried to stop a non-running client"),
            Self::AlreadyRunning => write!(f, "tried to start an already running client"),
            Self::InvalidChannel(err) => write!(f, "an invalid channel was provided: {}", err),
            Self::ClientDisconnected => write!(f, "this client has been disconnected"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Utf8(err) => Some(err),
            Self::Io(err) => Some(err),
            Self::Decode(err) => Some(err),
            Self::InvalidChannel(err) => Some(err),
            _ => None,
        }
    }
}