applied util function for consistency

This commit is contained in:
2026-08-21 16:47:22 +01:00
parent 70289215a7
commit 175feb370a

View File

@@ -22,7 +22,7 @@ fn main() {
if let Some(arg) = args.next() {
match arg.as_str() {
"add" => add_files(&arg, &mut args, &mut current_shelf),
"rm" => remove_files(&mut args, &mut current_shelf),
"rm" => remove_files(&mut args, &mut current_shelf).unwrap(),
"ls" => list_library(&mut library),
"cp" => copy_files(
&mut args,
@@ -85,33 +85,21 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
return;
}
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
fn remove_files(
args: &mut env::Args,
mut shelf: &mut Shelf,
) -> Result<(), Box<dyn std::error::Error>> {
if shelf.content.len() < 1 {
println!(
"Shelf \"{}\" is Empty. Are you using the right shelf?",
shelf.name
);
return;
return Ok(());
}
if args.len() > 0 {
for file in args {
// If the argument is a number instead of a file name
let index: usize = match file.parse::<usize>() {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index]);
index
}
Err(_) => match utils::match_file_name(&file, shelf) {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index as usize]);
index
}
Err(e) => {
eprintln!("{e}");
process::exit(1)
}
},
};
let index = utils::match_file_name_or_index(&file, &mut shelf)?;
let file_path = Path::new(&shelf.content[index]);
println!(
"Removed file {} from shelf {}",
@@ -120,9 +108,11 @@ fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
);
shelf.content.remove(index);
}
return Ok(());
} else {
// If there's no file specified, clear the whole thing
shelf.content.clear()
shelf.content.clear();
return Ok(());
}
}
@@ -165,7 +155,11 @@ fn copy_files(args: &mut env::Args, binary: String, shelf: &mut Shelf) {
println!(
"Copying file {} to {}",
source.to_str().unwrap(),
destination.to_str().unwrap()
destination
.canonicalize()
.expect("couldn't cannonicalize")
.to_str()
.unwrap()
);
let _ = fs::copy(source, destination);
return;
@@ -185,7 +179,7 @@ fn copy_files(args: &mut env::Args, binary: String, shelf: &mut Shelf) {
};
let mut get_old_file = |old_file: Option<String>| {
let old_file_index = match utils::match_file_name(&old_file.unwrap(), shelf) {
let old_file_index = match utils::match_file_name_or_index(&old_file.unwrap(), shelf) {
Ok(index) => index,
Err(e) => {
eprintln!("{e}");