From 1c5f7db2f8b53650f9b00757f432201d9b952180 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 29 Jul 2023 12:36:00 +0200 Subject: Build a 3-tuple intead of mutating a 2-tuple --- src/main.rs | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 24bd8f5..b6170ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,10 +106,11 @@ fn work() -> Result<(), Error> { let mut uniq_srcs: HashSet = HashSet::with_capacity(srcs.len()); let mut uniq_dsts: HashSet = HashSet::with_capacity(dsts.len()); - let mut src_dst = srcs + let dir = tempdir()?; + let mut ps = srcs .iter() .zip(dsts) - .map(|(s, d)| -> Result<(PathBuf, PathBuf), Error> { + .map(|(s, d)| -> Result<(PathBuf, PathBuf, PathBuf), Error> { let s = fs::canonicalize(s)?; let d = env::current_dir()?.join(Path::new(&d)); let d = normalize_path(&d); @@ -119,37 +120,32 @@ fn work() -> Result<(), Error> { } else if !uniq_dsts.insert(d.clone()) { Err(Error::DuplicateOutput(d)) } else { - Ok((s, d)) + let mut hasher = DefaultHasher::new(); + s.hash(&mut hasher); + let file = hasher.finish().to_string(); + let t = dir.path().join(&file); + Ok((s, t, d)) } }) .collect::, Error>>()?; /* Sort the src/dst pairs so that the sources with the longest componenets come first. */ - src_dst.sort_by_key(|(s, _)| Reverse(s.components().count())); - - let dir = tempdir()?; - for s_d in src_dst.iter_mut() { - let (s, d) = s_d; - let mut hasher = DefaultHasher::new(); - s.hash(&mut hasher); - let file = hasher.finish().to_string(); - let dst = dir.path().join(&file); + ps.sort_by_key(|s| Reverse(s.0.components().count())); + for (s, t, _) in ps.iter() { if flags.dryrun { - println!("{} -> {}", s.as_path().display(), dst.as_path().display()); + println!("{} -> {}", s.as_path().display(), t.as_path().display()); } else { - copy_and_remove_file_or_dir(&s, &dst)?; + copy_and_remove_file_or_dir(&s, &t)?; } - - *s_d = (dst, d.to_path_buf()) } - for (s, d) in src_dst.iter().rev() { + for (_, t, d) in ps.iter().rev() { if flags.dryrun { - println!("{} -> {}", s.as_path().display(), d.as_path().display()); + println!("{} -> {}", t.as_path().display(), d.as_path().display()); } else { - copy_and_remove_file_or_dir(&s, &d)?; + copy_and_remove_file_or_dir(&t, &d)?; } } -- cgit v1.2.3