Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lazy-static #142

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
62 changes: 29 additions & 33 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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<PathBuf> =
std::env::var(format!("{}_DATA", PROJECT_NAME.clone()))
.ok()
.map(PathBuf::from);
pub static ref CONFIG_FOLDER: Option<PathBuf> =
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> {
ProjectDirs::from("dev", "sachaos", env!("CARGO_PKG_NAME"))
}
pub static PROJECT_NAME: LazyLock<String> =
LazyLock::new(|| env!("CARGO_CRATE_NAME").to_uppercase());
pub static DATA_FOLDER: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
std::env::var(format!("{}_DATA", *PROJECT_NAME))
.ok()
.map(PathBuf::from)
});
pub static CONFIG_FOLDER: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
std::env::var(format!("{}_CONFIG", *PROJECT_NAME))
.ok()
.map(PathBuf::from)
});
pub static LOG_ENV: LazyLock<String> = LazyLock::new(|| format!("{}_LOGLEVEL", *PROJECT_NAME));
pub const LOG_FILE: &str = concat!(env!("CARGO_PKG_NAME"), ".log");

pub static PROJECT_DIRECTORY: LazyLock<Option<ProjectDirs>> =
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()
Expand Down Expand Up @@ -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",
Expand Down