ombre: Add `tempdir` module
df755097b82ac837d9c43c2f865780a935e019a6
Create temporary directories.
1 parent
e0a580f0
ombre/src/lib.rs
+1 -0
| 21 | 21 | #[cfg(feature = "readline")] |
|
| 22 | 22 | pub mod readline; |
|
| 23 | 23 | pub mod rng; |
|
| 24 | 24 | pub mod runtime; |
|
| 25 | 25 | pub mod string; |
|
| 26 | + | pub mod tempdir; |
|
| 26 | 27 | pub mod text; |
|
| 27 | 28 | #[cfg(feature = "ui")] |
|
| 28 | 29 | pub mod ui; |
|
| 29 | 30 | ||
| 30 | 31 | #[cfg(feature = "derive")] |
ombre/src/tempdir.rs
added
+80 -0
| 1 | + | //! Temporary directory management. |
|
| 2 | + | //! |
|
| 3 | + | //! This module provides functionality for creating and managing temporary directories |
|
| 4 | + | //! that are automatically cleaned up when they go out of scope. |
|
| 5 | + | use std::fs; |
|
| 6 | + | use std::path::{Path, PathBuf}; |
|
| 7 | + | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 8 | + | use std::{env, io, process}; |
|
| 9 | + | ||
| 10 | + | /// A temporary directory that is automatically deleted when dropped. |
|
| 11 | + | /// |
|
| 12 | + | /// The directory is created with a unique name based on the process ID and |
|
| 13 | + | /// current timestamp. It is automatically removed when the `TempDir` instance |
|
| 14 | + | /// is dropped, including all its contents. |
|
| 15 | + | pub struct TempDir { |
|
| 16 | + | path: PathBuf, |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | impl TempDir { |
|
| 20 | + | /// Creates a new temporary directory. |
|
| 21 | + | /// |
|
| 22 | + | /// The directory is created in the system's temporary directory location |
|
| 23 | + | /// with a unique name generated from the process ID and current timestamp. |
|
| 24 | + | /// |
|
| 25 | + | /// # Errors |
|
| 26 | + | /// |
|
| 27 | + | /// - If the system time cannot be retrieved. |
|
| 28 | + | /// - If the temporary directory cannot be created. |
|
| 29 | + | /// |
|
| 30 | + | pub fn new() -> io::Result<Self> { |
|
| 31 | + | let pid = process::id(); |
|
| 32 | + | let nanos = SystemTime::now() |
|
| 33 | + | .duration_since(UNIX_EPOCH) |
|
| 34 | + | .map_err(|e| io::Error::new(io::ErrorKind::Other, e))? |
|
| 35 | + | .as_nanos(); |
|
| 36 | + | let base = env::temp_dir(); |
|
| 37 | + | let path = base.join(format!("{pid}.{nanos}")); |
|
| 38 | + | let _ = fs::create_dir(&path)?; |
|
| 39 | + | ||
| 40 | + | Ok(TempDir { path }) |
|
| 41 | + | } |
|
| 42 | + | ||
| 43 | + | /// Returns a reference to the path of the temporary directory. |
|
| 44 | + | pub fn path(&self) -> &Path { |
|
| 45 | + | &self.path |
|
| 46 | + | } |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | impl Drop for TempDir { |
|
| 50 | + | fn drop(&mut self) { |
|
| 51 | + | fs::remove_dir_all(&self.path).ok(); |
|
| 52 | + | } |
|
| 53 | + | } |
|
| 54 | + | ||
| 55 | + | #[cfg(test)] |
|
| 56 | + | #[allow(clippy::unwrap_used)] |
|
| 57 | + | mod tests { |
|
| 58 | + | use super::*; |
|
| 59 | + | ||
| 60 | + | #[test] |
|
| 61 | + | fn test_tempdir_creation_and_cleanup() { |
|
| 62 | + | let temp_dir = TempDir::new().unwrap(); |
|
| 63 | + | let path = temp_dir.path().to_path_buf(); |
|
| 64 | + | ||
| 65 | + | assert!(path.exists()); |
|
| 66 | + | assert!(path.is_dir()); |
|
| 67 | + | drop(temp_dir); |
|
| 68 | + | assert!(!path.exists()); |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | #[test] |
|
| 72 | + | fn test_multiple_tempdirs() { |
|
| 73 | + | let dir1 = TempDir::new().unwrap(); |
|
| 74 | + | let dir2 = TempDir::new().unwrap(); |
|
| 75 | + | ||
| 76 | + | assert!(dir1.path() != dir2.path()); |
|
| 77 | + | assert!(dir1.path().exists()); |
|
| 78 | + | assert!(dir2.path().exists()); |
|
| 79 | + | } |
|
| 80 | + | } |