|
|
|
|
|
|
|
use std::collections::HashMap; |
|
use std::io::Error; |
|
use std::path::Path; |
|
use std::sync::OnceLock; |
|
|
|
|
|
|
|
const PUBLIC_DIRECTORY_NAME: &str = "public"; |
|
|
|
const COMMON_DIRECTORY_NAME: &str = "websurfx"; |
|
|
|
const CONFIG_FILE_NAME: &str = "config.lua"; |
|
|
|
const ALLOWLIST_FILE_NAME: &str = "allowlist.txt"; |
|
|
|
const BLOCKLIST_FILE_NAME: &str = "blocklist.txt"; |
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, Debug)] |
|
pub enum FileType { |
|
|
|
Config, |
|
|
|
AllowList, |
|
|
|
BlockList, |
|
|
|
Theme, |
|
} |
|
|
|
|
|
static FILE_PATHS_FOR_DIFF_FILE_TYPES: OnceLock<HashMap<FileType, Vec<String>>> = OnceLock::new(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn file_path(file_type: FileType) -> Result<&'static str, Error> { |
|
let file_path: &Vec<String> = 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(std::mem::take(&mut &*file_path[idx])); |
|
} |
|
} |
|
|
|
|
|
Err(Error::new( |
|
std::io::ErrorKind::NotFound, |
|
format!("{:?} file/folder not found!!", file_type), |
|
)) |
|
} |
|
|