use core::fmt::{self, Debug, Display};
use channels_io::framed::{FramedReadError, FramedWriteError};
use crate::util::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EncodeError {
TooLarge,
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLarge => f.write_str("data too large"),
}
}
}
impl Error for EncodeError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SendError<Ser, Io> {
Encode(EncodeError),
Io(Io),
Serde(Ser),
}
impl<Ser, Io> Display for SendError<Ser, Io>
where
Ser: Display,
Io: Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Encode(e) => Display::fmt(e, f),
Self::Io(e) => Display::fmt(e, f),
Self::Serde(e) => Display::fmt(e, f),
}
}
}
impl<Ser, Io> Error for SendError<Ser, Io> where Self: Debug + Display {}
impl<Ser, Io> From<FramedWriteError<EncodeError, Io>>
for SendError<Ser, Io>
{
fn from(value: FramedWriteError<EncodeError, Io>) -> Self {
match value {
FramedWriteError::Encode(x) => Self::Encode(x),
FramedWriteError::Io(x) => Self::Io(x),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DecodeError {
InvalidChecksum,
OutOfOrder,
TooLarge,
VersionMismatch,
}
impl Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidChecksum => f.write_str("invalid checksum"),
Self::OutOfOrder => f.write_str("data out of order"),
Self::TooLarge => f.write_str("data too large"),
Self::VersionMismatch => f.write_str("version mismatch"),
}
}
}
impl Error for DecodeError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RecvError<Des, Io> {
Decode(DecodeError),
Io(Io),
Serde(Des),
}
impl<Des: Display, Io: Display> Display for RecvError<Des, Io> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decode(e) => Display::fmt(e, f),
Self::Io(e) => Display::fmt(e, f),
Self::Serde(e) => Display::fmt(e, f),
}
}
}
impl<Des, Io> Error for RecvError<Des, Io> where Self: Debug + Display {}
impl<Des, Io> From<FramedReadError<DecodeError, Io>>
for RecvError<Des, Io>
{
fn from(value: FramedReadError<DecodeError, Io>) -> Self {
match value {
FramedReadError::Decode(x) => Self::Decode(x),
FramedReadError::Io(x) => Self::Io(x),
}
}
}