1#[derive(Debug)]
3pub enum Error {
4 DecodeError(String),
6 WireError(std::io::Error),
8 Unknown(Box<dyn std::error::Error + Send + Sync>)
13}
14
15unsafe impl Send for Error {}
16unsafe impl Sync for Error {}
17
18impl Error {
19 pub fn decode(msg: &str) -> Self {
21 Self::DecodeError(msg.into())
22 }
23}
24
25impl std::fmt::Display for Error {
26 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
27 match self {
28 Self::DecodeError(error) => write!(f, "Decoding error: {}", error),
29 Self::WireError(error) => write!(f, "IO error: {}", error),
30 Self::Unknown(error) => write!(f, "Unknown error: {}", error),
33 }
34 }
35}
36
37impl std::error::Error for Error {
38 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39 None
40 }
41}
42
43impl From<std::io::Error> for Error {
44 fn from(error: std::io::Error) -> Self {
45 Self::WireError(error)
46 }
47}
48
49impl From<Error> for std::io::Error {
50 fn from(err: Error) -> std::io::Error {
51 match err {
52 Error::WireError(e) => e,
53 Error::DecodeError(e) => Self::new(
54 std::io::ErrorKind::Other,
55 format!("{}", e).as_str()
56 ),
57 Error::Unknown(e) => Self::new(
58 std::io::ErrorKind::Other,
59 format!("{}", e).as_str()
60 ),
61 }
62 }
63}
64
65impl From<Box<dyn std::error::Error + Sync + Send>> for Error {
82 fn from(error: Box<dyn std::error::Error + Sync + Send>) -> Self {
83 Self::Unknown(error)
84 }
85}