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<regex::Error> for Error {
29 fn from(e: regex::Error) -> Self {
30 Error::Serialization(e.to_string())
31 }
32}
33
34impl From<serde_json::Error> for Error {
35 fn from(e: serde_json::Error) -> Self {
36 Error::Serialization(e.to_string())
37 }
38}
39
40impl From<std::io::Error> for Error {
41 fn from(e: std::io::Error) -> Self {
42 Error::Io(e.to_string())
43 }
44}
45
46impl From<SystemTimeError> for Error {
47 fn from(e: SystemTimeError) -> Self {
48 Error::Default(e.to_string())
49 }
50}
51
52impl From<tungstenite::Error> for Error {
53 fn from(e: tungstenite::Error) -> Self {
54 Error::Io(e.to_string())
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use crate::Error;
61
62 #[test]
63 fn can_display() {
64 let e = Error::Default("the message".to_string());
65 assert_eq!(e.to_string(), "Default Error: the message");
66 }
67}