#[derive(Debug)]
pub enum WireframeError<E = ()> {
DuplicateRoute(u32),
Io(std::io::Error),
Protocol(E),
Codec(crate::codec::CodecError),
}
impl<E> From<E> for WireframeError<E> {
fn from(error: E) -> Self { Self::Protocol(error) }
}
impl<E> WireframeError<E> {
#[must_use]
pub fn from_io(error: std::io::Error) -> Self { Self::Io(error) }
#[must_use]
pub fn from_codec(error: crate::codec::CodecError) -> Self { Self::Codec(error) }
#[must_use]
pub fn is_clean_close(&self) -> bool {
matches!(self, Self::Codec(codec_error) if codec_error.is_clean_close())
}
}
impl<E: std::fmt::Debug> std::fmt::Display for WireframeError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DuplicateRoute(id) => write!(f, "route id {id} was already registered"),
Self::Io(error) => write!(f, "transport error: {error}"),
Self::Protocol(error) => write!(f, "protocol error: {error:?}"),
Self::Codec(error) => write!(f, "codec error: {error}"),
}
}
}
impl<E> std::error::Error for WireframeError<E>
where
E: std::fmt::Debug + std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(error) => Some(error),
Self::Protocol(error) => Some(error),
Self::Codec(error) => Some(error),
Self::DuplicateRoute(_) => None,
}
}
}
pub type Result<T> = std::result::Result<T, WireframeError<()>>;