type optimization

master
Mathias Rothenhaeusler 2023-08-10 18:16:10 +02:00
parent c068350316
commit c33c579382
3 changed files with 19 additions and 18 deletions

View File

@ -1,8 +1,8 @@
use clap::{load_yaml, App}; use clap::{load_yaml, App, ArgMatches};
use directories::BaseDirs; use directories::BaseDirs;
use dotenv; use dotenv;
use sprintf::sprintf; use sprintf::sprintf;
use std::env; use std::{env, path::PathBuf};
pub struct Params { pub struct Params {
pub source_lang: String, pub source_lang: String,
@ -13,30 +13,30 @@ pub struct Params {
} }
impl Params { impl Params {
pub fn new() -> Self { pub fn new() -> Result<Self, String> {
let base_dir = match BaseDirs::new() { let base_dir: BaseDirs = BaseDirs::new().unwrap();
Some(dirs) => dirs,
None => { let my_path: PathBuf = base_dir.config_dir().join("deepl/.env");
panic!("Please provide key in ~/.config/deepl/.env file") 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 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(), source_lang: matches.value_of("source").unwrap_or("0").to_string(),
target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(), target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(),
key: env::var("KEY").unwrap(), key: env::var("KEY").unwrap(),
uri: env::var("URI").unwrap(), uri: env::var("URI").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(), text: matches.value_of("INPUT").unwrap().to_string(),
} })
} }
pub fn url(&self) -> String { pub fn url(&self) -> String {
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap() sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()

View File

@ -21,7 +21,8 @@ impl DeeplResponse {
)); ));
trans.push_str(&format!("{}: {}", target_language, translation.text)); trans.push_str(&format!("{}: {}", target_language, translation.text));
trans.push('\n'); trans.push('\n');
trans.push_str("---------------------------------------------------------\n"); trans.push_str(&"-".repeat(57));
trans.push('\n');
} }
trans trans
} }

View File

@ -11,7 +11,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let https = HttpsConnector::new(); let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https); let client = Client::builder().build::<_, hyper::Body>(https);
let params = Params::new(); let params = Params::new()?;
let request = Request::builder() let request = Request::builder()
.method(Method::POST) .method(Method::POST)