use serde::ser;
use std::fmt::{self, Display};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Message(String),
ExpectedStartStruct,
ExpectedEndStruct,
UnsupportedValue { kind: String },
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => write!(f, "{msg}"),
Error::ExpectedStartStruct => {
f.write_str("unexpected value type to start serialization")
}
Error::ExpectedEndStruct => {
f.write_str("expect an end of the struct before starting new one")
}
Error::UnsupportedValue { kind } => f.write_str(&format!(
"unsupported value encountered while serializing: {kind}"
)),
}
}
}
impl std::error::Error for Error {}