utf8_parser/
error.rs

1use core::fmt;
2
3/// Error type used for the `utf8-parser` crate
4#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5pub enum Utf8ParserError {
6    /// Encountered an invalid byte. This is a byte that's invalid no matter the context.
7    InvalidByte(u8),
8    /// Found a code point that's not valid UTF-8.
9    InvalidChar(u32),
10    /// Found a start byte in an unexpected place
11    UnexpectedStartByte(u8),
12    /// Found a continuation byte in an unexpected place
13    UnexpectedContinuationByte(u8),
14    /// Represented a valid code point in a longer form than necessary.
15    ///
16    /// From Wikipedia:
17    /// > The standard specifies that the correct encoding of a code point uses only the minimum
18    /// > number of bytes required to hold the significant bits of the code point. Longer encodings
19    /// > are called overlong and are not valid UTF-8 representations of the code point. This rule
20    /// > maintains a one-to-one correspondence between code points and their valid encodings, so
21    /// > that there is a unique valid encoding for each code point. This ensures that string
22    /// > comparisons and searches are well-defined.
23    OverlongEncoding,
24}
25
26impl fmt::Display for Utf8ParserError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::InvalidByte(byte) => {
30                write!(f, "Found invalid byte: 0x{byte:02x}")
31            }
32            Self::InvalidChar(word) => {
33                write!(f, "Parsed invalid UTF-8 code point: 0x{word:04x}")
34            }
35            Self::UnexpectedStartByte(byte) => {
36                write!(
37                    f,
38                    "Found start byte when a continuation byte was expected: 0x{byte:02x}"
39                )
40            }
41            Self::UnexpectedContinuationByte(byte) => {
42                write!(
43                    f,
44                    "Found continuation byte when a start byte was expected: 0x{byte:02x}"
45                )
46            }
47            Self::OverlongEncoding => {
48                write!(f, "Found overlong encoding")
49            }
50        }
51    }
52}
53
54impl core::error::Error for Utf8ParserError {}