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
//! The result types reported by the streaming [`Decoder`](crate::Decoder) and
//! [`Encoder`](crate::Encoder).
/// Why a conversion that substitutes errors stopped.
///
/// Both variants are ordinary control flow: `InputEmpty` means the caller should
/// supply more input, `OutputFull` means it should drain the output buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CoderResult {
/// All of the input was consumed.
InputEmpty,
/// The output buffer ran out of room before the input was consumed.
OutputFull,
}
/// Why a decode that reports errors stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DecoderResult {
/// All of the input was consumed.
InputEmpty,
/// The output buffer ran out of room before the input was consumed.
OutputFull,
/// A malformed byte sequence of the given length was found and consumed.
///
/// The bytes are counted from the start of the malformed sequence, which may
/// have begun in an earlier call, so the length can exceed the number of bytes
/// read by the call that reports it. Decoding continues from the byte after
/// the sequence; a caller substituting errors writes one U+FFFD per report.
Malformed(u8),
}
/// Why an encode that reports errors stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EncoderResult {
/// All of the input was consumed.
InputEmpty,
/// The output buffer ran out of room before the input was consumed.
OutputFull,
/// The given character has no representation in the target encoding, and was
/// consumed without producing output.
Unmappable(char),
}
impl DecoderResult {
/// Converts to a [`CoderResult`], panicking on `Malformed`.
pub(crate) fn as_coder_result(self) -> CoderResult {
match self {
DecoderResult::InputEmpty => CoderResult::InputEmpty,
DecoderResult::OutputFull => CoderResult::OutputFull,
DecoderResult::Malformed(_) => unreachable!("errors are handled by the caller"),
}
}
}
impl EncoderResult {
pub(crate) fn as_coder_result(self) -> CoderResult {
match self {
EncoderResult::InputEmpty => CoderResult::InputEmpty,
EncoderResult::OutputFull => CoderResult::OutputFull,
EncoderResult::Unmappable(_) => unreachable!("errors are handled by the caller"),
}
}
}