From 6b7337d26d9227dbce695d820a1bbb5f2c8afd7f Mon Sep 17 00:00:00 2001 From: Kevin Liu Date: Sun, 2 Oct 2016 03:25:24 -0700 Subject: [PATCH] Refactoring --- src/main.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2b7e84f..a0003d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,8 +65,8 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.") } if matches.is_present("resurrect") { - let histfile: PathBuf = graveyard.join(HISTFILE); - if let Ok(s) = read_last_line(&histfile) { + let histfile: &Path = &graveyard.join(HISTFILE); + if let Ok(s) = read_last_line(histfile) { let mut tokens = StrExt::split(s.as_str(), "\t"); let dest = tokens.next().expect("Bad histfile format: column A"); let source = tokens.next().expect("Bad histfile format: column B"); @@ -74,12 +74,11 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.") println!("ERROR: {}: {}", e, source); println!("Maybe the file was removed from the graveyard."); if prompt_yes("Remove it from the history?") { - delete_last_line(&histfile).unwrap(); + delete_last_line(histfile).unwrap(); } - } else { println!("Returned {} to {}", source, dest); - delete_last_line(&histfile).expect("Failed to remove history"); + delete_last_line(histfile).expect("Failed to remove history"); } } return; @@ -253,7 +252,7 @@ fn prompt_yes(prompt: &str) -> bool { false } -fn read_last_line(path: &PathBuf) -> std::io::Result { +fn read_last_line(path: &Path) -> std::io::Result { match fs::File::open(path) { Ok(f) => BufReader::new(f).lines().last().expect("Empty histfile"), Err(e) => Err(e) @@ -262,7 +261,7 @@ fn read_last_line(path: &PathBuf) -> std::io::Result { /// Set the length of the file to the difference between the size of the file /// and the size of last line of the file. -fn delete_last_line(path: &PathBuf) -> std::io::Result<()> { +fn delete_last_line(path: &Path) -> std::io::Result<()> { match fs::OpenOptions::new().write(true).open(path) { Ok(f) => { let total: u64 = f.metadata().expect("Failed to stat file").len();