diff --git a/src/main.rs b/src/main.rs index 522f489..41a75e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{fs::{File, create_dir, create_dir_all}, io::{Read, Write}}; +use std::{fs::{File, create_dir}, io::{Read, Write}}; use hyper_tls::HttpsConnector; use hyper::{Body, Client, body, client::HttpConnector}; @@ -20,10 +20,9 @@ pub async fn body_to_str(res: Body) -> String { } fn get_config_folder() -> String { - let xdg = std::env::var("XDG_CONFIG_HOME"); - let mut xdg_val = if xdg.is_ok() { xdg.clone().ok().unwrap() } else { "~/.config".to_string() }; - if xdg_val.ends_with("/") { xdg_val.remove(xdg_val.len() - 1); } - return xdg_val + "/Lychee_CLIent"; + let mut p = dirs::config_dir().unwrap(); + p.push("Lychee_CLIent"); + return p.to_str().unwrap().to_string(); } fn write_to_file(d: String, p: String) { @@ -40,7 +39,7 @@ fn read_from_file(p: String) -> String { #[tokio::main] async fn main() -> Result<(), Box> { - create_dir_all(get_config_folder()).expect("Cannot create folder."); + create_dir(get_config_folder()).expect("Cannot create folder."); let matches = App::new("Lychee CLIent") .version("0.1.0") .author("Louis Vallat ") @@ -84,7 +83,7 @@ async fn main() -> Result<(), Box> { #[cfg(test)] mod tests_main { use hyper::Body; - use crate::{body_to_str, get_config_folder}; + use crate::body_to_str; #[test] fn body_to_str_empty() { @@ -96,26 +95,4 @@ mod tests_main { assert_eq!("demo", tokio_test::block_on(body_to_str(Body::from("demo")))); assert_eq!("hello world", tokio_test::block_on(body_to_str(Body::from("hello world")))); } - - #[test] - fn get_config_folder_from_env() { - std::env::set_var("XDG_CONFIG_HOME", "~"); - assert_eq!("~/Lychee_CLIent", get_config_folder()); - - std::env::set_var("XDG_CONFIG_HOME", "~/.conf"); - assert_eq!("~/.conf/Lychee_CLIent", get_config_folder()); - } - - #[test] - fn get_config_folder_trailing_slash() { - std::env::set_var("XDG_CONFIG_HOME", "~/"); - assert_eq!("~/Lychee_CLIent", get_config_folder()); - } - - #[test] - fn get_config_folder_no_env() { - std::env::remove_var("XDG_CONFIG_HOME"); - assert_eq!("~/.config/Lychee_CLIent", get_config_folder()); - } - }