1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! What a conversion refuses, and where
use core::fmt;
/// Why an encode could not be written
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EncodeError {
/// The output buffer is shorter than the encoding needs
BufferTooSmall {
/// Bytes the encoding needs
needed: usize,
},
/// The encoding of an input this long does not fit in a `usize`
TooLong,
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EncodeError::BufferTooSmall { needed } => {
write!(f, "output buffer too small, needs {needed} bytes")
}
EncodeError::TooLong => f.write_str("input too long to encode"),
}
}
}
/// Why a decode could not be completed
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DecodeError {
/// A byte that is not in the alphabet, at this offset in the input
InvalidByte {
/// Offset of the byte in the input
position: usize,
/// The byte itself
byte: u8,
},
/// A character count that no encoding can produce
InvalidLength {
/// The length that was refused
length: usize,
},
/// The final character has bits that the decoded output cannot hold
InvalidLastSymbol {
/// Offset of the character in the input
position: usize,
/// The character itself
byte: u8,
},
/// Padding that is absent, too long, or not at the end
InvalidPadding {
/// Offset at which the padding went wrong
position: usize,
},
/// The output buffer is shorter than the decoding needs
BufferTooSmall {
/// Bytes the decoding needs
needed: usize,
},
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DecodeError::InvalidByte { position, byte } => {
write!(f, "invalid byte {byte:#04x} at offset {position}")
}
DecodeError::InvalidLength { length } => {
write!(f, "invalid encoded length {length}")
}
DecodeError::InvalidLastSymbol { position, byte } => {
write!(
f,
"symbol {byte:#04x} at offset {position} has bits to spare"
)
}
DecodeError::InvalidPadding { position } => {
write!(f, "invalid padding at offset {position}")
}
DecodeError::BufferTooSmall { needed } => {
write!(f, "output buffer too small, needs {needed} bytes")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for EncodeError {}
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}