use crate::yamux::frame::FrameDecodeError;
#[non_exhaustive]
#[derive(Debug)]
pub enum ConnectionError {
Io(std::io::Error),
Decode(FrameDecodeError),
NoMoreStreamIds,
Closed,
TooManyStreams,
}
impl PartialEq for ConnectionError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(ConnectionError::Io(e1), ConnectionError::Io(e2)) => e1.kind() == e2.kind(),
(ConnectionError::Decode(e1), ConnectionError::Decode(e2)) => e1 == e2,
(ConnectionError::NoMoreStreamIds, ConnectionError::NoMoreStreamIds)
| (ConnectionError::Closed, ConnectionError::Closed)
| (ConnectionError::TooManyStreams, ConnectionError::TooManyStreams) => true,
_ => false,
}
}
}
impl std::fmt::Display for ConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConnectionError::Io(e) => write!(f, "i/o error: {}", e),
ConnectionError::Decode(e) => write!(f, "decode error: {}", e),
ConnectionError::NoMoreStreamIds =>
f.write_str("number of stream ids has been exhausted"),
ConnectionError::Closed => f.write_str("connection is closed"),
ConnectionError::TooManyStreams => f.write_str("maximum number of streams reached"),
}
}
}
impl std::error::Error for ConnectionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ConnectionError::Io(e) => Some(e),
ConnectionError::Decode(e) => Some(e),
ConnectionError::NoMoreStreamIds
| ConnectionError::Closed
| ConnectionError::TooManyStreams => None,
}
}
}
impl From<std::io::Error> for ConnectionError {
fn from(e: std::io::Error) -> Self {
ConnectionError::Io(e)
}
}
impl From<FrameDecodeError> for ConnectionError {
fn from(e: FrameDecodeError) -> Self {
ConnectionError::Decode(e)
}
}
impl From<futures::channel::mpsc::SendError> for ConnectionError {
fn from(_: futures::channel::mpsc::SendError) -> Self {
ConnectionError::Closed
}
}
impl From<futures::channel::oneshot::Canceled> for ConnectionError {
fn from(_: futures::channel::oneshot::Canceled) -> Self {
ConnectionError::Closed
}
}