pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("UTF-8 error: {0}")]
Utf8(#[from] std::str::Utf8Error),
#[error("String conversion error: {0}")]
FromUtf8(#[from] std::string::FromUtf8Error),
#[cfg(feature = "json")]
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[cfg(feature = "http")]
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[cfg(feature = "crypto")]
#[error("Crypto error: {0}")]
Crypto(String),
#[cfg(feature = "db")]
#[error("Database error: {0}")]
Database(String),
#[cfg(feature = "core")]
#[error("Date/time error: {0}")]
DateTime(String),
#[error("Regex error: {0}")]
Regex(#[from] regex::Error),
#[error("Rutool error: {0}")]
Custom(String),
#[error("Conversion error: {0}")]
Conversion(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Permission denied: {0}")]
PermissionDenied(String),
#[error("Timeout error: {0}")]
Timeout(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Concurrency error: {0}")]
Concurrency(String),
}
impl Error {
pub fn custom<S: Into<String>>(message: S) -> Self {
Self::Custom(message.into())
}
#[cfg(feature = "crypto")]
pub fn crypto<S: Into<String>>(message: S) -> Self {
Self::Crypto(message.into())
}
#[cfg(feature = "db")]
pub fn database<S: Into<String>>(message: S) -> Self {
Self::Database(message.into())
}
#[cfg(feature = "core")]
pub fn datetime<S: Into<String>>(message: S) -> Self {
Self::DateTime(message.into())
}
pub fn conversion<S: Into<String>>(message: S) -> Self {
Self::Conversion(message.into())
}
pub fn validation<S: Into<String>>(message: S) -> Self {
Self::Validation(message.into())
}
pub fn not_found<S: Into<String>>(message: S) -> Self {
Self::NotFound(message.into())
}
pub fn permission_denied<S: Into<String>>(message: S) -> Self {
Self::PermissionDenied(message.into())
}
pub fn timeout<S: Into<String>>(message: S) -> Self {
Self::Timeout(message.into())
}
pub fn config<S: Into<String>>(message: S) -> Self {
Self::Config(message.into())
}
pub fn concurrency<S: Into<String>>(message: S) -> Self {
Self::Concurrency(message.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = Error::custom("test error");
assert!(matches!(err, Error::Custom(_)));
}
#[test]
fn test_error_display() {
let err = Error::validation("invalid input");
let msg = err.to_string();
assert!(msg.contains("Validation error"));
assert!(msg.contains("invalid input"));
}
}