Skip to content

Commit 53c9d81

Browse files
Optimize file structure
1 parent 00c828a commit 53c9d81

File tree

10 files changed

+137
-44
lines changed

10 files changed

+137
-44
lines changed

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ rand = "0.8.5"
1616
reqwest = { version = "0.11.10", features = ["blocking", "json"] }
1717
serde = { version = "1.0.137", features = ["derive"] }
1818
sha2 = "0.10.2"
19+
20+
[lib]
21+
name = "translator_mangler"
22+
path = "src/lib.rs"
23+
crate-type = [ "rlib", "cdylib", "staticlib" ]
24+
25+
[[bin]]
26+
name = "translator_mangler_cli"
27+
path = "src/bin/cli.rs"

README-zh.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# translator_mangler
2+
3+
| [English (en)](README.md) | **中文 (zh-CN)** |
4+
5+
用翻译服务让你的文本在不同语言间反复横跳,之后将其转换回源语言。
6+
7+
## 支持的翻译API
8+
9+
* [x] [百度翻译](https://api.fanyi.baidu.com/product/111)`Baidu`
10+
* [x] [有道 AI](https://ai.youdao.com/product-fanyi-text.s)`Youdao`
11+
* [ ] [谷歌翻译](https://cloud.google.com/translate/docs/reference/rest/v2/translate)`Google Cloud`
12+
13+
*注:打勾的API已经经过详尽的测试。*
14+
15+
## 使用本软件
16+
17+
### 安装
18+
19+
你可以任选一种方法:
20+
21+
1.[Releases](https://github.com/CSharperMantle/translator_mangler/releases)页面下载最新的预编译包;
22+
2. 自行从源代码构建。
23+
24+
### 运行
25+
26+
请按照终端的指引操作。注意,某些API可能需要在相对应的组织网站上创建账户才能够使用,某些API是收费的。详情请参考上一节中的链接。
27+
28+
以下是一次典型的使用百度API的运行样例。
29+
30+
```text
31+
PS > .\translator_mangler.exe
32+
[INFO] Welcome to translator_mangler!
33+
? Back-end API? Baidu
34+
? API key for Baidu API? ********************
35+
? App ID for Baidu API? *****************
36+
? Language bank? zh,en,yue,wyw,jp,kor,fra,spa,th,ara,ru,pt,de,it,el,nl,pl,bul,est,dan,fin,cs,rom,slo,swe,hu,cht,vie
37+
? Rounds to mangle? 20
38+
? API cool-down? 150
39+
[INFO] Configuration done.
40+
? Text to mangle? The quick fox jumps over the lazy dog. The price of the shirt is 9.15 pounds.
41+
? Original language? en
42+
[INFO] Processing...
43+
[OK] The shirts are 165 and 169.
44+
[INFO] Done.
45+
? Text to mangle? 先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。
46+
? Original language? zh
47+
[INFO] Processing...
48+
[OK] 第一位皇帝什么也没失去,但失败了。
49+
[INFO] Done.
50+
? Text to mangle? ^C
51+
[Enter the text you wish to mangle]
52+
Error: OperationInterrupted
53+
54+
PS >
55+
```
56+
57+
## 开发
58+
59+
本软件使用纯Rust编写。在配置好Rust开发环境后,即可直接克隆本repo,开始开发。
60+
61+
```sh
62+
git clone --depth=1 -- https://github.com/CSharperMantle/translator_mangler.git
63+
cd translator_mangler
64+
65+
# 运行Debug版
66+
cargo run
67+
# 运行Release版
68+
cargo run -r
69+
```
70+
71+
## 许可协议
72+
73+
详见[LICENSE](LICENSE)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# translator_mangler
22

3+
| **English (en)** | [中文 (zh-CN)](README-zh.md) |
4+
35
Mangle your input text by translating it over and over again.
46

57
## Supported translation APIs

src/main.rs src/bin/cli.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
mod mangler;
2-
mod translator;
1+
use inquire::error::InquireError;
32

4-
use core::panic;
5-
6-
use crate::mangler::{get_random_lang_path, mangle};
7-
use crate::translator::{
8-
baidu::TranslatorBaidu, google::TranslatorGoogleCloud, youdao::TranslatorYoudao, Translator,
9-
};
3+
use translator_mangler::get_random_lang_path;
4+
use translator_mangler::mangle;
5+
use translator_mangler::Translator;
6+
use translator_mangler::TranslatorBaidu;
7+
use translator_mangler::TranslatorGoogleCloud;
8+
use translator_mangler::TranslatorYoudao;
109

1110
fn prompt_baidu_api() -> inquire::error::InquireResult<Box<dyn Translator>> {
1211
let input_api_key = inquire::Text::new("API key for Baidu API?").prompt()?;
@@ -45,7 +44,11 @@ fn main() -> inquire::error::InquireResult<()> {
4544
"Baidu" => prompt_baidu_api()?,
4645
"Google Cloud" => prompt_google_cloud_api()?,
4746
"Youdao AI" => prompt_youdao_api()?,
48-
_ => panic!("Unsupported API"),
47+
_ => {
48+
return Err(InquireError::InvalidConfiguration(
49+
"Back-end API".to_string(),
50+
))
51+
}
4952
};
5053

5154
let input_langs = inquire::Text::new("Language bank?")

src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod mangler;
2+
mod translator;
3+
4+
pub use mangler::{get_random_lang_path, mangle};
5+
pub use translator::{
6+
baidu::TranslatorBaidu, google::TranslatorGoogleCloud, youdao::TranslatorYoudao, LanguagePair,
7+
TranslationError, Translator,
8+
};

src/mangler/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::translator::{LanguagePair, TranslationError, Translator};
1+
use crate::translator::{LanguagePair, TranslationError, Translator};
22
use rand::seq::SliceRandom;
33

44
/// Get a random 'path' of languages, starting from and ending with

src/translator/baidu.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ impl Translator for TranslatorBaidu {
7575
if !self.is_single_lang_supported(&lang.from_lang.as_str())
7676
|| !self.is_single_lang_supported(&lang.to_lang.as_str())
7777
{
78-
return Err(TranslationError::new("Unsupported language"));
78+
return Err(TranslationError {
79+
message: "Unsupported language".to_string(),
80+
});
7981
}
8082

8183
// Create salt for randomness
@@ -105,24 +107,26 @@ impl Translator for TranslatorBaidu {
105107

106108
// Handle network error
107109
if let Err(e) = result {
108-
return Err(TranslationError::new(
109-
format!("NETWORK ERR: {}", e).as_str(),
110-
));
110+
return Err(TranslationError {
111+
message: format!("NETWORK ERR: {}", e),
112+
});
111113
}
112114
let result_json = result.unwrap().json::<ResultBaidu>().unwrap();
113115
// Handle API error
114116
let error_code = &result_json.error_code;
115117
if error_code != "" && error_code.parse::<i32>().unwrap() != 0 {
116-
return Err(TranslationError::new(&format!(
117-
"API ERR: {} {}",
118-
result_json.error_code, result_json.error_msg
119-
)));
118+
return Err(TranslationError {
119+
message: format!(
120+
"API ERR: {} {}",
121+
result_json.error_code, result_json.error_msg
122+
),
123+
});
120124
}
121125

122126
Ok(result_json.trans_result[0].dst.clone())
123127
}
124128

125-
fn get_supported_langs(&self) -> &[&str] {
129+
fn get_supported_langs(&self) -> &[&'static str] {
126130
&Self::SUPPORTED_LANGS
127131
}
128132

src/translator/google.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ impl Translator for TranslatorGoogleCloud {
8989
.send();
9090
// Handle network error
9191
if let Err(e) = result {
92-
return Err(TranslationError::new(
93-
format!("NETWORK ERR: {}", e).as_str(),
94-
));
92+
return Err(TranslationError {
93+
message: format!("NETWORK ERR: {}", e),
94+
});
9595
}
9696
let unwrapped_result = result.unwrap();
9797

9898
let status = unwrapped_result.status();
9999
if !status.is_success() {
100-
return Err(TranslationError::new(
101-
format!("REQUEST ERR: HTTP {}", status).as_str(),
102-
));
100+
return Err(TranslationError {
101+
message: format!("REQUEST ERR: HTTP {}", status),
102+
});
103103
}
104104

105105
let result_json = unwrapped_result.json::<ResultGoogleCloud>().unwrap();
106106
// Handle API error
107107
Ok(result_json.data.translations[0].translated_text.clone())
108108
}
109109

110-
fn get_supported_langs(&self) -> &[&str] {
110+
fn get_supported_langs(&self) -> &[&'static str] {
111111
&Self::SUPPORTED_LANGS
112112
}
113113

src/translator/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@ pub mod youdao;
55
/// Errors that may occur when translating.
66
#[derive(Debug)]
77
pub struct TranslationError {
8+
/// The message string describing the error.
89
pub message: String,
910
}
1011

11-
impl TranslationError {
12-
pub fn new(message: &str) -> TranslationError {
13-
TranslationError {
14-
message: message.to_string(),
15-
}
16-
}
17-
}
18-
1912
impl std::fmt::Display for TranslationError {
2013
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2114
write!(f, "TranslationError: {}", self.message)
@@ -48,7 +41,7 @@ pub trait Translator {
4841
///
4942
/// let languages = translator.get_languages();
5043
/// ```
51-
fn get_supported_langs(&self) -> &[&str];
44+
fn get_supported_langs(&self) -> &[&'static str];
5245

5346
/// Check if a language is supported.
5447
///

src/translator/youdao.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ impl Translator for TranslatorYoudao {
6565
if !self.is_single_lang_supported(&lang.from_lang.as_str())
6666
|| !self.is_single_lang_supported(&lang.to_lang.as_str())
6767
{
68-
return Err(TranslationError::new("Unsupported language"));
68+
return Err(TranslationError {
69+
message: "Unsupported language".to_string(),
70+
});
6971
}
7072

7173
// Get current UNIX timestamp
@@ -114,24 +116,23 @@ impl Translator for TranslatorYoudao {
114116

115117
// Handle network error
116118
if let Err(e) = result {
117-
return Err(TranslationError::new(
118-
format!("NETWORK ERR: {}", e).as_str(),
119-
));
119+
return Err(TranslationError {
120+
message: format!("NETWORK ERR: {}", e),
121+
});
120122
}
121123
let result_json = result.unwrap().json::<ResultYoudao>().unwrap();
122124
// Handle API error
123125
let error_code = &result_json.error_code;
124126
if error_code != "" && error_code.parse::<i32>().unwrap() != 0 {
125-
return Err(TranslationError::new(&format!(
126-
"API ERR: {}",
127-
result_json.error_code
128-
)));
127+
return Err(TranslationError {
128+
message: format!("API ERR: {}", result_json.error_code),
129+
});
129130
}
130131

131132
Ok(result_json.translation[0].clone())
132133
}
133134

134-
fn get_supported_langs(&self) -> &[&str] {
135+
fn get_supported_langs(&self) -> &[&'static str] {
135136
&Self::SUPPORTED_LANGS
136137
}
137138

0 commit comments

Comments
 (0)