diff --git a/src/engine/params.rs b/src/engine/params.rs index eeee5cf..8eef148 100644 --- a/src/engine/params.rs +++ b/src/engine/params.rs @@ -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 { + 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() diff --git a/src/engine/response.rs b/src/engine/response.rs index 9dc309e..29a69b4 100644 --- a/src/engine/response.rs +++ b/src/engine/response.rs @@ -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 } diff --git a/src/main.rs b/src/main.rs index 24af129..7dea481 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ async fn main() -> Result<(), Box> { 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)