1use core::fmt;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum EncodeError {
8 BufferTooSmall {
10 needed: usize,
12 },
13 TooLong,
15}
16
17impl fmt::Display for EncodeError {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 match self {
20 EncodeError::BufferTooSmall { needed } => {
21 write!(f, "output buffer too small, needs {needed} bytes")
22 }
23 EncodeError::TooLong => f.write_str("input too long to encode"),
24 }
25 }
26}
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30pub enum DecodeError {
31 InvalidByte {
33 position: usize,
35 byte: u8,
37 },
38 InvalidLength {
40 length: usize,
42 },
43 InvalidLastSymbol {
45 position: usize,
47 byte: u8,
49 },
50 InvalidPadding {
52 position: usize,
54 },
55 BufferTooSmall {
57 needed: usize,
59 },
60}
61
62impl fmt::Display for DecodeError {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 match self {
65 DecodeError::InvalidByte { position, byte } => {
66 write!(f, "invalid byte {byte:#04x} at offset {position}")
67 }
68 DecodeError::InvalidLength { length } => {
69 write!(f, "invalid encoded length {length}")
70 }
71 DecodeError::InvalidLastSymbol { position, byte } => {
72 write!(
73 f,
74 "symbol {byte:#04x} at offset {position} has bits to spare"
75 )
76 }
77 DecodeError::InvalidPadding { position } => {
78 write!(f, "invalid padding at offset {position}")
79 }
80 DecodeError::BufferTooSmall { needed } => {
81 write!(f, "output buffer too small, needs {needed} bytes")
82 }
83 }
84 }
85}
86
87#[cfg(feature = "std")]
88impl std::error::Error for EncodeError {}
89
90#[cfg(feature = "std")]
91impl std::error::Error for DecodeError {}