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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{DecodeError, MessageError};

/// An error returned by a Runner

#[derive(Debug)]
pub enum Error {
    /// An I/O error occured

    Io(std::io::Error),
    /// Invalid utf-8 was parsed (either you sent invalid utf-8, or Twitch did and we read it).

    InvalidUtf8(std::str::Utf8Error),
    /// We could not parse a message -- this should never happen

    ParsingFailure(MessageError),
    /// You requested a capability and Twitch rejected it

    InvalidCap {
        /// The capability name

        cap: String,
    },
    /// You're already on that channel

    AlreadyOnChannel {
        /// The channel name

        channel: String,
    },
    /// You weren't on that channel

    NotOnChannel {
        /// The channel name

        channel: String,
    },
    /// You could not join this channel, you were banned prior.

    BannedFromChannel {
        /// The channel name

        channel: String,
    },
    /// Your connection timed out.

    TimedOut,
    /// Twitch restarted the server, you should reconnect.

    ShouldReconnect,
    /// An unexpected EOF was found -- this means the connectionc losed abnormally.

    UnexpectedEof,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(err) => write!(f, "io error: {}", err),
            Self::InvalidUtf8(err) => write!(f, "invalid utf-8 while parsing: {}", err),
            Self::ParsingFailure(err) => write!(f, "could not parse message: {}", err),
            Self::InvalidCap { cap } => {
                write!(f, "request capability '{}' was not acknowledged", cap)
            }
            Self::AlreadyOnChannel { channel } => write!(f, "already on channel '{}'", channel),
            Self::NotOnChannel { channel } => write!(f, "not on channel '{}'", channel),
            Self::BannedFromChannel { channel } => write!(f, "banned from channel '{}'", channel),
            Self::TimedOut => write!(f, "your connection timed out"),
            Self::ShouldReconnect => write!(f, "you should reconnect. Twitch restarted the server"),
            Self::UnexpectedEof => write!(f, "reached an unexpected EOF"),
        }
    }
}

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

impl From<DecodeError> for Error {
    fn from(err: DecodeError) -> Self {
        match err {
            DecodeError::Io(err) => Self::Io(err),
            DecodeError::InvalidUtf8(err) => Self::InvalidUtf8(err),
            DecodeError::ParseError(err) => Self::ParsingFailure(err),
            DecodeError::Eof => Self::UnexpectedEof,
        }
    }
}

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

impl From<MessageError> for Error {
    fn from(err: MessageError) -> Self {
        Self::ParsingFailure(err)
    }
}