use std::fmt;
use tokio::io;
#[derive(Debug)]
pub enum ParamError {
Key(String),
Value(String)
}
#[derive(Debug)]
pub enum Error {
BadFormat(String),
InvalidSize(String),
IO(std::io::Error),
Param(ParamError),
SerializeError(String),
Protocol(String)
}
impl Error {
pub fn bad_format(s: impl Into<String>) -> Self {
Self::BadFormat(s.into())
}
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BadFormat(s) => write!(f, "Bad format; {s}"),
Self::InvalidSize(s) => write!(f, "Invalid size; {s}"),
Self::IO(e) => write!(f, "I/O error; {e}"),
Self::Param(pe) => {
if f.alternate() {
match pe {
ParamError::Key(s) => write!(f, "Invalid key; {s}"),
ParamError::Value(s) => write!(f, "Invalid value; {s}")
}
} else {
match pe {
ParamError::Key(s) | ParamError::Value(s) => write!(f, "{s}")
}
}
}
Self::Protocol(s) => write!(f, "Protocol; {s}"),
Self::SerializeError(s) => write!(f, "Unable to serialize; {s}")
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::IO(err)
}
}