diff --git a/Cargo.lock b/Cargo.lock index 4cf0192..880c40c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2891,7 +2891,6 @@ dependencies = [ "human-panic", "humantime", "json5", - "lazy_static", "libc", "log", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 020519f..2bbbc06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,6 @@ futures = "0.3.30" human-panic = "2.0.1" humantime = "2.1.0" json5 = "0.4.1" -lazy_static = "1.5.0" libc = "0.2.155" log = "0.4.21" pretty_assertions = "1.4.0" diff --git a/src/utils.rs b/src/utils.rs index aa3bebd..7611ef2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,8 @@ -use std::path::PathBuf; +use std::{path::PathBuf, sync::LazyLock}; use color_eyre::eyre::Result; use directories::{BaseDirs, ProjectDirs}; use human_panic::Metadata; -use lazy_static::lazy_static; use tracing::error; use tracing_error::ErrorLayer; use tracing_subscriber::{ @@ -19,23 +18,23 @@ const VERSION_MESSAGE: &str = concat!( ")" ); -lazy_static! { - pub static ref PROJECT_NAME: String = env!("CARGO_CRATE_NAME").to_uppercase().to_string(); - pub static ref DATA_FOLDER: Option = - std::env::var(format!("{}_DATA", PROJECT_NAME.clone())) - .ok() - .map(PathBuf::from); - pub static ref CONFIG_FOLDER: Option = - std::env::var(format!("{}_CONFIG", PROJECT_NAME.clone())) - .ok() - .map(PathBuf::from); - pub static ref LOG_ENV: String = format!("{}_LOGLEVEL", PROJECT_NAME.clone()); - pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME")); -} - -fn project_directory() -> Option { - ProjectDirs::from("dev", "sachaos", env!("CARGO_PKG_NAME")) -} +pub static PROJECT_NAME: LazyLock = + LazyLock::new(|| env!("CARGO_CRATE_NAME").to_uppercase()); +pub static DATA_FOLDER: LazyLock> = LazyLock::new(|| { + std::env::var(format!("{}_DATA", *PROJECT_NAME)) + .ok() + .map(PathBuf::from) +}); +pub static CONFIG_FOLDER: LazyLock> = LazyLock::new(|| { + std::env::var(format!("{}_CONFIG", *PROJECT_NAME)) + .ok() + .map(PathBuf::from) +}); +pub static LOG_ENV: LazyLock = LazyLock::new(|| format!("{}_LOGLEVEL", *PROJECT_NAME)); +pub const LOG_FILE: &str = concat!(env!("CARGO_PKG_NAME"), ".log"); + +pub static PROJECT_DIRECTORY: LazyLock> = + LazyLock::new(|| ProjectDirs::from("dev", "sachaos", env!("CARGO_PKG_NAME"))); pub fn initialize_panic_handler() -> Result<()> { let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default() @@ -87,40 +86,37 @@ pub fn initialize_panic_handler() -> Result<()> { } pub fn get_data_dir() -> PathBuf { - let directory = if let Some(s) = DATA_FOLDER.clone() { - s - } else if let Some(proj_dirs) = project_directory() { + if let Some(s) = &*DATA_FOLDER { + s.clone() + } else if let Some(ref proj_dirs) = *PROJECT_DIRECTORY { proj_dirs.data_local_dir().to_path_buf() } else { PathBuf::from(".").join(".data") - }; - directory + } } pub fn get_config_dir() -> PathBuf { - let directory = if let Some(s) = CONFIG_FOLDER.clone() { - s - } else if let Some(proj_dirs) = project_directory() { + if let Some(s) = &*CONFIG_FOLDER { + s.clone() + } else if let Some(ref proj_dirs) = *PROJECT_DIRECTORY { proj_dirs.config_local_dir().to_path_buf() } else { PathBuf::from(".").join(".config") - }; - directory + } } pub fn get_old_config_dir() -> PathBuf { - let directory = if let Some(base_dirs) = BaseDirs::new() { + if let Some(base_dirs) = BaseDirs::new() { base_dirs.config_dir().to_path_buf() } else { PathBuf::from(".").join(".config") - }; - directory + } } pub fn initialize_logging() -> Result<()> { let directory = get_data_dir(); std::fs::create_dir_all(directory.clone())?; - let log_path = directory.join(LOG_FILE.clone()); + let log_path = directory.join(LOG_FILE); let log_file = std::fs::File::create(log_path)?; std::env::set_var( "RUST_LOG",