use std::fmt;
use std::net::AddrParseError;
use std::string::FromUtf8Error;
use std::sync::mpsc::SendError;
#[derive(Clone, Debug)]
pub enum Error {
Connection(String),
Signaling(String),
Config(String),
Invalid(String),
System(String),
Busy(String),
Full,
Unknown,
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(_err: std::io::Error) -> Self {
Self::System(String::from("generic IO error"))
}
}
impl From<FromUtf8Error> for Error {
fn from(_err: FromUtf8Error) -> Self {
Self::System(String::from("address is not valid"))
}
}
impl From<AddrParseError> for Error {
fn from(_err: AddrParseError) -> Self {
Self::Invalid(String::from("ip address is not a valid ipv4 format"))
}
}
impl From<()> for Error {
fn from(_err: ()) -> Self {
Self::Invalid(String::from("data is invalid"))
}
}
impl<T> From<SendError<T>> for Error {
fn from(_err: SendError<T>) -> Self {
Self::Connection(String::from("channel is no longer available"))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Connection(s) => write!(f, "network connection failed: {}", s),
Self::Signaling(s) => write!(f, "signaling server is unavailable: {}", s),
Self::Config(s) => write!(f, "local configuration is not valid: {}", s),
Self::Invalid(s) => write!(f, "message is not valid: {}", s),
Self::System(s) => write!(f, "operating system error: {}", s),
Self::Busy(s) => write!(f, "process is busy or unavailable: {}", s),
Self::Full => write!(f, "item limit has been reached"),
Self::Unknown => write!(f, "unknown error"),
}
}
}