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
use codic::codic::{error, App, Config, Error};
use std::path::PathBuf;
/// Result型にまとめるためのバインディング
async fn codic_run(path: &PathBuf) -> Result<String, Error> {
Ok(App::from_config_file(path)?
.take_params().await?
.run().await?
)
}
#[tokio::main]
async fn main() {
// コンフィグファイルの絶対パス
let config_file = ".codic_config.json";
let mut path = std::env::current_exe().unwrap()
.parent().unwrap()
.to_path_buf();
path.push(&config_file);
match codic_run(&path).await {
// 正常に実行されたら翻訳されたテキストを表示
Ok(text) => println!("{}", text),
Err(error) => {
match error {
// サブコマンドが選択されたら
Error::ChosenEditConfig{command} => {
match command {
// 新しいファイルを生成
error::EditConfig::Make => {
match Config::make_default_file(&path).await {
Ok(_) => println!("{} を作成しました。tokenにCodic APIキーを入力してください、\n{}",
config_file, "codic config edit -t <token>"),
Err(_) => println!("{}の作成に失敗しました。", path.to_string_lossy()),
};
},
// ファイルを表示
error::EditConfig::Show => {
println!("{:?}", Config::from_file(&path).expect("ファイルの表示に失敗しました"));
},
// ファイルを削除
error::EditConfig::Remove => {
match std::fs::remove_file(&path) {
Ok(_) => println!("{}を削除しました。", path.to_string_lossy()),
Err(_) => eprintln!("{}の削除に失敗しました。", path.to_string_lossy()),
};
},
// ファイルを編集
error::EditConfig::Edit(token, casing) => {
match Config::edit(&path, token.as_deref(), casing.as_deref()).await {
Ok(_) => {
println!("編集しました。");
println!("{:?}", Config::from_file(&path).unwrap());
},
Err(_) => println!("{}の編集に失敗しました。", path.to_string_lossy()),
};
},
error::EditConfig::None => (),
}
},
// コンフィグファイルが開けなかったら
Error::CannotOpenConfig{error} => {
println!("{} オープンエラー", config_file);
println!("{}", error);
match Config::make_default_file(&path).await {
Ok(_) => println!("{} を作成しました。tokenにCodic APIキーを入力してください、\n{}",
config_file, "codic config edit -t <token>"),
Err(_) => println!("{}の作成に失敗しました。", path.to_string_lossy()),
};
},
// コンフィグファイルが破損していたら
Error::ConfigSyntaxError{error} => {
println!("{} コンフィグファイル構文エラー", config_file);
println!("{}", error);
},
// CodicAPIに接続できなかったら
Error::CouldNotConnect{error} => {
println!("接続エラー");
println!("{}", error);
},
// APIへのリクエストに問題があったら
Error::CodicError(error) => {
println!("リクエストエラー");
println!("{}", error);
},
}
}
};
}