use std::{fmt, io, string::FromUtf8Error};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Protocol(&'static str),
UnknownCommand(String),
TooLong(&'static str),
Utf8(FromUtf8Error),
Timeout,
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
impl From<FromUtf8Error> for Error {
fn from(e: FromUtf8Error) -> Self {
Error::Utf8(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "io error: {e}"),
Error::Protocol(s) => write!(f, "protocol error: {s}"),
Error::UnknownCommand(cmd) => write!(f, "unknown command: {cmd}"),
Error::TooLong(what) => write!(f, "value too long: {what}"),
Error::Utf8(e) => write!(f, "utf-8 error: {e}"),
Error::Timeout => write!(f, "operation timed out"),
}
}
}
impl std::error::Error for Error {}