text_translator/
lib.rs

1/*!
2# text_translator
3
4## Description
5
6This crate permits to translate text between languages easily. Its goals are:
7
8- implementing an unique library for different APIs
9- permitting language translations / detections with or withtout API key when possible
10- ease of use / relative performances
11- (later) async translations
12
13It wants to implement the following APIs:
14
15- `[x]` [Yandex.Translate](https://tech.yandex.com/translate/doc/dg/concepts/about-docpage)
16    - `[x]` with [API key](https://translate.yandex.com/developers/keys)
17    - `[ ]` without key (5_000 chars/translation max)
18- `[ ]` [Google Translate](https://cloud.google.com/translate/docs/)
19- `[ ]` [Bing](https://azure.microsoft.com/en-us/services/cognitive-services/translator-text-api/)
20
21## How to use
22
23To use it, you first need to construct a translator (a struct implementing the [Api](trait.Api.html) trait).
24
25Then, you will be able to do various function calls on this struct:
26
27- [`my_translator.translate(my_text, input_language, target_language)`](trait.Api.html#tymethod.translate)
28- [`my_translator.detect(my_text)`](trait.ApiDetect.html#tymethod.detect) if the API implements language detection
29
30Languages are represented with the [`Language`](enum.Language.html) enum for target language, and [`InputLanguage`](enum.InputLanguage.html) for input language.
31See their respective documentations for more.
32
33## Examples
34
35For the moment, only the Yandex API is implemented.
36
37To see examples on how to use it, see [its documentation](struct.Yandex.html).
38*/
39
40mod api;
41mod languages;
42
43pub use api::*;
44pub use languages::*;
45
46/// Enum containing different errors that may be raised by the program at runtime.
47#[derive(Debug, Clone, Eq, PartialEq)]
48pub enum Error {
49    /// Error when trying to convert translation result to utf-8.
50    CouldNotConvertToUtf8String(std::string::FromUtf8Error),
51    /// Error when trying to convert translation result to utf-8.
52    CouldNotConvertToUtf8Str(std::str::Utf8Error),
53    /// Error when deserializing JSON string.
54    CouldNotDerializeJson,
55    /// Error when sending API request : no KEY set.
56    NoApiKeySet,
57    /// Error parsing query to a valid URI.
58    CouldNotParseUri(String),
59    /// Error executing `tokio::runtime::Runtime::new()`.
60    FailedToCreateTokioRuntime,
61    /// Language input and output are the same.
62    SameLanguages(Language, Language),
63    /// Could not retrieve language code.
64    UnknownLanguageCode(String),
65    /// Yandex API error.
66    YandexAPIError(api::yandex::YandexError),
67}
68
69impl std::fmt::Display for Error {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        write!(f, "Error : {}", &self)
72    }
73}
74
75impl std::error::Error for Error {}