pastry_dht/
error.rs

1use std::{
2    array::TryFromSliceError,
3    fmt::{self, Display},
4    net::AddrParseError,
5    num::ParseIntError,
6    time::SystemTimeError,
7};
8
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// Pastry errors.
12#[derive(Clone, Debug, PartialEq)]
13pub enum Error {
14    Abort,
15    Config(String),
16    Internal(String),
17    Parse(String),
18    Value(String),
19}
20
21impl std::error::Error for Error {}
22
23impl Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        match self {
26            Error::Config(s) | Error::Internal(s) | Error::Parse(s) | Error::Value(s) => {
27                write!(f, "{}", s)
28            }
29            Error::Abort => write!(f, "Operation aborted"),
30        }
31    }
32}
33
34impl From<std::string::FromUtf8Error> for Error {
35    fn from(err: std::string::FromUtf8Error) -> Self {
36        Error::Internal(err.to_string())
37    }
38}
39
40impl From<std::io::Error> for Error {
41    fn from(err: std::io::Error) -> Self {
42        Error::Internal(err.to_string())
43    }
44}
45
46impl From<AddrParseError> for Error {
47    fn from(err: AddrParseError) -> Self {
48        Error::Internal(err.to_string())
49    }
50}
51
52impl From<ParseIntError> for Error {
53    fn from(err: ParseIntError) -> Self {
54        Error::Internal(err.to_string())
55    }
56}
57
58impl<T> From<std::sync::TryLockError<T>> for Error {
59    fn from(err: std::sync::TryLockError<T>) -> Self {
60        Error::Internal(err.to_string())
61    }
62}
63
64impl<T> From<std::sync::PoisonError<T>> for Error {
65    fn from(err: std::sync::PoisonError<T>) -> Self {
66        Error::Internal(err.to_string())
67    }
68}
69
70impl From<tokio::task::JoinError> for Error {
71    fn from(err: tokio::task::JoinError) -> Self {
72        Error::Internal(err.to_string())
73    }
74}
75
76impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
77    fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self {
78        Error::Internal(err.to_string())
79    }
80}
81
82impl<T> From<tokio::sync::mpsc::error::TrySendError<T>> for Error {
83    fn from(err: tokio::sync::mpsc::error::TrySendError<T>) -> Self {
84        Error::Internal(err.to_string())
85    }
86}
87
88impl From<tokio::sync::oneshot::error::RecvError> for Error {
89    fn from(err: tokio::sync::oneshot::error::RecvError) -> Self {
90        Error::Internal(err.to_string())
91    }
92}
93
94impl From<Error> for tonic::Status {
95    fn from(err: Error) -> Self {
96        tonic::Status::internal(err.to_string())
97    }
98}
99
100impl From<tonic::Status> for Error {
101    fn from(err: tonic::Status) -> Self {
102        Error::Internal(err.to_string())
103    }
104}
105
106impl From<tonic::transport::Error> for Error {
107    fn from(err: tonic::transport::Error) -> Self {
108        Error::Internal(err.to_string())
109    }
110}
111
112impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
113    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
114        Error::Internal(err.to_string())
115    }
116}
117
118impl From<TryFromSliceError> for Error {
119    fn from(err: TryFromSliceError) -> Self {
120        Error::Internal(err.to_string())
121    }
122}
123
124impl From<SystemTimeError> for Error {
125    fn from(err: SystemTimeError) -> Self {
126        Error::Internal(err.to_string())
127    }
128}