Skip to main content

planck_pack_core/
error.rs

1use core::fmt;
2
3/// Errors that can occur during decoding.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum DecodeError {
6    /// The ordinal value exceeds the type's radix.
7    OrdinalOutOfRange { ordinal: u128, radix: u128 },
8    /// After decoding all fields, remaining ordinal was nonzero.
9    ExcessData,
10    /// Input bytes are too short.
11    InsufficientData { expected: usize, got: usize },
12}
13
14impl fmt::Display for DecodeError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            DecodeError::OrdinalOutOfRange { ordinal, radix } => {
18                write!(f, "ordinal {ordinal} out of range for radix {radix}")
19            }
20            DecodeError::ExcessData => {
21                write!(f, "excess data after decoding all fields")
22            }
23            DecodeError::InsufficientData { expected, got } => {
24                write!(f, "insufficient data: expected {expected} bytes, got {got}")
25            }
26        }
27    }
28}
29
30#[cfg(feature = "std")]
31impl std::error::Error for DecodeError {}