Skip to main content

tape_base64/
error.rs

1//! What a conversion refuses, and where
2
3use core::fmt;
4
5/// Why an encode could not be written
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum EncodeError {
8    /// The output buffer is shorter than the encoding needs
9    BufferTooSmall {
10        /// Bytes the encoding needs
11        needed: usize,
12    },
13    /// The encoding of an input this long does not fit in a `usize`
14    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/// Why a decode could not be completed
29#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30pub enum DecodeError {
31    /// A byte that is not in the alphabet, at this offset in the input
32    InvalidByte {
33        /// Offset of the byte in the input
34        position: usize,
35        /// The byte itself
36        byte: u8,
37    },
38    /// A character count that no encoding can produce
39    InvalidLength {
40        /// The length that was refused
41        length: usize,
42    },
43    /// The final character has bits that the decoded output cannot hold
44    InvalidLastSymbol {
45        /// Offset of the character in the input
46        position: usize,
47        /// The character itself
48        byte: u8,
49    },
50    /// Padding that is absent, too long, or not at the end
51    InvalidPadding {
52        /// Offset at which the padding went wrong
53        position: usize,
54    },
55    /// The output buffer is shorter than the decoding needs
56    BufferTooSmall {
57        /// Bytes the decoding needs
58        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 {}