zjson/string/
error.rs

1use core::fmt;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4/// The error returned when parsing a [`String`](super::String) fails.
5pub enum ParseStringError {
6    /// The JSON string ended before the string was terminated.
7    UnexpectedEnd,
8    /// An invalid character was escaped.
9    InvalidEscape(char),
10    /// A non-hex character was used in a unicode escape.
11    InvalidUnicodeEscape(char),
12    /// A low surrogate was found that was not prefixed by a high surrogate.
13    MissingHighSurrogate {
14        /// The low surrogate found.
15        low: u16,
16    },
17    /// A high surrogate was found that was not followed by a low surrogate.
18    MissingLowSurrogate {
19        /// The high surrogate found.
20        high: u16,
21    },
22    /// A unicode escape sequence was found after a high surrogate but it was not a valid low surrogate.
23    InvalidLowSurrogate {
24        /// The high surrogate found.
25        high: u16,
26        /// The unicode escape sequence.
27        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 {}