use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
at: usize,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
End,
Expected(&'static str),
ControlCharacter,
Escape,
Surrogate,
Utf8,
Number,
TooDeep,
Trailing,
}
impl Error {
pub(crate) fn new(kind: ErrorKind, at: usize) -> Self {
Error { kind, at }
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn position(&self) -> usize {
self.at
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let what = match self.kind {
ErrorKind::End => "the input ended inside a value",
ErrorKind::Expected(what) => return write!(f, "expected {what} at byte {}", self.at),
ErrorKind::ControlCharacter => "a control character in a string, which JSON needs escaped,",
ErrorKind::Escape => "a bad escape",
ErrorKind::Surrogate => "half of a surrogate pair",
ErrorKind::Utf8 => "bytes that are not UTF-8",
ErrorKind::Number => "a number outside JSON's grammar",
ErrorKind::TooDeep => "arrays and objects nested deeper than the limit",
ErrorKind::Trailing => "bytes after the value",
};
write!(f, "{what} at byte {}", self.at)
}
}
impl core::error::Error for Error {}