|
| 1 | +//! Common Directories that are outside of install dir |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + env, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +#[macro_export] |
| 9 | +/// Declare a statically allocated `OnceLock` path, and create that directory if it does not exists. |
| 10 | +macro_rules! get_path_and_create { |
| 11 | + ($path_ident:ident, $init:expr) => {{ |
| 12 | + static $path_ident: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new(); |
| 13 | + $path_ident.get_or_init(|| { |
| 14 | + let __path__ = $init; |
| 15 | + $crate::utils::ensure_dir(&__path__) |
| 16 | + .expect("unable to create one of the directory under installation folder"); |
| 17 | + __path__ |
| 18 | + }) |
| 19 | + }}; |
| 20 | +} |
| 21 | + |
| 22 | +/// Get a path to user's "home" directory. |
| 23 | +/// |
| 24 | +/// The home directory is determined by a combination of ways |
| 25 | +/// with a fallback order: |
| 26 | +/// |
| 27 | +/// 1. The `HOME` environment variable |
| 28 | +/// 2. The `USERPROFILE` environment variable (Windows only) |
| 29 | +/// 3. The [`home_dir`](dirs::home_dir) function of the `dirs` crate |
| 30 | +/// |
| 31 | +/// # Panic |
| 32 | +/// |
| 33 | +/// Will panic if such directory cannot be determined by neither the `HOME` env var |
| 34 | +/// nor [`dirs::home_dir`] function. |
| 35 | +pub fn home_dir() -> PathBuf { |
| 36 | + let base = env::var_os("HOME").filter(|oss| !oss.is_empty()); |
| 37 | + #[cfg(windows)] |
| 38 | + let base = base.or_else(|| env::var_os("USERPROFILE").filter(|oss| !oss.is_empty())); |
| 39 | + |
| 40 | + base.map(PathBuf::from) |
| 41 | + .unwrap_or_else(|| dirs::home_dir().expect("home directory cannot be determined.")) |
| 42 | +} |
| 43 | + |
| 44 | +/// (User) Configuration directory to store all our configs. |
| 45 | +/// |
| 46 | +/// Note: This dir will be stored under OS's `config_dir`, which can be: |
| 47 | +/// - `$HOME/.config/rim` on Linux |
| 48 | +/// - `$HOME/Library/Application Support/rim` on macOS |
| 49 | +/// - `$HOME\AppData\Roaming\rim` on Windows |
| 50 | +/// |
| 51 | +/// # Panic |
| 52 | +/// Panic if the OS's config directory cannot be determined, which typically meaning |
| 53 | +/// that the `HOME` env var is missing and the current OS is not support by the [`dirs`] crate. |
| 54 | +pub fn rim_config_dir() -> &'static Path { |
| 55 | + get_path_and_create!(RIM_CONFIG_DIR, { |
| 56 | + let mut config_root = home_dir(); |
| 57 | + |
| 58 | + cfg_if::cfg_if! { |
| 59 | + if #[cfg(target_os = "linux")] { |
| 60 | + config_root.push(".config") |
| 61 | + } else if #[cfg(windows)] { |
| 62 | + config_root.push("AppData"); |
| 63 | + config_root.push("Roaming"); |
| 64 | + } else if #[cfg(target_os = "macos")] { |
| 65 | + config_root.push("Library"); |
| 66 | + config_root.push("Application Support"); |
| 67 | + } else { |
| 68 | + // fallback to directly use `dir::config_dir`. |
| 69 | + // The reason we not directly using it was because we can |
| 70 | + // support mocked test on Windows by setting the `HOME` env var. |
| 71 | + dirs::config_dir().expect( |
| 72 | + "unable to determine config directory, maybe your OS is not supported" |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + config_root.push("rim"); |
| 77 | + config_root |
| 78 | + }) |
| 79 | +} |
0 commit comments