use std::error;
use std::fmt;
use std::io;
use unsigned_varint::decode;
#[derive(Debug)]
pub enum MultistreamSelectError {
IoError(io::Error),
FailedHandshake,
UnknownMessage,
WrongProtocolName,
VarintParseError(String),
}
impl From<io::Error> for MultistreamSelectError {
#[inline]
fn from(err: io::Error) -> MultistreamSelectError {
MultistreamSelectError::IoError(err)
}
}
impl From<decode::Error> for MultistreamSelectError {
#[inline]
fn from(err: decode::Error) -> MultistreamSelectError {
MultistreamSelectError::VarintParseError(err.to_string())
}
}
impl error::Error for MultistreamSelectError {
#[inline]
fn description(&self) -> &str {
match *self {
MultistreamSelectError::IoError(_) => "I/O error",
MultistreamSelectError::FailedHandshake => {
"the remote doesn't use the same multistream-select protocol as we do"
}
MultistreamSelectError::UnknownMessage => "received an unknown message from the remote",
MultistreamSelectError::WrongProtocolName => {
"protocol names must always start with `/`, otherwise this error is returned"
}
MultistreamSelectError::VarintParseError(_) => {
"failure to parse variable-length integer"
}
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
MultistreamSelectError::IoError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for MultistreamSelectError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}