use std::{fmt, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JsonDecodeErrorKind {
InputTooLarge,
EmptyInput,
InvalidJson,
UnexpectedTopLevel,
Deserialize,
}
impl fmt::Display for JsonDecodeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Self::InputTooLarge => "input_too_large",
Self::EmptyInput => "empty_input",
Self::InvalidJson => "invalid_json",
Self::UnexpectedTopLevel => "unexpected_top_level",
Self::Deserialize => "deserialize",
};
f.write_str(name)
}
}
impl FromStr for JsonDecodeErrorKind {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if value.eq_ignore_ascii_case("input_too_large") {
Ok(Self::InputTooLarge)
} else if value.eq_ignore_ascii_case("empty_input") {
Ok(Self::EmptyInput)
} else if value.eq_ignore_ascii_case("invalid_json") {
Ok(Self::InvalidJson)
} else if value.eq_ignore_ascii_case("unexpected_top_level") {
Ok(Self::UnexpectedTopLevel)
} else if value.eq_ignore_ascii_case("deserialize") {
Ok(Self::Deserialize)
} else {
Err("unknown JsonDecodeErrorKind")
}
}
}