From: Joshua Simmons Date: Sun, 20 Aug 2023 15:46:50 +0000 (+0200) Subject: narcissus-core: Add helpers to fetch important directories X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=e7c1f47e45d6e43eae9787e9835541e43c872812;p=josh%2Fnarcissus narcissus-core: Add helpers to fetch important directories --- diff --git a/libs/narcissus-core/src/directory.rs b/libs/narcissus-core/src/directory.rs new file mode 100644 index 0000000..9469996 --- /dev/null +++ b/libs/narcissus-core/src/directory.rs @@ -0,0 +1,81 @@ +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; + +/// Returns the path to the user's cache directory. +/// +/// | Platform | | +/// | -------- | -----------------------------------| +/// | Linux | `$XDG_CACHE_DIR` or `$HOME/.cache` | +pub fn cache_dir() -> Option<&'static Path> { + static DIR: OnceLock> = OnceLock::new(); + + #[cfg(target_os = "linux")] + *DIR.get_or_init(|| { + let path = if let Some(path) = std::env::var_os("XDG_CACHE_DIR") { + path.into() + } else { + let mut path: PathBuf = std::env::var_os("HOME")?.into(); + path.push(".cache"); + path + }; + Some(Box::leak(path.into_boxed_path())) + }) +} + +/// Returns the path to the user's config directory. +/// +/// | Platform | | +/// | -------- | --------------------------------------| +/// | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | +pub fn config_dir() -> Option<&'static Path> { + static DIR: OnceLock> = OnceLock::new(); + + #[cfg(target_os = "linux")] + *DIR.get_or_init(|| { + let path = if let Some(path) = std::env::var_os("XDG_CONFIG_HOME") { + path.into() + } else { + let mut path: PathBuf = std::env::var_os("HOME")?.into(); + path.push(".config"); + path + }; + Some(Box::leak(path.into_boxed_path())) + }) +} + +/// Returns the path to the user's data directory. +/// +/// | Platform | | +/// | -------- | -----------------------------------------| +/// | Linux | `$XDG_DATA_HOME` or `$HOME/.local/share` | +pub fn data_dir() -> Option<&'static Path> { + static DIR: OnceLock> = OnceLock::new(); + + #[cfg(target_os = "linux")] + *DIR.get_or_init(|| { + let path = if let Some(path) = std::env::var_os("XDG_DATA_HOME") { + path.into() + } else { + let mut path: PathBuf = std::env::var_os("HOME")?.into(); + path.push(".local"); + path.push("share"); + path + }; + Some(Box::leak(path.into_boxed_path())) + }) +} + +/// Returns the path to the user's runtime directory. +/// +/// | Platform | | +/// | -------- | -------------------| +/// | Linux | `$XDG_RUNTIME_DIR` | +pub fn runtime_dir() -> Option<&'static Path> { + static DIR: OnceLock> = OnceLock::new(); + + #[cfg(target_os = "linux")] + *DIR.get_or_init(|| { + let path: PathBuf = std::env::var_os("XDG_RUNTIME_DIR")?.into(); + Some(Box::leak(path.into_boxed_path())) + }) +} diff --git a/libs/narcissus-core/src/lib.rs b/libs/narcissus-core/src/lib.rs index ebe0443..1e23a6b 100644 --- a/libs/narcissus-core/src/lib.rs +++ b/libs/narcissus-core/src/lib.rs @@ -1,5 +1,6 @@ mod arena; mod bitset; +mod directory; mod finite; mod fixed_vec; mod libc; @@ -21,6 +22,8 @@ mod widen; pub use arena::{Arena, HybridArena}; pub use bitset::BitIter; +pub use directory::{cache_dir, config_dir, data_dir, runtime_dir}; +pub use finite::{FiniteF32, FiniteF64, NotFiniteError}; pub use fixed_vec::FixedVec; pub use mutex::Mutex; pub use pool::{Handle, Pool}; @@ -28,9 +31,6 @@ pub use ref_count::{Arc, Rc}; pub use uuid::Uuid; pub use virtual_mem::{virtual_commit, virtual_free, virtual_reserve}; pub use virtual_vec::{VirtualDeque, VirtualVec}; - -pub use finite::{FiniteF32, FiniteF64, NotFiniteError}; - pub use widen::Widen; use std::{ffi::CStr, mem::MaybeUninit};