Skip to content

Commit 1bd913d

Browse files
committed
update the erro handle
1 parent 606a592 commit 1bd913d

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rmxt"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2024"
55
authors = ["Santosh Shrestha <santoshxshrestha@gmail.com> "]
66
description = "A replacement for the 'rm' command with a trash feature for safer file deletion."

src/main.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ use clap::Parser;
44
use dirs::home_dir;
55
use 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+
745
fn 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

Comments
 (0)