use core::fmt;
use alloc::string::{String, ToString};
#[derive(Debug, Clone)]
enum ErrorKind {
UnknownField(String),
Custom(String),
}
#[derive(Debug, Clone)]
pub struct Error(ErrorKind);
impl Error {
pub fn unknown_field<I>(field: I) -> Self
where
I: Into<String>,
{
Self(ErrorKind::UnknownField(field.into()))
}
pub fn custom<T>(msg: T) -> Self
where
T: fmt::Display,
{
Self(ErrorKind::Custom(msg.to_string()))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
ErrorKind::UnknownField(ref field) => {
f.write_str("unknown field '")?;
f.write_str(field)?;
f.write_str("'")?;
Ok(())
}
ErrorKind::Custom(ref x) => f.write_str(x),
}
}
}
impl core::error::Error for Error {}