|
| 1 | +//! A super small crate which contains [AutoDeletePath](struct.AutoDeletePath.html), |
| 2 | +//! a path which gets automatically deleted when it goes out of scope. |
| 3 | +//! |
| 4 | +//! # Examples |
| 5 | +//! ``` |
| 6 | +//! { |
| 7 | +//! let tmp_path = auto_delete_path::AutoDeletePath::temp(); // creates a new path at the default temp folder |
| 8 | +//! std::fs::create_dir(&tmp_path); // AutoDeletePath implements AsRef<Path> |
| 9 | +//! let subfile = tmp_path.as_ref().join("subfile"); // create a subfile |
| 10 | +//! std::fs::File::create(&subfile).unwrap(); |
| 11 | +//! } // tmp_path dies here, so the directory and its contents will be deleted |
| 12 | +//!``` |
| 13 | +//! |
| 14 | +//! See [AutoDeletePath](struct.AutoDeletePath.html) and [include_to_auto_delete_path](macro.include_to_auto_delete_path.html) |
| 15 | +//! for more examples. |
| 16 | +
|
| 17 | +use std::{ |
| 18 | + path::{Path, PathBuf}, |
| 19 | + sync::atomic::{AtomicU16, Ordering}, |
| 20 | +}; |
| 21 | + |
| 22 | +/// Macro for including a source file, and writing it to a new `AutoDeletePath::temp`. |
| 23 | +/// |
| 24 | +/// Useful for testing. |
| 25 | +/// |
| 26 | +/// # Panics |
| 27 | +/// |
| 28 | +/// Panics if writing to the tempfile fails. |
| 29 | +/// |
| 30 | +/// # Example |
| 31 | +/// |
| 32 | +/// ``` |
| 33 | +/// let tmp_path = auto_delete_path::include_to_auto_delete_path!("test-resources/test-include.txt"); |
| 34 | +/// assert_eq!(std::fs::read_to_string(&tmp_path).unwrap(), "Included file!\n"); |
| 35 | +/// ``` |
| 36 | +#[macro_export] |
| 37 | +macro_rules! include_to_auto_delete_path { |
| 38 | + ($file:expr) => {{ |
| 39 | + use std::io::Write; |
| 40 | + |
| 41 | + let tmp_path = $crate::AutoDeletePath::temp(); |
| 42 | + let file_bytes = include_bytes!($file); |
| 43 | + let mut file = std::fs::File::create(&tmp_path).unwrap(); |
| 44 | + file.write_all(file_bytes).unwrap(); |
| 45 | + tmp_path |
| 46 | + }}; |
| 47 | +} |
| 48 | + |
| 49 | +/// This struct simply holds an instance of `std::path::PathBuf`. |
| 50 | +/// However, when such an instance goes out of scope and is destroyed, its destructor will be called, |
| 51 | +/// which attempts to delete the owned path (either file or directory). |
| 52 | +/// |
| 53 | +/// This works even if the program panics. |
| 54 | +/// |
| 55 | +/// Useful for creating temporary files that you want to be deleted automatically. |
| 56 | +pub struct AutoDeletePath { |
| 57 | + path: PathBuf, |
| 58 | +} |
| 59 | + |
| 60 | +impl AutoDeletePath { |
| 61 | + /// Creates an AutoDeletePath in the default temp directory. |
| 62 | + /// This method just returns a path; If you want to actually make a file or folder, |
| 63 | + /// you have to do that manually. |
| 64 | + /// |
| 65 | + /// # Examples |
| 66 | + /// |
| 67 | + /// File: |
| 68 | + /// ``` |
| 69 | + /// let mut temp_path_clone = std::path::PathBuf::new(); |
| 70 | + /// { |
| 71 | + /// let temp_path = auto_delete_path::AutoDeletePath::temp(); |
| 72 | + /// temp_path_clone = temp_path.as_ref().to_owned(); |
| 73 | + /// assert!(!temp_path_clone.exists()); |
| 74 | + /// std::fs::write(&temp_path, "spam").unwrap(); |
| 75 | + /// assert!(temp_path_clone.exists()); |
| 76 | + /// } // temp_path dies here, so the file is deleted |
| 77 | + /// assert!(!temp_path_clone.exists()); |
| 78 | + /// ``` |
| 79 | + /// |
| 80 | + /// Directory: |
| 81 | + /// ``` |
| 82 | + /// let mut temp_path_clone = std::path::PathBuf::new(); |
| 83 | + /// { |
| 84 | + /// let temp_path = auto_delete_path::AutoDeletePath::temp(); |
| 85 | + /// temp_path_clone = temp_path.as_ref().to_owned(); |
| 86 | + /// assert!(!temp_path_clone.exists()); |
| 87 | + /// std::fs::create_dir(&temp_path).unwrap(); |
| 88 | + /// assert!(temp_path_clone.exists()); |
| 89 | + /// } // temp_path dies here, so the directory is deleted |
| 90 | + /// assert!(!temp_path_clone.exists()); |
| 91 | + /// ``` |
| 92 | + pub fn temp() -> Self { |
| 93 | + Self { |
| 94 | + path: create_temp_path(), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl std::convert::AsRef<Path> for AutoDeletePath { |
| 100 | + fn as_ref(&self) -> &Path { |
| 101 | + &self.path |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl Drop for AutoDeletePath { |
| 106 | + fn drop(&mut self) { |
| 107 | + if self.path.is_dir() { |
| 108 | + std::fs::remove_dir_all(&self.path).ok(); |
| 109 | + } else { |
| 110 | + std::fs::remove_file(&self.path).ok(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +static PATH_COUNT: AtomicU16 = AtomicU16::new(1); |
| 116 | + |
| 117 | +/// Creates a random path at the default temp directory (usually /tmp). |
| 118 | +fn create_temp_path() -> PathBuf { |
| 119 | + create_temp_path_at_directory(std::env::temp_dir()) |
| 120 | +} |
| 121 | + |
| 122 | +/// Creates a random path at the specified directory. |
| 123 | +fn create_temp_path_at_directory<P: AsRef<Path>>(directory: P) -> PathBuf { |
| 124 | + PathBuf::from(format!( |
| 125 | + "{}/rustytemp-{}", |
| 126 | + directory.as_ref().display(), |
| 127 | + PATH_COUNT.fetch_add(1, Ordering::Relaxed) |
| 128 | + )) |
| 129 | +} |
0 commit comments