use std::error::Error;
use std::fmt;
use std::io;
#[derive(Debug, Clone)]
pub enum SmuxError {
InvalidProtocol,
Consumed,
GoAway,
Timeout,
WouldBlock,
Io(io::ErrorKind),
}
impl fmt::Display for SmuxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SmuxError::InvalidProtocol => write!(f, "invalid protocol"),
SmuxError::Consumed => write!(f, "peer consumed more than sent"),
SmuxError::GoAway => write!(f, "stream id overflows, should start a new connection"),
SmuxError::Timeout => write!(f, "timeout"),
SmuxError::WouldBlock => write!(f, "operation would block on IO"),
SmuxError::Io(kind) => write!(f, "io error: {:?}", kind),
}
}
}
impl Error for SmuxError {}
impl From<io::Error> for SmuxError {
fn from(err: io::Error) -> Self {
SmuxError::Io(err.kind())
}
}
impl From<SmuxError> for io::Error {
fn from(err: SmuxError) -> Self {
match err {
SmuxError::Timeout => io::Error::new(io::ErrorKind::TimedOut, err),
SmuxError::Io(kind) => io::Error::from(kind),
_ => io::Error::new(io::ErrorKind::Other, err),
}
}
}