use std::fmt;
#[derive(Debug)]
pub enum ErrorType {
BadKeyType,
EarlyEnd,
ExpectedArray,
ExpectedArrayComma,
ExpectedBoolean,
ExpectedEnum,
ExpectedFloat,
ExpectedInteger,
ExpectedMap,
ExpectedObjectColon,
ExpectedMapComma,
ExpectedMapEnd,
ExpectedNull,
ExpectedNumber,
ExpectedSigned,
ExpectedString,
ExpectedUnsigned,
InternalError,
InvalidEscape,
InvalidExponent,
InvalidNumber,
InvalidUTF8,
InvalidUnicodeEscape,
InvlaidUnicodeCodepoint,
KeyMustBeAString,
NoStructure,
Parser,
EOF,
Serde(String),
Syntax,
TrailingCharacters,
UnexpectedCharacter,
UnexpectedEnd,
UnterminatedString,
ExpectedArrayContent,
ExpectedObjectContent,
ExpectedObjectKey,
Overflow,
IO(std::io::Error),
}
#[cfg_attr(tarpaulin, skip)]
impl PartialEq for ErrorType {
#[must_use]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::IO(_), Self::IO(_))
| (Self::BadKeyType, Self::BadKeyType)
| (Self::EarlyEnd, Self::EarlyEnd)
| (Self::ExpectedArray, Self::ExpectedArray)
| (Self::ExpectedArrayComma, Self::ExpectedArrayComma)
| (Self::ExpectedBoolean, Self::ExpectedBoolean)
| (Self::ExpectedEnum, Self::ExpectedEnum)
| (Self::ExpectedFloat, Self::ExpectedFloat)
| (Self::ExpectedInteger, Self::ExpectedInteger)
| (Self::ExpectedMap, Self::ExpectedMap)
| (Self::ExpectedObjectColon, Self::ExpectedObjectColon)
| (Self::ExpectedMapComma, Self::ExpectedMapComma)
| (Self::ExpectedMapEnd, Self::ExpectedMapEnd)
| (Self::ExpectedNull, Self::ExpectedNull)
| (Self::ExpectedNumber, Self::ExpectedNumber)
| (Self::ExpectedSigned, Self::ExpectedSigned)
| (Self::ExpectedString, Self::ExpectedString)
| (Self::ExpectedUnsigned, Self::ExpectedUnsigned)
| (Self::InternalError, Self::InternalError)
| (Self::InvalidEscape, Self::InvalidEscape)
| (Self::InvalidExponent, Self::InvalidExponent)
| (Self::InvalidNumber, Self::InvalidNumber)
| (Self::InvalidUTF8, Self::InvalidUTF8)
| (Self::InvalidUnicodeEscape, Self::InvalidUnicodeEscape)
| (Self::InvlaidUnicodeCodepoint, Self::InvlaidUnicodeCodepoint)
| (Self::KeyMustBeAString, Self::KeyMustBeAString)
| (Self::NoStructure, Self::NoStructure)
| (Self::Parser, Self::Parser)
| (Self::EOF, Self::EOF)
| (Self::Syntax, Self::Syntax)
| (Self::TrailingCharacters, Self::TrailingCharacters)
| (Self::UnexpectedCharacter, Self::UnexpectedCharacter)
| (Self::UnexpectedEnd, Self::UnexpectedEnd)
| (Self::UnterminatedString, Self::UnterminatedString)
| (Self::ExpectedArrayContent, Self::ExpectedArrayContent)
| (Self::ExpectedObjectContent, Self::ExpectedObjectContent)
| (Self::ExpectedObjectKey, Self::ExpectedObjectKey)
| (Self::Overflow, Self::Overflow) => true,
(Self::Serde(s1), Self::Serde(s2)) => s1 == s2,
_ => false,
}
}
}
#[derive(Debug, PartialEq)]
pub struct Error {
index: usize,
character: char,
error: ErrorType,
}
impl Error {
pub(crate) fn new(index: usize, character: char, error: ErrorType) -> Self {
Self {
index,
character,
error,
}
}
pub(crate) fn generic(t: ErrorType) -> Self {
Self {
index: 0,
character: '💩',
error: t,
}
}
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:?} at character {} ('{}')",
self.error, self.index, self.character
)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn fmt() {
let e = Error::generic(ErrorType::InternalError);
assert_eq!(
format!("{}", e),
"InternalError at character 0 ('\u{1f4a9}')"
)
}
}