markdown_translator/
error.rs1use std::fmt;
6
7#[derive(Debug)]
19pub enum TranslationError {
20 Http(reqwest::Error),
22 Custom(String),
24 RateLimitError(String),
26 ApiError {
28 code: i32,
30 message: String
32 },
33 ParseError(String),
35}
36
37impl fmt::Display for TranslationError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 TranslationError::Http(e) => write!(f, "HTTP error: {}", e),
41 TranslationError::Custom(msg) => write!(f, "{}", msg),
42 TranslationError::RateLimitError(msg) => write!(f, "Rate limit error: {}", msg),
43 TranslationError::ApiError { code, message } => write!(f, "API error {}: {}", code, message),
44 TranslationError::ParseError(msg) => write!(f, "Parse error: {}", msg),
45 }
46 }
47}
48
49impl std::error::Error for TranslationError {}
50
51impl From<reqwest::Error> for TranslationError {
52 fn from(error: reqwest::Error) -> Self {
53 TranslationError::Http(error)
54 }
55}
56
57impl From<String> for TranslationError {
58 fn from(error: String) -> Self {
59 TranslationError::Custom(error)
60 }
61}
62
63impl From<&str> for TranslationError {
64 fn from(error: &str) -> Self {
65 TranslationError::Custom(error.to_string())
66 }
67}
68
69pub type Result<T> = std::result::Result<T, TranslationError>;