From f29c854251ee0f47a94fb88fd94b9c24809c53a2 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Mon, 19 Aug 2019 09:10:34 -0500 Subject: fixed restore bug and wrote test for it, added ideas to readme, created all_paths function --- src/main.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index bb1701a..28c3acf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,14 @@ pub fn build_path(filename: &str) -> Result { .iter() .collect(); - fs::canonicalize(&relative) + if relative.exists() { + fs::canonicalize(&relative) + } else { + fs::File::create(relative.clone())?; + let full_path = fs::canonicalize(&relative.clone())?; + fs::remove_file(relative)?; + Ok(full_path) + } } pub fn rsycle(rsyclebin: PathBuf, old_path: PathBuf) -> Result<(), io::Error> { @@ -148,13 +155,7 @@ pub fn most_recent_current_path(rsyclebin: PathBuf, path: PathBuf) -> Result Result<(), io::Error> { - let mut paths: Vec = fs::read_dir(rsyclebin.clone()) - .unwrap() - .map(Result::unwrap) - .map(|dir| dir.path()) - .collect(); - - for current_path in paths.iter_mut() { + for current_path in all_paths(rsyclebin.clone())? { let mut filename: Vec<&str> = current_path .file_name() .unwrap() @@ -186,7 +187,9 @@ fn find_original_path(rsyclebin: PathBuf, current_path: PathBuf) -> Result Result Result<(), io::Error> { - let paths: Vec = fs::read_dir(rsyclebin.clone()) - .unwrap() - .map(Result::unwrap) - .map(|dir| dir.path()) - .collect(); - - for path in paths { + for path in all_paths(rsyclebin)? { if fs::remove_file(path.clone()).is_err() { fs::remove_dir_all(path)? } @@ -214,6 +211,13 @@ pub fn empty(rsyclebin: PathBuf) -> Result<(), io::Error> { Ok(()) } +fn all_paths(rsyclebin: PathBuf) -> Result, io::Error> { + Ok(fs::read_dir(rsyclebin.clone())? + .map(Result::unwrap) + .map(|dir| dir.path()) + .collect()) +} + fn restore_cli(_rsyclebin: PathBuf) { println!("unimplemented!") } -- cgit v1.2.3