use std::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Message(String),
ExpectedEquals,
UnterminatedQuote,
InvalidEscape(char),
ParseInt(String),
ParseFloat(String),
ParseBool(String),
ParseChar(String),
TopLevelNotMap,
Unsupported(&'static str),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Message(msg) => f.write_str(msg),
Error::ExpectedEquals => f.write_str("expected `=` separator in key/value pair"),
Error::UnterminatedQuote => f.write_str("unterminated quoted value"),
Error::InvalidEscape(c) => write!(f, "invalid escape sequence `\\{c}`"),
Error::ParseInt(t) => write!(f, "invalid integer `{t}`"),
Error::ParseFloat(t) => write!(f, "invalid float `{t}`"),
Error::ParseBool(t) => write!(f, "invalid bool `{t}` (expected `true` or `false`)"),
Error::ParseChar(t) => write!(f, "invalid char `{t}` (expected a single character)"),
Error::TopLevelNotMap => {
f.write_str("top level value must be a struct or map of key/value pairs")
}
Error::Unsupported(what) => write!(f, "unsupported value type: {what}"),
}
}
}
impl std::error::Error for Error {}
impl serde::ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl serde::de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}