]> git.nega.tv - josh/narcissus/commitdiff
narcissus-core: Add helpers to fetch important directories
authorJoshua Simmons <josh@nega.tv>
Sun, 20 Aug 2023 15:46:50 +0000 (17:46 +0200)
committerJoshua Simmons <josh@nega.tv>
Sun, 20 Aug 2023 15:46:50 +0000 (17:46 +0200)
libs/narcissus-core/src/directory.rs [new file with mode: 0644]
libs/narcissus-core/src/lib.rs

diff --git a/libs/narcissus-core/src/directory.rs b/libs/narcissus-core/src/directory.rs
new file mode 100644 (file)
index 0000000..9469996
--- /dev/null
@@ -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<Option<&'static Path>> = 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<Option<&'static Path>> = 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<Option<&'static Path>> = 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<Option<&'static Path>> = 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()))
+    })
+}
index ebe04439ace3291edc4e86c2ae8a6f2cb7e8da72..1e23a6bff92f50bb890fc8e44b40334f9e0bd43b 100644 (file)
@@ -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};