use nom::Err;
use alloc::borrow::ToOwned;
use alloc::string::String;
use core::fmt::{self, Display, Formatter};
#[derive(Debug)]
pub enum Error {
ParseError(String),
SerdeError(String),
UnknownError,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Error::ParseError(msg) => f.write_str(msg),
Error::SerdeError(msg) => f.write_str(msg),
Error::UnknownError => f.write_str("Unknown Error"),
}
}
}
#[cfg(feature = "std")]
impl alloc::error::Error for Error {}
#[cfg(feature = "deserialize")]
impl serde::de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::SerdeError(msg.to_string())
}
}
impl From<Err<nom::error::Error<&str>>> for Error {
fn from(err: Err<nom::error::Error<&str>>) -> Self {
match err {
Err::Incomplete(_) => Error::UnknownError,
Err::Error(e) => Error::ParseError(e.code.description().to_owned()),
Err::Failure(e) => Error::ParseError(e.code.description().to_owned()),
}
}
}