Advertisement
Guest User

Untitled

a guest
May 26th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. use std::*;
  2. use std::{
  3. env, io,
  4. path::{Path, PathBuf},
  5. };
  6.  
  7.  
  8. pub struct Directory {}
  9. impl Directory {
  10. pub fn visit_dirs<T, F>(dir: &T, cb: F) -> io::Result<()>
  11. where
  12. T: AsRef<Path>,
  13. F: Fn(&Path) + Copy,
  14. {
  15. if dir.as_ref().is_dir() {
  16. fs::read_dir(dir)?.try_for_each(|entry| {
  17. let entry = entry?;
  18. let path = entry.path();
  19. if path.is_dir() {
  20. cb(&path);
  21. Self::visit_dirs(&path, cb)?;
  22. } else {
  23. cb(&path);
  24. }
  25. ()
  26. })
  27. }
  28. Ok(())
  29. }
  30.  
  31. pub fn copy_dir<T>(src: T, dst: T)
  32. where
  33. T: AsRef<Path>,
  34. {
  35. fs::remove_dir_all(&dst);
  36. Self::copy_dir_merge(src, dst);
  37. }
  38.  
  39. fn copy_dir_merge<T>(src: T, dst: T)
  40. where
  41. T: AsRef<Path>,
  42. {
  43. let _ = fs::create_dir_all(&dst);
  44. let _ = Self::visit_dirs(&src, |path| {
  45. let dst = dst.as_ref().join(path.strip_prefix(&src).unwrap());
  46. if path.is_dir() {
  47. fs::create_dir_all(dst);
  48. } else {
  49. fs::copy(path, dst);
  50. }
  51. });
  52. }
  53. }
  54.  
  55. fn main() {
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement