#[derive(Debug)]
pub enum Error {
DecodeError(String),
WireError(std::io::Error),
Unknown(Box<dyn std::error::Error + Send + Sync>)
}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}
impl Error {
pub fn decode(msg: &str) -> Self {
Self::DecodeError(msg.into())
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::DecodeError(error) => write!(f, "Decoding error: {}", error),
Self::WireError(error) => write!(f, "IO error: {}", error),
Self::Unknown(error) => write!(f, "Unknown error: {}", error),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::WireError(error)
}
}
impl From<Error> for std::io::Error {
fn from(err: Error) -> std::io::Error {
match err {
Error::WireError(e) => e,
Error::DecodeError(e) => Self::new(
std::io::ErrorKind::Other,
format!("{}", e).as_str()
),
Error::Unknown(e) => Self::new(
std::io::ErrorKind::Other,
format!("{}", e).as_str()
),
}
}
}
impl From<Box<dyn std::error::Error + Sync + Send>> for Error {
fn from(error: Box<dyn std::error::Error + Sync + Send>) -> Self {
Self::Unknown(error)
}
}