Skip to content

Commit

Permalink
Removed thiserror
Browse files Browse the repository at this point in the history
The crate now has no dependencies.
  • Loading branch information
ecton committed Jan 22, 2023
1 parent 4811eaf commit 70765d9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ readme = "./README.md"
rust-version = "1.58"

[dependencies]
thiserror = "1.0.38"

[workspace]
members = ["benchmarks", "xtask"]
Expand Down
95 changes: 66 additions & 29 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::Infallible;
use std::fmt::Display;

/// A JSON parsing error with location information.
#[derive(thiserror::Error, Debug, PartialEq)]
#[error("error at {offset}: {kind}")]
#[derive(Debug, PartialEq)]
pub struct Error<DelegateError = Infallible> {
pub(crate) offset: usize,
pub(crate) kind: ErrorKind<DelegateError>,
Expand All @@ -23,6 +23,20 @@ impl<DelegateError> Error<DelegateError> {
}
}

impl<DelegateError> std::error::Error for Error<DelegateError> where
DelegateError: std::fmt::Debug + Display
{
}

impl<DelegateError> Display for Error<DelegateError>
where
DelegateError: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error at {}: {}", self.offset, self.kind)
}
}

impl Error<Infallible> {
#[must_use]
pub(crate) fn into_fallable<DelegateError>(self) -> Error<DelegateError> {
Expand All @@ -34,94 +48,68 @@ impl Error<Infallible> {
}

/// An error from parsing JSON.
#[derive(thiserror::Error, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind<DelegateError = Infallible> {
/// An invalid UTF-8 sequence was encountered.
#[error("invalid utf-8")]
Utf8,
/// An end-of-file was encountered when it wasn't expected.
#[error("unexpected end of file")]
UnexpectedEof,
// #[error("the maximum_string_length has been exceeded")]
// TemporaryBufferCapacityExceeded,
/// While parsing an object, something was encountered that was not a valid object key.
#[error("expected object key")]
ExpectedObjectKey,
/// While parsing an object, a colon was expected after an object's key.
#[error("expected colon (':')")]
ExpectedColon,
/// A [`Value`][crate::Value] was expected.
#[error("expected value")]
ExpectedValue,
/// While parsing an object, either the end of an object (`}`) or a comma
/// was expected.
#[error("expected comma or end of object")]
ExpectedCommaOrEndOfObject,
/// While parsing an array, either the end of an array (`]`) or a comma was
/// expected.
#[error("expected comma or end of array")]
ExpectedCommaOrEndOfArray,
/// When parsing an object or an array, a trailing comma was detected. The
/// JSON specification disallows trailing commas.
#[error("trailing commas are not allowed")]
IllegalTrailingComma,
/// An unexpected character was encountered while parsing a
/// [`Value`][crate::Value].
#[error("unexpected character: '{0}'")]
Unexpected(u8),
/// The source being parsed contained additional non-whitespace data after a
/// [`Value`][crate::Value] was parsed.
#[error("trailing non-whitespace data was encountered")]
TrailingNonWhitespace,
/// A non-string was encountered for an object key. The JSON standard
/// requires all keys to be strings.
#[error("object keys must be strings")]
ObjectKeysMustBeStrings,
/// An exponent was expected on a floating point number.
#[error("expected exponent sign or digit for number literal")]
ExpectedExponent,
/// At least one decimal digit was expected on a floating point number.
#[error("expected decimal digit for number literal")]
ExpectedDecimalDigit,
/// At least one integer digit was expected on a floating point number.
#[error("expected decimal digit for number literal")]
ExpectedDigit,
/// An invalid hexadecimal character was encountered in a unicode escape
/// sequence in a string.
#[error("invalid hexadecimal in unicode escape sequence")]
InvalidHexadecimal,
/// An invalid character was escaped.
#[error("invalid escaped character")]
InvalidEscape,
/// An object is missing its end (`}`).
#[error("expected end of object ('}}')")]
UnclosedObject,
/// An array is missing its end (`]`).
#[error("expected end of array (']')")]
UnclosedArray,
/// A string is missing its end (`"`).
#[error("expected end of string ('\"')")]
UnclosedString,
/// A string was expected, but another type was found.
#[error("expected a string")]
ExpectedString,
/// A number was expected, but another type was found.
#[error("expected a number")]
ExpectedNumber,
/// The JSON being parsed has more depth than the parser was configured to
/// allow.
#[error("the recursion limit has been reached")]
RecursionLimitReached,
/// A value that wasn't an object or array was contained in a JSON payload.
///
/// This error is only returned when the `allow_all_types_at_root`
/// configuration is set to `false`.
#[error("the recursion limit has been reached")]
PayloadsShouldBeObjectOrArray,
/// An error was returned from a
/// [`ParseDelegate`](crate::parser::ParseDelegate).
#[error("error from delegate: {0}")]
ErrorFromDelegate(DelegateError),
}

Expand Down Expand Up @@ -155,3 +143,52 @@ impl ErrorKind<Infallible> {
}
}
}

impl<DelegateError> std::error::Error for ErrorKind<DelegateError> where
DelegateError: std::fmt::Debug + Display
{
}

impl<DelegateError> Display for ErrorKind<DelegateError>
where
DelegateError: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::Utf8 => f.write_str("invalid utf-8"),
ErrorKind::UnexpectedEof => f.write_str("unexpected end of file"),
ErrorKind::ExpectedObjectKey => f.write_str("expected object key"),
ErrorKind::ExpectedColon => f.write_str("expected colon (':')"),
ErrorKind::ExpectedValue => f.write_str("expected value"),
ErrorKind::ExpectedCommaOrEndOfObject => f.write_str("expected comma or end of object"),
ErrorKind::ExpectedCommaOrEndOfArray => f.write_str("expected comma or end of array"),
ErrorKind::IllegalTrailingComma => f.write_str("trailing commas are not allowed"),
ErrorKind::Unexpected(ch) => write!(f, "unexpected character: '{ch}'"),
ErrorKind::TrailingNonWhitespace => {
f.write_str("trailing non-whitespace data was encountered")
}
ErrorKind::ObjectKeysMustBeStrings => f.write_str("object keys must be strings"),
ErrorKind::ExpectedExponent => {
f.write_str("expected exponent sign or digit for number literal")
}
ErrorKind::ExpectedDecimalDigit => {
f.write_str("expected decimal digit for number literal")
}
ErrorKind::ExpectedDigit => f.write_str("expected decimal digit for number literal"),
ErrorKind::InvalidHexadecimal => {
f.write_str("invalid hexadecimal in unicode escape sequence")
}
ErrorKind::InvalidEscape => f.write_str("invalid escaped character"),
ErrorKind::UnclosedObject => f.write_str("expected end of object ('}}')"),
ErrorKind::UnclosedArray => f.write_str("expected end of array (']')"),
ErrorKind::UnclosedString => f.write_str("expected end of string ('\"')"),
ErrorKind::ExpectedString => f.write_str("expected a string"),
ErrorKind::ExpectedNumber => f.write_str("expected a number"),
ErrorKind::RecursionLimitReached => f.write_str("the recursion limit has been reached"),
ErrorKind::PayloadsShouldBeObjectOrArray => {
f.write_str("JSON only allows objects or arrays at the root")
}
ErrorKind::ErrorFromDelegate(err) => write!(f, "error from delegate: {err}"),
}
}
}

0 comments on commit 70765d9

Please sign in to comment.