use std::fmt;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum JsonLexicalErrorReason {
UnexpectedEnd,
UnexpectedByte,
ExpectedColon,
ExpectedCommaOrArrayEnd,
ExpectedCommaOrObjectEnd,
ExpectedObjectKey,
InvalidEscape,
InvalidUnicodeEscape,
UnpairedSurrogate,
InvalidUtf8,
InvalidNumber,
IntegerOutOfRange,
FloatOutOfRange,
TrailingCharacters,
NestingOverflow,
}
impl fmt::Display for JsonLexicalErrorReason {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEnd => formatter.write_str("unexpected end of input"),
Self::UnexpectedByte => formatter.write_str("unexpected byte"),
Self::ExpectedColon => formatter.write_str("expected ':'"),
Self::ExpectedCommaOrArrayEnd => formatter.write_str("expected ',' or ']' in array"),
Self::ExpectedCommaOrObjectEnd => formatter.write_str("expected ',' or '}' in object"),
Self::ExpectedObjectKey => formatter.write_str("expected object key"),
Self::InvalidEscape => formatter.write_str("invalid string escape"),
Self::InvalidUnicodeEscape => formatter.write_str("invalid Unicode escape"),
Self::UnpairedSurrogate => formatter.write_str("unpaired Unicode surrogate"),
Self::InvalidUtf8 => formatter.write_str("invalid UTF-8"),
Self::InvalidNumber => formatter.write_str("invalid JSON number"),
Self::IntegerOutOfRange => formatter.write_str("JSON integer is outside the supported 64-bit range"),
Self::FloatOutOfRange => formatter.write_str("JSON number is outside the finite f64 range"),
Self::TrailingCharacters => formatter.write_str("trailing characters"),
Self::NestingOverflow => formatter.write_str("JSON nesting overflow"),
}
}
}
#[cfg(test)]
mod tests {
use super::JsonLexicalErrorReason;
#[test]
fn test_json_lexical_error_reason_formats_every_variant() {
let cases = [
(JsonLexicalErrorReason::UnexpectedEnd, "unexpected end of input"),
(JsonLexicalErrorReason::UnexpectedByte, "unexpected byte"),
(JsonLexicalErrorReason::ExpectedColon, "expected ':'"),
(
JsonLexicalErrorReason::ExpectedCommaOrArrayEnd,
"expected ',' or ']' in array",
),
(
JsonLexicalErrorReason::ExpectedCommaOrObjectEnd,
"expected ',' or '}' in object",
),
(JsonLexicalErrorReason::ExpectedObjectKey, "expected object key"),
(JsonLexicalErrorReason::InvalidEscape, "invalid string escape"),
(JsonLexicalErrorReason::InvalidUnicodeEscape, "invalid Unicode escape"),
(JsonLexicalErrorReason::UnpairedSurrogate, "unpaired Unicode surrogate"),
(JsonLexicalErrorReason::InvalidUtf8, "invalid UTF-8"),
(JsonLexicalErrorReason::InvalidNumber, "invalid JSON number"),
(
JsonLexicalErrorReason::IntegerOutOfRange,
"JSON integer is outside the supported 64-bit range",
),
(
JsonLexicalErrorReason::FloatOutOfRange,
"JSON number is outside the finite f64 range",
),
(JsonLexicalErrorReason::TrailingCharacters, "trailing characters"),
(JsonLexicalErrorReason::NestingOverflow, "JSON nesting overflow"),
];
for (reason, expected) in cases {
assert_eq!(reason.to_string(), expected);
}
}
}