From 175feb370a2ea2e9ee95eb0f9dcf13447e34aa7c Mon Sep 17 00:00:00 2001 From: voidarc Date: Fri, 21 Aug 2026 16:47:22 +0100 Subject: [PATCH] applied util function for consistency --- src/main.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c97e12..5ba4266 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { 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::() { - 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| { - 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}");