Skip to main content

Better variable naming; tidy up the code

ID
daefd24
date
2024-06-20 09:59:32+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
a7779a5
message
Better variable naming; tidy up the code
changed files
2 files, 25 additions, 24 deletions

Changed files

src/can_be_deleted.rs (4211) → src/can_be_deleted.rs (4131)

diff --git a/src/can_be_deleted.rs b/src/can_be_deleted.rs
index bc0e670..c229c1f 100644
--- a/src/can_be_deleted.rs
+++ b/src/can_be_deleted.rs
@@ -1,18 +1,22 @@
 use std::collections::HashSet;
+use std::ffi::OsString;
 use std::fs;
 use std::io;
 use std::path::Path;
 
-/// Given a Result<DirEntry> from `fs::read_dir()`, try to get the
-/// filename of the entry.
+/// Return the names of files/folders inside a directory.
 ///
-/// Filenames will be lowercased for easy comparisons.
+/// Names are lowercased for easy comparisons.
 ///
-fn file_name(dir_entry: io::Result<fs::DirEntry>) -> Option<String> {
-    match dir_entry.map(|e| e.file_name().into_string()) {
-        Ok(Ok(s)) => Some(s.to_lowercase()),
-        _ => None,
+fn get_names_in_directory(dir: &Path) -> io::Result<HashSet<OsString>> {
+    let mut names = Vec::new();
+
+    for entry in fs::read_dir(dir)? {
+        let entry = entry?;
+        names.push(entry.file_name().to_ascii_lowercase());
     }
+
+    Ok(HashSet::from_iter(names))
 }
 
 pub fn can_be_deleted(path: &Path) -> bool {
@@ -38,18 +42,15 @@ pub fn can_be_deleted(path: &Path) -> bool {
     // A directory is safe to delete if the ONLY things it contains are these entries;
     // any other entry should block the directory from being deleted.
     //
-    let deletable_entries: HashSet<Option<String>> = [".DS_Store", "__pycache__", ".venv"]
-        .iter()
-        .map(|&s| Some(s.to_lowercase().to_owned()))
-        .collect();
-
-    match fs::read_dir(path) {
-        Ok(entries) => {
-            let names: HashSet<Option<String>> = HashSet::from_iter(entries.map(|e| file_name(e)));
-
-            names.is_subset(&deletable_entries)
-        }
-        _ => false,
+    let deletable_names = HashSet::from([
+        OsString::from(".ds_store"),
+        OsString::from("__pycache__"),
+        OsString::from(".venv"),
+    ]);
+
+    match get_names_in_directory(path) {
+        Ok(names) => names.is_subset(&deletable_names),
+        Err(_) => false,
     }
 }
 

src/emptydir.rs (4301) → src/emptydir.rs (4326)

diff --git a/src/emptydir.rs b/src/emptydir.rs
index d2f749f..8ffcbe1 100644
--- a/src/emptydir.rs
+++ b/src/emptydir.rs
@@ -8,7 +8,7 @@ use walkdir::WalkDir;
 /// Returns the number of directories deleted.
 ///
 pub fn emptydir(root: &Path) -> u32 {
-    let iterator = WalkDir::new(root)
+    let directories_to_delete = WalkDir::new(root)
         .contents_first(true)
         .into_iter()
         .filter_map(|e| e.ok())
@@ -17,13 +17,13 @@ pub fn emptydir(root: &Path) -> u32 {
 
     let mut count_deleted: u32 = 0;
 
-    for entry in iterator {
-        match fs::remove_dir_all(entry.path()) {
+    for dir in directories_to_delete {
+        match fs::remove_dir_all(dir.path()) {
             Ok(_) => {
-                println!("{}", entry.path().display());
+                println!("{}", dir.path().display());
                 count_deleted += 1;
             }
-            _ => (),
+            Err(_) => (),
         };
     }