1pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, thiserror::Error)]
8pub enum Error {
9 #[error("IO error: {0}")]
11 Io(#[from] std::io::Error),
12
13 #[error("UTF-8 error: {0}")]
15 Utf8(#[from] std::str::Utf8Error),
16
17 #[error("String conversion error: {0}")]
19 FromUtf8(#[from] std::string::FromUtf8Error),
20
21 #[cfg(feature = "json")]
23 #[error("JSON error: {0}")]
24 Json(#[from] serde_json::Error),
25
26 #[cfg(feature = "http")]
28 #[error("HTTP error: {0}")]
29 Http(#[from] reqwest::Error),
30
31 #[cfg(feature = "crypto")]
33 #[error("Crypto error: {0}")]
34 Crypto(String),
35
36 #[cfg(feature = "db")]
38 #[error("Database error: {0}")]
39 Database(String),
40
41 #[cfg(feature = "core")]
43 #[error("Date/time error: {0}")]
44 DateTime(String),
45
46 #[error("Regex error: {0}")]
48 Regex(#[from] regex::Error),
49
50 #[error("Rutool error: {0}")]
52 Custom(String),
53
54 #[error("Conversion error: {0}")]
56 Conversion(String),
57
58 #[error("Validation error: {0}")]
60 Validation(String),
61
62 #[error("Not found: {0}")]
64 NotFound(String),
65
66 #[error("Permission denied: {0}")]
68 PermissionDenied(String),
69
70 #[error("Timeout error: {0}")]
72 Timeout(String),
73
74 #[error("Configuration error: {0}")]
76 Config(String),
77
78 #[error("Concurrency error: {0}")]
80 Concurrency(String),
81}
82
83impl Error {
84 pub fn custom<S: Into<String>>(message: S) -> Self {
86 Self::Custom(message.into())
87 }
88
89 #[cfg(feature = "crypto")]
91 pub fn crypto<S: Into<String>>(message: S) -> Self {
92 Self::Crypto(message.into())
93 }
94
95 #[cfg(feature = "db")]
97 pub fn database<S: Into<String>>(message: S) -> Self {
98 Self::Database(message.into())
99 }
100
101 #[cfg(feature = "core")]
103 pub fn datetime<S: Into<String>>(message: S) -> Self {
104 Self::DateTime(message.into())
105 }
106
107 pub fn conversion<S: Into<String>>(message: S) -> Self {
109 Self::Conversion(message.into())
110 }
111
112 pub fn validation<S: Into<String>>(message: S) -> Self {
114 Self::Validation(message.into())
115 }
116
117 pub fn not_found<S: Into<String>>(message: S) -> Self {
119 Self::NotFound(message.into())
120 }
121
122 pub fn permission_denied<S: Into<String>>(message: S) -> Self {
124 Self::PermissionDenied(message.into())
125 }
126
127 pub fn timeout<S: Into<String>>(message: S) -> Self {
129 Self::Timeout(message.into())
130 }
131
132 pub fn config<S: Into<String>>(message: S) -> Self {
134 Self::Config(message.into())
135 }
136
137 pub fn concurrency<S: Into<String>>(message: S) -> Self {
139 Self::Concurrency(message.into())
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 #[test]
148 fn test_error_creation() {
149 let err = Error::custom("test error");
150 assert!(matches!(err, Error::Custom(_)));
151 }
152
153 #[test]
154 fn test_error_display() {
155 let err = Error::validation("invalid input");
156 let msg = err.to_string();
157 assert!(msg.contains("Validation error"));
158 assert!(msg.contains("invalid input"));
159 }
160}