diff --git a/src/handler/paths.rs b/src/handler/paths.rs index 9b4fa07..91f7f94 100644 --- a/src/handler/paths.rs +++ b/src/handler/paths.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; use std::io::Error; use std::path::Path; +use std::sync::OnceLock; // ------- Constants -------- static PUBLIC_DIRECTORY_NAME: &str = "public"; @@ -20,57 +21,7 @@ pub enum FileType { Theme, } -static FILE_PATHS_FOR_DIFF_FILE_TYPES: once_cell::sync::Lazy>> = - once_cell::sync::Lazy::new(|| { - HashMap::from([ - ( - FileType::Config, - vec![ - format!( - "{}/.config/{}/{}", - std::env::var("HOME").unwrap(), - COMMON_DIRECTORY_NAME, - CONFIG_FILE_NAME - ), - format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME), - format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME), - ], - ), - ( - FileType::Theme, - vec![ - format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME), - format!("./{}/", PUBLIC_DIRECTORY_NAME), - ], - ), - ( - FileType::AllowList, - vec![ - format!( - "{}/.config/{}/{}", - std::env::var("HOME").unwrap(), - COMMON_DIRECTORY_NAME, - ALLOWLIST_FILE_NAME - ), - format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME), - format!("./{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME), - ], - ), - ( - FileType::BlockList, - vec![ - format!( - "{}/.config/{}/{}", - std::env::var("HOME").unwrap(), - COMMON_DIRECTORY_NAME, - BLOCKLIST_FILE_NAME - ), - format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME), - format!("./{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME), - ], - ), - ]) - }); +static FILE_PATHS_FOR_DIFF_FILE_TYPES: OnceLock>> = OnceLock::new(); /// A helper function which returns an appropriate config file path checking if the config /// file exists on that path. @@ -95,11 +46,64 @@ static FILE_PATHS_FOR_DIFF_FILE_TYPES: once_cell::sync::Lazy Result { - let file_path = FILE_PATHS_FOR_DIFF_FILE_TYPES.get(&file_type).unwrap(); +pub fn file_path(file_type: FileType) -> Result<&'static str, Error> { + let file_path: &Vec = FILE_PATHS_FOR_DIFF_FILE_TYPES + .get_or_init(|| { + HashMap::from([ + ( + FileType::Config, + vec![ + format!( + "{}/.config/{}/{}", + std::env::var("HOME").unwrap(), + COMMON_DIRECTORY_NAME, + CONFIG_FILE_NAME + ), + format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME), + format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME), + ], + ), + ( + FileType::Theme, + vec![ + format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME), + format!("./{}/", PUBLIC_DIRECTORY_NAME), + ], + ), + ( + FileType::AllowList, + vec![ + format!( + "{}/.config/{}/{}", + std::env::var("HOME").unwrap(), + COMMON_DIRECTORY_NAME, + ALLOWLIST_FILE_NAME + ), + format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME), + format!("./{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME), + ], + ), + ( + FileType::BlockList, + vec![ + format!( + "{}/.config/{}/{}", + std::env::var("HOME").unwrap(), + COMMON_DIRECTORY_NAME, + BLOCKLIST_FILE_NAME + ), + format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME), + format!("./{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME), + ], + ), + ]) + }) + .get(&file_type) + .unwrap(); + for (idx, _) in file_path.iter().enumerate() { if Path::new(file_path[idx].as_str()).exists() { - return Ok(file_path[idx].clone()); + return Ok(std::mem::take(&mut &*file_path[idx])); } } diff --git a/src/results/user_agent.rs b/src/results/user_agent.rs index 13166bf..3bfa05b 100644 --- a/src/results/user_agent.rs +++ b/src/results/user_agent.rs @@ -1,28 +1,32 @@ //! This module provides the functionality to generate random user agent string. +use std::sync::OnceLock; + use fake_useragent::{Browsers, UserAgents, UserAgentsBuilder}; -static USER_AGENTS: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { - UserAgentsBuilder::new() - .cache(false) - .dir("/tmp") - .thread(1) - .set_browsers( - Browsers::new() - .set_chrome() - .set_safari() - .set_edge() - .set_firefox() - .set_mozilla(), - ) - .build() -}); +static USER_AGENTS: OnceLock = OnceLock::new(); /// A function to generate random user agent to improve privacy of the user. /// /// # Returns /// /// A randomly generated user agent string. -pub fn random_user_agent() -> String { - USER_AGENTS.random().to_string() +pub fn random_user_agent() -> &'static str { + USER_AGENTS + .get_or_init(|| { + UserAgentsBuilder::new() + .cache(false) + .dir("/tmp") + .thread(1) + .set_browsers( + Browsers::new() + .set_chrome() + .set_safari() + .set_edge() + .set_firefox() + .set_mozilla(), + ) + .build() + }) + .random() }