use crate::env::EnvError;
use std::env;
use std::path::PathBuf;
use std::sync::OnceLock;
pub static APP_ENV: OnceLock<AppEnv> = OnceLock::new();
#[derive(Debug)]
pub struct AppEnv {
pub app_file_path: PathBuf,
pub app_file_path_without_ext: PathBuf,
pub app_dir: PathBuf,
pub app_file_name: String,
pub app_file_name_without_ext: String,
}
pub fn init_env() -> Result<(), EnvError> {
let app_file_path = env::current_exe().map_err(EnvError::GetAppPath)?;
let mut app_file_path_without_ext = app_file_path.clone();
app_file_path_without_ext.pop();
let mut app_dir = app_file_path.clone();
app_dir.pop();
let app_file_name = app_file_path
.file_name()
.ok_or(EnvError::GetAppFileName())?
.to_string_lossy()
.to_string();
let app_file_name_without_ext = app_file_path
.file_stem()
.ok_or(EnvError::GetAppFileName())?
.to_string_lossy()
.to_string();
let env = AppEnv {
app_file_path,
app_file_path_without_ext,
app_dir,
app_file_name,
app_file_name_without_ext,
};
APP_ENV.set(env).map_err(|_| EnvError::SetAppEnv())?;
Ok(())
}