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 } => {
44 write!(f, "API error {}: {}", code, message)
45 }
46 TranslationError::ParseError(msg) => write!(f, "Parse error: {}", msg),
47 }
48 }
49}
50
51impl std::error::Error for TranslationError {}
52
53impl From<reqwest::Error> for TranslationError {
54 fn from(error: reqwest::Error) -> Self {
55 TranslationError::Http(error)
56 }
57}
58
59impl From<String> for TranslationError {
60 fn from(error: String) -> Self {
61 TranslationError::Custom(error)
62 }
63}
64
65impl From<&str> for TranslationError {
66 fn from(error: &str) -> Self {
67 TranslationError::Custom(error.to_string())
68 }
69}
70
71pub type Result<T> = std::result::Result<T, TranslationError>;