yimi_rutool/
error.rs

1//! Error types for the rutool library
2
3/// Result type alias for rutool operations
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Main error type for rutool operations
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// IO operation errors
10    #[error("IO error: {0}")]
11    Io(#[from] std::io::Error),
12
13    /// UTF-8 conversion errors
14    #[error("UTF-8 error: {0}")]
15    Utf8(#[from] std::str::Utf8Error),
16
17    /// String conversion errors
18    #[error("String conversion error: {0}")]
19    FromUtf8(#[from] std::string::FromUtf8Error),
20
21    /// JSON parsing/serialization errors
22    #[cfg(feature = "json")]
23    #[error("JSON error: {0}")]
24    Json(#[from] serde_json::Error),
25
26    /// HTTP client errors
27    #[cfg(feature = "http")]
28    #[error("HTTP error: {0}")]
29    Http(#[from] reqwest::Error),
30
31    /// Cryptography errors
32    #[cfg(feature = "crypto")]
33    #[error("Crypto error: {0}")]
34    Crypto(String),
35
36    /// Database errors
37    #[cfg(feature = "db")]
38    #[error("Database error: {0}")]
39    Database(String),
40
41    /// Date/time parsing errors
42    #[cfg(feature = "core")]
43    #[error("Date/time error: {0}")]
44    DateTime(String),
45
46    /// Regex compilation errors
47    #[error("Regex error: {0}")]
48    Regex(#[from] regex::Error),
49
50    /// Generic error with custom message
51    #[error("Rutool error: {0}")]
52    Custom(String),
53
54    /// Conversion errors
55    #[error("Conversion error: {0}")]
56    Conversion(String),
57
58    /// Validation errors
59    #[error("Validation error: {0}")]
60    Validation(String),
61
62    /// Not found errors
63    #[error("Not found: {0}")]
64    NotFound(String),
65
66    /// Permission denied errors
67    #[error("Permission denied: {0}")]
68    PermissionDenied(String),
69
70    /// Timeout errors
71    #[error("Timeout error: {0}")]
72    Timeout(String),
73
74    /// Configuration errors
75    #[error("Configuration error: {0}")]
76    Config(String),
77
78    /// Concurrency errors (lock poisoning, etc.)
79    #[error("Concurrency error: {0}")]
80    Concurrency(String),
81}
82
83impl Error {
84    /// Create a new custom error
85    pub fn custom<S: Into<String>>(message: S) -> Self {
86        Self::Custom(message.into())
87    }
88
89    /// Create a new crypto error
90    #[cfg(feature = "crypto")]
91    pub fn crypto<S: Into<String>>(message: S) -> Self {
92        Self::Crypto(message.into())
93    }
94
95    /// Create a new database error
96    #[cfg(feature = "db")]
97    pub fn database<S: Into<String>>(message: S) -> Self {
98        Self::Database(message.into())
99    }
100
101    /// Create a new date/time error
102    #[cfg(feature = "core")]
103    pub fn datetime<S: Into<String>>(message: S) -> Self {
104        Self::DateTime(message.into())
105    }
106
107    /// Create a new conversion error
108    pub fn conversion<S: Into<String>>(message: S) -> Self {
109        Self::Conversion(message.into())
110    }
111
112    /// Create a new validation error
113    pub fn validation<S: Into<String>>(message: S) -> Self {
114        Self::Validation(message.into())
115    }
116
117    /// Create a new not found error
118    pub fn not_found<S: Into<String>>(message: S) -> Self {
119        Self::NotFound(message.into())
120    }
121
122    /// Create a new permission denied error
123    pub fn permission_denied<S: Into<String>>(message: S) -> Self {
124        Self::PermissionDenied(message.into())
125    }
126
127    /// Create a new timeout error
128    pub fn timeout<S: Into<String>>(message: S) -> Self {
129        Self::Timeout(message.into())
130    }
131
132    /// Create a new configuration error
133    pub fn config<S: Into<String>>(message: S) -> Self {
134        Self::Config(message.into())
135    }
136
137    /// Create a new concurrency error
138    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}