use core::fmt;
use crate::string;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ParseObjectError {
UnexpectedEnd,
ExpectedName {
c: char,
or_end: bool,
},
InvalidName(string::ParseStringError),
ExpectedColon(char),
InvalidElement(char),
ExpectedCommaOrEnd(char),
TrailingComma,
}
impl fmt::Display for ParseObjectError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEnd => write!(f, "Unexpected end of JSON object!"),
Self::ExpectedName { c, or_end: true } => write!(
f,
"Invalid character ({c}) in JSON object (expected a string name or an end, '}}')!"
),
Self::ExpectedName { c, or_end: false } => write!(
f,
"Invalid character ({c}) in JSON object (expected a string name)!"
),
Self::InvalidName(err) => err.fmt(f),
Self::ExpectedColon(c) => {
write!(
f,
"Invalid character ({c}) in JSON object (expected a colon, ':')!"
)
}
Self::InvalidElement(c) => write!(
f,
"Invalid character ({c}) in JSON object (expected a value)!"
),
Self::ExpectedCommaOrEnd(c) => write!(
f,
"Invalid character ({c}) in JSON object (expected a comma or an end, '}}')!"
),
Self::TrailingComma => write!(f, "Trailing comma in JSON object!"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseObjectError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Self::InvalidName(err) = self {
Some(err)
} else {
None
}
}
}