aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/res/dirs/temp_dir.rs
diff options
context:
space:
mode:
Diffstat (limited to 'mingling/src/res/dirs/temp_dir.rs')
-rw-r--r--mingling/src/res/dirs/temp_dir.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/mingling/src/res/dirs/temp_dir.rs b/mingling/src/res/dirs/temp_dir.rs
new file mode 100644
index 0000000..f4f0dca
--- /dev/null
+++ b/mingling/src/res/dirs/temp_dir.rs
@@ -0,0 +1,77 @@
+use std::{
+ env::temp_dir,
+ path::{Path, PathBuf},
+};
+
+/// A global resource that provides the system temporary directory path.
+///
+/// This struct wraps a `PathBuf` representing the platform's temporary directory
+/// (e.g., `/tmp` on Unix, `%TEMP%` on Windows).
+///
+/// Note that `std::env::temp_dir()` is infallible — it always returns a path,
+/// falling back to a platform-specific default (e.g., `/tmp` on Unix) if the
+/// environment variable is not set.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ResTempDir {
+ tmp: PathBuf,
+}
+
+impl ResTempDir {
+ /// Creates a new `ResTempDir` by querying the OS for the system temporary directory.
+ ///
+ /// This method is infallible since `std::env::temp_dir()` always succeeds,
+ /// returning a platform-specific default when environment variables are unset.
+ pub fn new() -> Self {
+ Self { tmp: temp_dir() }
+ }
+}
+
+impl Default for ResTempDir {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl From<PathBuf> for ResTempDir {
+ fn from(path: PathBuf) -> Self {
+ Self { tmp: path }
+ }
+}
+
+impl From<&Path> for ResTempDir {
+ fn from(path: &Path) -> Self {
+ Self { tmp: path.to_path_buf() }
+ }
+}
+
+impl From<&PathBuf> for ResTempDir {
+ fn from(path: &PathBuf) -> Self {
+ Self { tmp: path.clone() }
+ }
+}
+
+impl From<ResTempDir> for PathBuf {
+ fn from(res: ResTempDir) -> Self {
+ res.tmp
+ }
+}
+
+impl AsRef<Path> for ResTempDir {
+ fn as_ref(&self) -> &Path {
+ self.tmp.as_path()
+ }
+}
+
+impl std::ops::Deref for ResTempDir {
+ type Target = PathBuf;
+
+ fn deref(&self) -> &Self::Target {
+ &self.tmp
+ }
+}
+
+impl std::ops::DerefMut for ResTempDir {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.tmp
+ }
+}