use crate::{DecodeError, MessageError};
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
InvalidUtf8(std::str::Utf8Error),
ParsingFailure(MessageError),
InvalidCap {
cap: String,
},
AlreadyOnChannel {
channel: String,
},
NotOnChannel {
channel: String,
},
BannedFromChannel {
channel: String,
},
TimedOut,
ShouldReconnect,
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)
}
}