From a7a28ed8c64d54c7f47a53106032d49c2aeef058 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 24 May 2023 12:01:36 +0300 Subject: [PATCH] chore: make websurfx directory and config file names as constants --- src/config_parser/parser.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/config_parser/parser.rs b/src/config_parser/parser.rs index 66a9284..bbeba86 100644 --- a/src/config_parser/parser.rs +++ b/src/config_parser/parser.rs @@ -5,6 +5,10 @@ use super::parser_models::Style; use rlua::Lua; use std::{format, fs, path::Path}; +// ------- Constants -------- +static COMMON_DIRECTORY_NAME: &str = "websurfx"; +static CONFIG_FILE_NAME: &str = "config.lua"; + /// A named struct which stores the parsed config file options. /// /// # Fields @@ -67,20 +71,29 @@ impl Config { fn handle_different_config_file_path() -> Result> { if Path::new( format!( - "{}/.config/websurfx/config.lua", - std::env::var("HOME").unwrap() + "{}/.config/{}/config.lua", + std::env::var("HOME").unwrap(), + COMMON_DIRECTORY_NAME ) .as_str(), ) .exists() { Ok(format!( - "{}/.config/websurfx/config.lua", - std::env::var("HOME").unwrap() + "{}/.config/{}/{}", + std::env::var("HOME").unwrap(), + COMMON_DIRECTORY_NAME, + CONFIG_FILE_NAME )) - } else if Path::new("/etc/xdg/websurfx/config.lua").exists() { + } else if Path::new( + format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str(), + ) + .exists() + { Ok("/etc/xdg/websurfx/config.lua".to_string()) - } else if Path::new("./websurfx/config.lua").exists() { + } else if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str()) + .exists() + { Ok("./websurfx/config.lua".to_string()) } else { Err(format!("Config file not found!!").into())