macro_rules! forward_to_unserializable_type { ($($ty:ident => $se_method:ident,)*) => { $( fn $se_method(self, _v: $ty) -> Result { Err(SerializerError::UnserializableType { name: "$ty" }) } )* } } mod deserializer; mod serializer; mod extract_string; mod extract_document_id; pub use self::deserializer::Deserializer; pub use self::serializer::Serializer; pub use self::extract_string::ExtractString; pub use self::extract_document_id::extract_document_id; use std::{fmt, error::Error}; use rmp_serde::encode::Error as RmpError; use serde::ser; #[derive(Debug)] pub enum SerializerError { DocumentIdNotFound, RmpError(RmpError), SledError(sled::Error), UnserializableType { name: &'static str }, Custom(String), } impl ser::Error for SerializerError { fn custom(msg: T) -> Self { SerializerError::Custom(msg.to_string()) } } impl fmt::Display for SerializerError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { SerializerError::DocumentIdNotFound => { write!(f, "serialized document does not have an id according to the schema") } SerializerError::RmpError(e) => write!(f, "rmp serde related error: {}", e), SerializerError::SledError(e) => write!(f, "sled related error: {}", e), SerializerError::UnserializableType { name } => { write!(f, "Only struct and map types are considered valid documents and can be serialized, not {} types directly.", name) }, SerializerError::Custom(s) => f.write_str(&s), } } } impl Error for SerializerError {} impl From for SerializerError { fn from(value: String) -> SerializerError { SerializerError::Custom(value) } } impl From for SerializerError { fn from(error: RmpError) -> SerializerError { SerializerError::RmpError(error) } } impl From for SerializerError { fn from(error: sled::Error) -> SerializerError { SerializerError::SledError(error) } }