use alloc::boxed::Box;
use alloc::string::{String, ToString};
use nom::error::ErrorKind;
use nom::Input;
#[cfg(feature = "serde")]
use serde_core::de;
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
pub enum DeserializeError<I: Input + Debug + Display> {
Message(String),
ParseError(Option<I>, ErrorKind),
WrappedParseError(Box<Self>, Option<I>, ErrorKind),
ConversionError(String),
ExpectedInteger,
EmptyString,
ExpectedString,
ExpectedList,
ExpectedListOrNull,
ExpectedListOrString,
ExpectedAnnotatedList,
UnexpectedNamedList,
NotSupported,
OutOfOrder,
}
impl<I: Input + Debug + Display> core::error::Error for DeserializeError<I> {}
#[cfg(feature = "serde")]
impl<I: Input + Debug + Display> de::Error for DeserializeError<I> {
fn custom<T: Display>(msg: T) -> Self {
DeserializeError::Message(msg.to_string())
}
}
impl<I: Input + Debug + Display> nom::error::ParseError<I> for DeserializeError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
DeserializeError::ParseError(Some(input), kind)
}
fn append(input: I, kind: ErrorKind, other: DeserializeError<I>) -> DeserializeError<I> {
DeserializeError::WrappedParseError(Box::new(other), Some(input), kind)
}
}
impl<I: Input + Debug + Display> Display for DeserializeError<I> {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
DeserializeError::Message(msg) => write!(formatter, "serde message: {}", msg),
DeserializeError::ParseError(input, kind) => {
match input {
None => write!(formatter, "parse error: {}", kind.description()),
Some(input) => write!(formatter, "parse error: {} for input {}", kind.description(), input)
}
},
DeserializeError::WrappedParseError(err, input, kind) => {
match input {
None => write!(formatter, "parse error: {} (wrapped: {})", kind.description(), err.to_string()),
Some(input) => write!(formatter, "parse error: {} for input {} (wrapped: {})", kind.description(), input, err.to_string())
}
},
DeserializeError::ConversionError(err) => write!(formatter, "type conversion error: {}", err),
DeserializeError::ExpectedInteger => write!(formatter, "expected integer"),
DeserializeError::EmptyString => write!(formatter, "empty string"),
DeserializeError::ExpectedString => write!(formatter, "expected string"),
DeserializeError::ExpectedList => write!(formatter, "expected list"),
DeserializeError::ExpectedListOrNull => write!(formatter, "expected list or null"),
DeserializeError::ExpectedListOrString => write!(formatter, "expected list or string"),
DeserializeError::ExpectedAnnotatedList => write!(formatter, "expected annotated list"),
DeserializeError::UnexpectedNamedList => write!(formatter, "unexpected named list"),
DeserializeError::NotSupported => write!(formatter, "not supported"),
DeserializeError::OutOfOrder => write!(formatter, "deserialization functions invoked out of order")
}
}
}
impl<I: Input + Debug + Display> DeserializeError<I> {
pub fn map_to_conversion_error<E: Display>(e: E) -> Self {
DeserializeError::ConversionError(e.to_string())
}
}