use std::borrow::Cow; use maildir::MailEntryError; use mailparse::MailParseError; use serde::ser::SerializeStruct as _; use serde_json::Error as JSONError; pub type Result = std::result::Result; #[derive(Debug)] pub enum Error { IoError(std::io::Error), MailEntryError(MailEntryError), SortOrder(String), Setuid(String), JSONError(JSONError), PathError { msg: String, path: String }, } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { write!(f, "{:?}", self) } } impl std::error::Error for Error {} impl serde::Serialize for Error { fn serialize(&self, serializer: S) -> std::result::Result where S: serde::Serializer, { let mut state = serializer.serialize_struct("Error", 1)?; let err_str: Cow = match self { Error::IoError(e) => Cow::Owned(e.to_string()), Error::MailEntryError(e) => Cow::Owned(e.to_string()), Error::SortOrder(s) => Cow::Borrowed(s), Error::Setuid(s) => Cow::Borrowed(s), Error::JSONError(e) => Cow::Owned(e.to_string()), Error::PathError { msg, path } => Cow::Owned(format!("{} {:?}", msg, path)), }; state.serialize_field("error", &err_str)?; state.end() } } impl From for Error { fn from(io_err: std::io::Error) -> Self { Error::IoError(io_err) } } impl From for Error { fn from(me_err: MailEntryError) -> Self { Error::MailEntryError(me_err) } } impl From for Error { fn from(mp_err: MailParseError) -> Self { Error::MailEntryError(MailEntryError::ParseError(mp_err)) } } impl From for Error { fn from(j_err: JSONError) -> Self { Error::JSONError(j_err) } }