@@ -4,6 +4,44 @@ use clap::Parser;
44use dirs:: home_dir;
55use std:: fs:: { self , rename} ;
66
7+ pub fn get_trash_directory ( ) -> std:: path:: PathBuf {
8+ match home_dir ( ) {
9+ Some ( dir) => dir. join ( ".trash/" ) ,
10+ None => panic ! ( "Could not find home directory" ) ,
11+ }
12+ }
13+
14+ pub fn tidy_trash_directory ( ) {
15+ let trash = get_trash_directory ( ) ;
16+
17+ match fs:: metadata ( & trash) {
18+ Ok ( metadata) => {
19+ if metadata. is_dir ( ) {
20+ match fs:: remove_dir_all ( & trash) {
21+ Ok ( _) => {
22+ println ! ( "Trash directory cleaned." ) ;
23+
24+ if let Err ( e) = fs:: create_dir_all ( & trash) {
25+ eprintln ! ( "Error recreating trash directory: {e}" ) ;
26+ }
27+ }
28+ Err ( e) => eprintln ! ( "Error tidying trash directory: {e}" ) ,
29+ }
30+ } else {
31+ eprintln ! ( "Path exists but is not a directory." ) ;
32+ }
33+ }
34+ Err ( e) => {
35+ println ! ( "Trash directory does not exist or cannot be accessed: {e}" ) ;
36+ println ! ( "Creating trash directory at: {:?}" , & trash) ;
37+
38+ if let Err ( e) = fs:: create_dir_all ( & trash) {
39+ eprintln ! ( "Error creating trash directory: {e}" ) ;
40+ }
41+ }
42+ }
43+ }
44+
745fn main ( ) {
846 // parsing the args
947 let args = Args :: parse ( ) ;
@@ -14,15 +52,12 @@ fn main() {
1452 let tidy = args. tidy ;
1553 let dir = args. dir ;
1654
17- // getting the home directory and appending .trash to it
18- let trash = match home_dir ( ) {
19- Some ( dir) => dir. join ( ".trash/" ) ,
20- None => panic ! ( "Could not find home directory" ) ,
21- } ;
55+ let trash = get_trash_directory ( ) ;
2256
2357 // tidying the trash directory if the flag is set
2458 if tidy {
25- fs:: remove_dir_all ( & trash) . unwrap ( ) ;
59+ tidy_trash_directory ( ) ;
60+ return ;
2661 }
2762
2863 // creating the trash directory if it doesn't exist
0 commit comments