use serde::{de, ser}; use std::error::Error; use std::fmt::Display; pub type StructResult = Result; #[derive(Debug, PartialEq, Eq)] pub enum StructError { SerializationNotSupported(&'static str), DeserializationNotSupported(&'static str), Serde(String), UnexpectedEOF, } impl Display for StructError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::SerializationNotSupported(name) => { writeln!(f, "Serialization for type '{}' not supported", name) } Self::DeserializationNotSupported(name) => { writeln!(f, "Deserialization for type '{}' not supported", name) } Self::Serde(message) => writeln!(f, "Serde error: {}", message), Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"), } } } impl Error for StructError {} impl ser::Error for StructError { fn custom(msg: T) -> Self { Self::Serde(msg.to_string()) } } impl de::Error for StructError { fn custom(msg: T) -> Self { Self::Serde(msg.to_string()) } }