type optimization
parent
c068350316
commit
c33c579382
|
@ -1,8 +1,8 @@
|
|||
use clap::{load_yaml, App};
|
||||
use clap::{load_yaml, App, ArgMatches};
|
||||
use directories::BaseDirs;
|
||||
use dotenv;
|
||||
use sprintf::sprintf;
|
||||
use std::env;
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
pub struct Params {
|
||||
pub source_lang: String,
|
||||
|
@ -13,30 +13,30 @@ pub struct Params {
|
|||
}
|
||||
|
||||
impl Params {
|
||||
pub fn new() -> Self {
|
||||
let base_dir = match BaseDirs::new() {
|
||||
Some(dirs) => dirs,
|
||||
None => {
|
||||
panic!("Please provide key in ~/.config/deepl/.env file")
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let base_dir: BaseDirs = BaseDirs::new().unwrap();
|
||||
|
||||
let my_path: PathBuf = base_dir.config_dir().join("deepl/.env");
|
||||
match dotenv::from_path(my_path.as_path()) {
|
||||
Ok(env) => env,
|
||||
Err(_) => {
|
||||
return Err(format!(
|
||||
"Could not load .env file {:?}",
|
||||
base_dir.config_dir().join("deepl/.env").as_path()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let my_path = base_dir.config_dir().join("deepl/.env");
|
||||
match dotenv::from_path(my_path.as_path()) {
|
||||
Ok(env) => env,
|
||||
Err(_) => panic!("Could not load .env file {:?}", my_path.as_path()),
|
||||
};
|
||||
|
||||
let yaml = load_yaml!("./../../cli.yml");
|
||||
let matches = App::from_yaml(yaml).get_matches();
|
||||
let matches: ArgMatches = App::from_yaml(yaml).get_matches();
|
||||
|
||||
Self {
|
||||
Ok(Self {
|
||||
source_lang: matches.value_of("source").unwrap_or("0").to_string(),
|
||||
target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(),
|
||||
key: env::var("KEY").unwrap(),
|
||||
uri: env::var("URI").unwrap(),
|
||||
text: matches.value_of("INPUT").unwrap().to_string(),
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn url(&self) -> String {
|
||||
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()
|
||||
|
|
|
@ -21,7 +21,8 @@ impl DeeplResponse {
|
|||
));
|
||||
trans.push_str(&format!("{}: {}", target_language, translation.text));
|
||||
trans.push('\n');
|
||||
trans.push_str("---------------------------------------------------------\n");
|
||||
trans.push_str(&"-".repeat(57));
|
||||
trans.push('\n');
|
||||
}
|
||||
trans
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let https = HttpsConnector::new();
|
||||
let client = Client::builder().build::<_, hyper::Body>(https);
|
||||
|
||||
let params = Params::new();
|
||||
let params = Params::new()?;
|
||||
|
||||
let request = Request::builder()
|
||||
.method(Method::POST)
|
||||
|
|
Loading…
Reference in New Issue