arete_sdk/
error.rs

1use std::{sync::PoisonError, time::SystemTimeError};
2use strum_macros::Display;
3
4#[derive(Debug, Display)]
5pub enum Error {
6    #[strum(to_string = "Default Error: {0}")]
7    Default(String),
8
9    #[strum(to_string = "Io Error: {0}")]
10    Io(String),
11
12    #[strum(to_string = "Lock Error: {0}")]
13    Lock(String),
14
15    #[strum(to_string = "Serialization Error: {0}")]
16    Serialization(String),
17
18    #[strum(to_string = "Timeout Error: {0}")]
19    Timeout(String),
20}
21
22impl<T> From<PoisonError<T>> for Error {
23    fn from(e: PoisonError<T>) -> Self {
24        Self::Lock(e.to_string())
25    }
26}
27
28impl From<serde_json::Error> for Error {
29    fn from(e: serde_json::Error) -> Self {
30        Error::Serialization(e.to_string())
31    }
32}
33
34impl From<std::io::Error> for Error {
35    fn from(e: std::io::Error) -> Self {
36        Error::Io(e.to_string())
37    }
38}
39
40impl From<SystemTimeError> for Error {
41    fn from(e: SystemTimeError) -> Self {
42        Error::Default(e.to_string())
43    }
44}
45
46impl From<tungstenite::Error> for Error {
47    fn from(e: tungstenite::Error) -> Self {
48        Error::Io(e.to_string())
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use crate::Error;
55
56    #[test]
57    fn can_display() {
58        let e = Error::Default("the message".to_string());
59        assert_eq!(e.to_string(), "Default Error: the message");
60    }
61}