use std::fmt::{self, Display};
use std::io;
use tokio_tungstenite::tungstenite;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug)]
pub enum ErrorKind {
IoError,
EncodeError,
DecodeError,
PacketError,
SendError,
SocketError,
ConfigError,
PidError,
InvalidClientStatus,
AuthFailed,
}
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug)]
pub struct Error {
kind: ErrorKind,
message: String,
}
impl Error {
#[must_use]
pub fn new(kind: ErrorKind, message: &str) -> Self {
Self {
kind,
message: message.to_owned(),
}
}
#[must_use]
pub const fn from_string(kind: ErrorKind, message: String) -> Self {
Self { kind, message }
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)
}
}
impl std::error::Error for Error {}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::from_string(ErrorKind::IoError, format!("IoError {:?}", err))
}
}
impl From<tungstenite::Error> for Error {
fn from(err: tungstenite::Error) -> Self {
Self::from_string(ErrorKind::IoError, format!("Websocket error: {:?}", err))
}
}
impl From<tokio_rustls::webpki::Error> for Error {
fn from(err: tokio_rustls::webpki::Error) -> Self {
Self::from_string(ErrorKind::ConfigError, format!("webpki error: {:?}", err))
}
}
impl From<quinn::ConnectError> for Error {
fn from(err: quinn::ConnectError) -> Self {
Self::from_string(
ErrorKind::SocketError,
format!("Quic connect error: {:?}", err),
)
}
}
impl From<quinn::ConnectionError> for Error {
fn from(err: quinn::ConnectionError) -> Self {
Self::from_string(
ErrorKind::SocketError,
format!("Quic connection error: {:?}", err),
)
}
}
impl From<quinn::ConfigError> for Error {
fn from(err: quinn::ConfigError) -> Self {
Self::from_string(
ErrorKind::ConfigError,
format!("Quic config error: {:?}", err),
)
}
}
impl From<quinn::WriteError> for Error {
fn from(err: quinn::WriteError) -> Self {
Self::from_string(
ErrorKind::SocketError,
format!("Quic write error: {:?}", err),
)
}
}
impl From<codec::EncodeError> for Error {
fn from(err: codec::EncodeError) -> Self {
Self::from_string(ErrorKind::EncodeError, format!("{:?}", err))
}
}
impl From<codec::DecodeError> for Error {
fn from(err: codec::DecodeError) -> Self {
Self::from_string(ErrorKind::DecodeError, format!("{:?}", err))
}
}