1pub mod detection;
2mod translator;
3
4use std::{error::Error as StdError, fmt};
5pub use translator::*;
6
7#[derive(Debug)]
8pub enum StatusCode {
10 BadRequest,
11 KeyBlocked,
12 DailyReqLimitExceeded,
13 DailyCharLimitExceeded,
14 TextTooLong,
15 TooManyRequests,
16 UnprocessableText,
17 InternalServerError,
18 LangNotSupported,
19}
20
21impl From<StatusCode> for usize {
22 fn from(code: StatusCode) -> usize {
23 use StatusCode::*;
24
25 match code {
26 BadRequest => 400,
27 KeyBlocked => 402,
28 DailyReqLimitExceeded => 403,
29 DailyCharLimitExceeded => 404,
30 TextTooLong => 413,
31 TooManyRequests => 429,
32 UnprocessableText => 422,
33 InternalServerError => 500,
34 LangNotSupported => 501,
35 }
36 }
37}
38
39#[derive(Debug)]
40pub enum Error {
42 TooManyRequests,
44 Request,
46 NotValidLength { min: usize, max: usize },
48 EngineNotSupported(String),
50 Server(StatusCode),
52 TranslationNotFound,
54 Reqwest(reqwest::Error),
56 CssParser(String),
58 InputOutput(std::io::Error),
60}
61
62impl StdError for Error {}
63
64impl fmt::Display for Error {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 use Error::*;
67
68 match &self {
69 TooManyRequests => "Server Error: You made too many requests to the server. \
70 According to google, you are allowed to make 5 requests per \
71 second and up to 200k requests per day. You can wait and \
72 try again later or you can try the translate_batch function"
73 .to_string(),
74 Request => "Request exception can happen due to an api connection error. \n\
75 Please check your connection and try again"
76 .into(),
77 TranslationNotFound => {
78 "No translation was found using the current translator. Try another translator?"
79 .into()
80 }
81 NotValidLength { min, max } => format!(
82 "Text length need to be between {min} and {max} characters"
83 ),
84 EngineNotSupported(engine) => format!(
85 "Translator {engine} is not supported.\n\
86 Supported translators: `deepl`, `google`, `libre`, `linguee`, `microsoft`, `mymemory`, `papago`, `pons`, `qcri`, `yandex`.",
87 ),
88 Error::Server(code) => format!("{code:?}"),
89 Reqwest(err) => err.to_string(),
90 CssParser(err) => err.clone(),
91 InputOutput(err) => err.to_string(),
92 }
93 .fmt(f)
94 }
95}
96
97impl From<reqwest::Error> for Error {
98 fn from(err: reqwest::Error) -> Self {
99 Error::Reqwest(err)
100 }
101}
102
103impl From<std::io::Error> for Error {
104 fn from(err: std::io::Error) -> Self {
105 Error::InputOutput(err)
106 }
107}