deeptrans/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
pub mod detection;
mod translator;

use std::{error::Error as StdError, fmt};
pub use translator::*;
#[derive(Debug)]
pub enum StatusCode {
    BadRequest,
    KeyBlocked,
    DailyReqLimitExceeded,
    DailyCharLimitExceeded,
    TextTooLong,
    TooManyRequests,
    UnprocessableText,
    InternalServerError,
    LangNotSupported,
}

impl From<StatusCode> for usize {
    fn from(code: StatusCode) -> usize {
        use StatusCode::*;

        match code {
            BadRequest => 400,
            KeyBlocked => 402,
            DailyReqLimitExceeded => 403,
            DailyCharLimitExceeded => 404,
            TextTooLong => 413,
            TooManyRequests => 429,
            UnprocessableText => 422,
            InternalServerError => 500,
            LangNotSupported => 501,
        }
    }
}

#[derive(Debug)]
pub enum Error {
    /// Error occurred during the request call, e.g a connection problem.
    TooManyRequests,
    /// Error occurred during the request call, e.g a connection problem.
    Request,
    /// The provided text exceed the length limit of the translator
    NotValidLength {
        min: usize,
        max: usize,
    },
    EngineNotSupported(String),
    Server(StatusCode),
    /// Translation was found for the text provided by the user
    TranslationNotFound,
    Reqwest(reqwest::Error),
    CssParser(String),
    InputOutput(std::io::Error),
}

impl StdError for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Error::*;

        match &self {
            TooManyRequests => "Server Error: You made too many requests to the server. \
            According to google, you are allowed to make 5 requests per \
            second and up to 200k requests per day. You can wait and \
            try again later or you can try the translate_batch function"
                .to_string(),
            Request => "Request exception can happen due to an api connection error. \n\
            Please check your connection and try again"
                .into(),
            TranslationNotFound => {
                "No translation was found using the current translator. Try another translator?"
                    .into()
            }
            NotValidLength { min, max } => format!(
                "Text length need to be between {min} and {max} characters"
            ),
            EngineNotSupported(engine) => format!(
                "Translator {} is not supported.\n\
                Supported translators: `deepl`, `google`, `libre`, `linguee`, `microsoft`, `mymemory`, `papago`, `pons`, `qcri`, `yandex`.",
                engine
            ),
            Error::Server(code) => format!("{code:?}"),
            Reqwest(err) => err.to_string(),
            CssParser(err) => err.clone(),
            InputOutput(err) => err.to_string(),
        }
        .fmt(f)
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::Reqwest(err)
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::InputOutput(err)
    }
}