use futures_core::stream::TryStream;
use futures_sink::Sink;
use std::{error, fmt};
pub enum Error<T, I>
where
T: Sink<I> + TryStream,
{
BrokenTransportSend(<T as Sink<I>>::Error),
BrokenTransportRecv(Option<<T as TryStream>::Error>),
TransportFull,
ClientDropped,
Desynchronized,
}
impl<T, I> fmt::Display for Error<T, I>
where
T: Sink<I> + TryStream,
<T as Sink<I>>::Error: fmt::Display,
<T as TryStream>::Error: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::BrokenTransportSend(_) => f.pad("underlying transport failed to send a request"),
Error::BrokenTransportRecv(Some(_)) => {
f.pad("underlying transport failed while attempting to receive a response")
}
Error::BrokenTransportRecv(None) => f.pad("transport closed with in-flight requests"),
Error::TransportFull => f.pad("no more in-flight requests allowed"),
Error::ClientDropped => f.pad("Client was dropped"),
Error::Desynchronized => f.pad("server sent a response the client did not expect"),
}
}
}
impl<T, I> fmt::Debug for Error<T, I>
where
T: Sink<I> + TryStream,
<T as Sink<I>>::Error: fmt::Debug,
<T as TryStream>::Error: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::BrokenTransportSend(ref se) => write!(f, "BrokenTransportSend({:?})", se),
Error::BrokenTransportRecv(Some(ref se)) => write!(f, "BrokenTransportRecv({:?})", se),
Error::BrokenTransportRecv(None) => f.pad("BrokenTransportRecv"),
Error::TransportFull => f.pad("TransportFull"),
Error::ClientDropped => f.pad("ClientDropped"),
Error::Desynchronized => f.pad("Desynchronized"),
}
}
}
impl<T, I> error::Error for Error<T, I>
where
T: Sink<I> + TryStream,
<T as Sink<I>>::Error: error::Error + 'static,
<T as TryStream>::Error: error::Error + 'static,
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::BrokenTransportSend(ref se) => Some(se),
Error::BrokenTransportRecv(Some(ref se)) => Some(se),
_ => None,
}
}
}
impl<T, I> Error<T, I>
where
T: Sink<I> + TryStream,
{
pub(crate) fn from_sink_error(e: <T as Sink<I>>::Error) -> Self {
Error::BrokenTransportSend(e)
}
pub(crate) fn from_stream_error(e: <T as TryStream>::Error) -> Self {
Error::BrokenTransportRecv(Some(e))
}
}