1use core::fmt;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4pub enum ParseStringError {
6 UnexpectedEnd,
8 InvalidEscape(char),
10 InvalidUnicodeEscape(char),
12 MissingHighSurrogate {
14 low: u16,
16 },
17 MissingLowSurrogate {
19 high: u16,
21 },
22 InvalidLowSurrogate {
24 high: u16,
26 low: u16,
28 },
29}
30
31impl fmt::Display for ParseStringError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::UnexpectedEnd => {
35 write!(f, "Unexpected end of JSON string (missing \")!")
36 }
37 Self::InvalidEscape(c) => write!(f, "Invalid escape character ({c}) in JSON string!"),
38 Self::InvalidUnicodeEscape(c) => {
39 write!(
40 f,
41 "Invalid character ({c}) in unicode escape in JSON string!"
42 )
43 }
44 Self::MissingHighSurrogate { low } => {
45 write!(
46 f,
47 "Found a low surrogate (\\u{low:0>4x}) not prefixed with a high surrogate!"
48 )
49 }
50 Self::MissingLowSurrogate { high } => {
51 write!(
52 f,
53 "Found a high surrogate (\\u{high:0>4x}) not followed by a low surrogate!"
54 )
55 }
56 Self::InvalidLowSurrogate { high, low } => {
57 write!(f, "Invalid low surrogate (\\u{low:0>4x}) after a high surrogate (\\u{high:0>4x}!)")
58 }
59 }
60 }
61}
62
63#[cfg(feature = "std")]
64impl std::error::Error for ParseStringError {}