file_io/delete.rs
1use std::path::Path;
2
3/// Deletes a folder at the specified path if it exists.
4///
5/// # Arguments
6///
7/// * `path` - The path to the folder to delete (can be a `&str`, `String`, `Path`, or `PathBuf`).
8///
9/// # Panics
10///
11/// If some error is encountered while deleting the folder at `path`.
12///
13/// # Examples
14///
15/// ## Using a string literal
16///
17/// ```
18/// use file_io::{create_folder, delete_folder};
19/// use std::path::Path;
20///
21/// // Create a folder to delete later.
22/// let path: &str = "folder/subfolder_5";
23/// create_folder(path);
24///
25/// // Verify that the folder exists.
26/// assert!(Path::new(path).exists());
27///
28/// // Now delete the folder.
29/// delete_folder(path);
30///
31/// // Verify that the folder no longer exists.
32/// assert!(!Path::new(path).exists());
33/// ```
34///
35/// ## Using a `Path` reference
36///
37/// ```
38/// use file_io::{create_folder, delete_folder};
39/// use std::path::Path;
40///
41/// // Create a folder to delete later.
42/// let path: &Path = Path::new("folder/subfolder_5");
43/// create_folder(path);
44///
45/// // Verify that the folder exists.
46/// assert!(path.exists());
47///
48/// // Now delete the folder.
49/// delete_folder(path);
50///
51/// // Verify that the folder no longer exists.
52/// assert!(!path.exists());
53/// ```
54pub fn delete_folder<P: AsRef<Path>>(path: P) {
55 let path = path.as_ref();
56 if path.exists() {
57 std::fs::remove_dir_all(path)
58 .unwrap_or_else(|_| panic!("Failed to delete folder at '{path:?}'."));
59 }
60}
61
62/// Deletes a file at the specified path if it exists.
63///
64/// # Arguments
65///
66/// * `path` - The path to the file to delete (can be a `&str`, `String`, `Path`, or `PathBuf`).
67///
68/// # Panics
69///
70/// If some error is encountered while deleting the file at `path`.
71///
72/// # Examples
73///
74/// ## Using a string literal
75///
76/// ```
77/// use file_io::{delete_file, save_string_to_file};
78/// use std::path::Path;
79///
80/// // Create a file to delete later.
81/// let path: &str = "file_to_delete_1.txt";
82/// save_string_to_file("Hello, world!", path);
83///
84/// // Verify that the file exists.
85/// assert!(Path::new(path).exists());
86///
87/// // Now delete the file.
88/// delete_file(path);
89///
90/// // Verify that the file no longer exists.
91/// assert!(!Path::new(path).exists());
92/// ```
93///
94/// ## Using a `Path` reference
95///
96/// ```
97/// use file_io::{delete_file, save_string_to_file};
98/// use std::path::Path;
99///
100/// // Create a file to delete later.
101/// let path: &Path = Path::new("file_to_delete_2.txt");
102/// save_string_to_file("Hello, world!", path);
103///
104/// // Verify that the file exists.
105/// assert!(path.exists());
106///
107/// // Now delete the file.
108/// delete_file(path);
109///
110/// // Verify that the file no longer exists.
111/// assert!(!path.exists());
112/// ```
113pub fn delete_file<P: AsRef<Path>>(path: P) {
114 let path = path.as_ref();
115 if path.exists() {
116 std::fs::remove_file(path)
117 .unwrap_or_else(|_| panic!("Failed to delete file at '{path:?}'."));
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124 use crate::save::save_string_to_file;
125 use crate::test_utils::get_temp_dir_path;
126 use crate::to_path_buf;
127 use tempfile::tempdir;
128
129 #[test]
130 fn test_delete_file() {
131 // Create a temporary directory.
132 let temp_dir = tempdir().unwrap();
133
134 // Get the path to the temporary directory.
135 let temp_dir_path = get_temp_dir_path(&temp_dir);
136
137 // Path to the file to copy.
138 let file_path = temp_dir_path.join("file_to_copy.txt");
139
140 // File path in different formats.
141 let file_paths: Vec<Box<dyn AsRef<Path>>> = vec![
142 Box::new(file_path.to_str().unwrap()), // &str
143 Box::new(file_path.to_str().unwrap().to_string()), // String
144 Box::new(file_path.as_path()), // Path
145 Box::new(file_path.clone()), // PathBuf
146 ];
147
148 // Test with all different path formats.
149 for file_path in file_paths {
150 // Get a reference to this path representation (i.e. "unbox").
151 let file_path = file_path.as_ref();
152
153 // File path as a pathbuf.
154 let file_path_buf = to_path_buf(file_path);
155
156 // Check that the file does not exist before creating it.
157 assert!(!file_path_buf.exists());
158
159 // Create a file at the specified path.
160 save_string_to_file("Hello, world!", file_path);
161
162 // Verify that the file exists.
163 assert!(file_path_buf.exists());
164
165 // Now delete the file.
166 delete_file(file_path);
167
168 // Verify that the file no longer exists.
169 assert!(!file_path_buf.exists());
170 }
171 }
172}