use core::fmt;
use calandria::RetainedBytes;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum FrameDecodeError<E> {
DecoderFailed,
RetainedByteCapacity {
retained: RetainedBytes,
incoming: usize,
limit: RetainedBytes,
},
InputSizeOverflow,
Decoder(E),
RetainedContractViolation {
retained: RetainedBytes,
limit: RetainedBytes,
},
}
impl<E: fmt::Display> fmt::Display for FrameDecodeError<E> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DecoderFailed => formatter.write_str("the frame decoder has already failed"),
Self::RetainedByteCapacity { .. } => {
formatter.write_str("input would exceed decoder retained-byte capacity")
}
Self::InputSizeOverflow => {
formatter.write_str("input length exceeds the retained-byte accounting domain")
}
Self::Decoder(error) => error.fmt(formatter),
Self::RetainedContractViolation { .. } => {
formatter.write_str("decoder violated its retained-byte reporting contract")
}
}
}
}
impl<E: core::error::Error + 'static> core::error::Error for FrameDecodeError<E> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Decoder(error) => Some(error),
_ => None,
}
}
}