Skip to main content

bornera_core/framing/
error.rs

1//! Recoverable admission rejection and terminal decoder failures.
2
3use core::fmt;
4
5use calandria::RetainedBytes;
6
7/// Why a bounded decoder driver could not admit or decode bytes.
8#[derive(Clone, Debug, Eq, PartialEq)]
9#[non_exhaustive]
10pub enum FrameDecodeError<E> {
11    /// The decoder already observed a terminal adapter or accounting failure.
12    DecoderFailed,
13    /// The borrowed input chunk could not fit without exceeding retained bytes.
14    RetainedByteCapacity {
15        /// Bytes retained before the rejected input.
16        retained: RetainedBytes,
17        /// Bytes in the rejected borrowed input.
18        incoming: usize,
19        /// Configured retained-byte maximum.
20        limit: RetainedBytes,
21    },
22    /// The platform input length could not fit the fixed-width byte domain.
23    InputSizeOverflow,
24    /// The protocol adapter reported a terminal decoder failure.
25    Decoder(E),
26    /// The adapter retained more memory than the configured mechanical bound.
27    RetainedContractViolation {
28        /// Retained bytes reported by the adapter.
29        retained: RetainedBytes,
30        /// Configured retained-byte maximum.
31        limit: RetainedBytes,
32    },
33}
34
35impl<E: fmt::Display> fmt::Display for FrameDecodeError<E> {
36    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::DecoderFailed => formatter.write_str("the frame decoder has already failed"),
39            Self::RetainedByteCapacity { .. } => {
40                formatter.write_str("input would exceed decoder retained-byte capacity")
41            }
42            Self::InputSizeOverflow => {
43                formatter.write_str("input length exceeds the retained-byte accounting domain")
44            }
45            Self::Decoder(error) => error.fmt(formatter),
46            Self::RetainedContractViolation { .. } => {
47                formatter.write_str("decoder violated its retained-byte reporting contract")
48            }
49        }
50    }
51}
52
53impl<E: core::error::Error + 'static> core::error::Error for FrameDecodeError<E> {
54    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
55        match self {
56            Self::Decoder(error) => Some(error),
57            _ => None,
58        }
59    }
60}