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
/*!
# text_translator

## Description

This crate permits to translate text between languages easily. Its goals are:

- implementing an unique library for different APIs
- permitting language translations / detections with or withtout API key when possible
- ease of use / relative performances
- (later) async translations

It wants to implement the following APIs:

- `[x]` [Yandex.Translate](https://tech.yandex.com/translate/doc/dg/concepts/about-docpage)
    - `[x]` with [API key](https://translate.yandex.com/developers/keys)
    - `[ ]` without key (5_000 chars/translation max)
- `[ ]` [Google Translate](https://cloud.google.com/translate/docs/)
- `[ ]` [Bing](https://azure.microsoft.com/en-us/services/cognitive-services/translator-text-api/)

## How to use

To use it, you first need to construct a translator (a struct implementing the [Api](trait.Api.html) trait).

Then, you will be able to do various function calls on this struct:

- [`my_translator.translate(my_text, input_language, target_language)`](trait.Api.html#tymethod.translate)
- [`my_translator.detect(my_text)`](trait.ApiDetect.html#tymethod.detect) if the API implements language detection

Languages are represented with the [`Language`](enum.Language.html) enum for target language, and [`InputLanguage`](enum.InputLanguage.html) for input language.
See their respective documentations for more.

## Examples

For the moment, only the Yandex API is implemented.

To see examples on how to use it, see [its documentation](struct.Yandex.html).
*/

mod api;
mod languages;

pub use api::*;
pub use languages::*;

/// Enum containing different errors that may be raised by the program at runtime.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Error {
    /// Error when trying to convert translation result to utf-8.
    CouldNotConvertToUtf8String(std::string::FromUtf8Error),
    /// Error when trying to convert translation result to utf-8.
    CouldNotConvertToUtf8Str(std::str::Utf8Error),
    /// Error when deserializing JSON string.
    CouldNotDerializeJson,
    /// Error when sending API request : no KEY set.
    NoApiKeySet,
    /// Error parsing query to a valid URI.
    CouldNotParseUri(String),
    /// Error executing `tokio::runtime::Runtime::new()`.
    FailedToCreateTokioRuntime,
    /// Language input and output are the same.
    SameLanguages(Language, Language),
    /// Could not retrieve language code.
    UnknownLanguageCode(String),
    /// Yandex API error.
    YandexAPIError(api::yandex::YandexError),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error : {}", &self)
    }
}

impl std::error::Error for Error {}