use std::fmt;
#[derive(Debug)]
pub enum TranslationError {
Http(reqwest::Error),
Custom(String),
RateLimitError(String),
ApiError {
code: i32,
message: String
},
ParseError(String),
}
impl fmt::Display for TranslationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TranslationError::Http(e) => write!(f, "HTTP error: {}", e),
TranslationError::Custom(msg) => write!(f, "{}", msg),
TranslationError::RateLimitError(msg) => write!(f, "Rate limit error: {}", msg),
TranslationError::ApiError { code, message } => write!(f, "API error {}: {}", code, message),
TranslationError::ParseError(msg) => write!(f, "Parse error: {}", msg),
}
}
}
impl std::error::Error for TranslationError {}
impl From<reqwest::Error> for TranslationError {
fn from(error: reqwest::Error) -> Self {
TranslationError::Http(error)
}
}
impl From<String> for TranslationError {
fn from(error: String) -> Self {
TranslationError::Custom(error)
}
}
impl From<&str> for TranslationError {
fn from(error: &str) -> Self {
TranslationError::Custom(error.to_string())
}
}
pub type Result<T> = std::result::Result<T, TranslationError>;