diff --git a/src/main.rs b/src/main.rs index 5abca2b..0adf734 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,28 +41,19 @@ fn read_current_dir() -> Vec { fn lock_dir() { if Path::new(LOCK_FILE).is_file() { - panic!("Lock file \"{}\" exists!", LOCK_FILE); + panic!("Lock file \"{}\" already exists!", LOCK_FILE); } - let _file = match File::create(LOCK_FILE) { - Ok(file) => file, - Err(_) => panic!("Could not create lock file: {}", LOCK_FILE), - }; + let _file = File::create(LOCK_FILE).expect("Could not create LOCK_FILE"); } fn unlock_dir() { if Path::new(LOCK_FILE).is_file() { - let _result = match fs::remove_file(LOCK_FILE){ - Ok(result) => result, - Err(_) => panic!("Could not remove lock file: {}", LOCK_FILE), - }; + let _result = fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE"); } } fn create_temp_file(paths: &Vec) -> String { - let mut tmpfile = match NamedTempFile::new(){ - Ok(file) => file, - Err(e) => panic!("Could not create tempfile: {}", e), - }; + let mut tmpfile = NamedTempFile::with_suffix(".mvwrap").expect("Could not create tempfile"); for path in paths { let _ = writeln!(tmpfile, "{}", path); @@ -70,20 +61,14 @@ fn create_temp_file(paths: &Vec) -> String { let filepath = tmpfile.path().display().to_string(); - let _ = match tmpfile.keep(){ - Ok(res) => res, - Err(e) => panic!("Can't keep tempfile: {}", e), - }; + tmpfile.keep().expect("Can't keep tempfile"); filepath } fn remove_temp_file(tmpfile: &String) { if Path::new(tmpfile).is_file() { - let _result = match fs::remove_file(tmpfile){ - Ok(result) => result, - Err(_) => panic!("Could not remove tempfile: {}", tmpfile), - }; + let _result = fs::remove_file(tmpfile).expect("Could not remove tempfile"); } } @@ -159,13 +144,32 @@ fn run_checks(src_paths: &Vec, dst_paths: &Vec) -> bool { // Make sure all destination files are unique let dst_paths_length_unique = unique_length(&dst_paths); if dst_paths.len() != dst_paths_length_unique { - println!("Clashing destination names!"); - // TODO: show which ones! + println!("ERROR: You're trying to move multiple files to the same name."); + show_doubles(&src_paths, &dst_paths); return true } false } +fn show_doubles(src_paths: &Vec, dst_paths: &Vec) { + let mut paths = HashMap::new(); + let dst_len = dst_paths.len(); + + for i in 0..dst_len { + paths.entry(&dst_paths[i]).or_insert(Vec::new()); + paths.get_mut(&dst_paths[i]).unwrap().push(i); + } + + for (path, lines) in paths.iter() { + if lines.len() > 1 { + for i in lines.iter() { + println!("[line: {}] {} -> {}", i+1, src_paths[*i], path); + } + } + } + +} + fn main() { // Place lockfile to not have multiple mvw processes running in the same dir at the same time lock_dir(); @@ -183,9 +187,7 @@ fn main() { let mut new_paths = read_temp_file(&temp_file); - // Compare length, if not equal offer to try again (or panic for now) while run_checks(&paths, &new_paths) { - let confirmation = Confirm::new() .with_prompt("Continue editing?") .interact() @@ -202,7 +204,6 @@ fn main() { // (also don't overwrite existing files that are not in the input list!) - move_safely(&paths, &new_paths); //display_temp_file(&temp_file);