Skip to main content

Pull the bulk of the logic out into a standalone function

ID
3776780
date
2024-06-18 20:41:46+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
c53d37b
message
Pull the bulk of the logic out into a standalone function
changed files
1 file, 14 additions, 3 deletions

Changed files

src/main.rs (959) → src/main.rs (1227)

diff --git a/src/main.rs b/src/main.rs
index e770817..197ee20 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,21 +1,26 @@
 #![deny(warnings)]
 
 use std::fs;
+use std::path::Path;
 
 use colored::*;
 use walkdir::WalkDir;
 
 mod can_be_deleted;
 
-fn main() -> Result<(), std::io::Error> {
-    let iterator = WalkDir::new(".")
+/// Recurse through a given root directory, and delete any "empty" directories.
+///
+/// Returns the number of directories deleted.
+///
+fn emptydir(root: &Path) -> u32 {
+    let iterator = WalkDir::new(root)
         .contents_first(true)
         .into_iter()
         .filter_map(|e| e.ok())
         .filter(|e| e.file_type().is_dir())
         .filter(|e| can_be_deleted::can_be_deleted(e.path()));
 
-    let mut count_deleted = 0;
+    let mut count_deleted: u32 = 0;
 
     for entry in iterator {
         match fs::remove_dir_all(entry.path()) {
@@ -27,6 +32,12 @@ fn main() -> Result<(), std::io::Error> {
         };
     }
 
+    count_deleted
+}
+
+fn main() -> Result<(), std::io::Error> {
+    let count_deleted = emptydir(Path::new("."));
+
     match count_deleted {
         0 => println!("{}", "No empty directories found".blue()),
         1 => println!("{}", "1 directory deleted".green()),