Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::*;
- use std::{
- env, io,
- path::{Path, PathBuf},
- };
- pub struct Directory {}
- impl Directory {
- pub fn visit_dirs<T, F>(dir: &T, cb: F) -> io::Result<()>
- where
- T: AsRef<Path>,
- F: Fn(&Path) + Copy,
- {
- if dir.as_ref().is_dir() {
- fs::read_dir(dir)?.try_for_each(|entry| -> io::Result<()> {
- let entry = entry?;
- let path = entry.path();
- if path.is_dir() {
- cb(&path);
- Self::visit_dirs(&path, cb)?;
- } else {
- cb(&path);
- }
- Ok(())
- })?;
- }
- Ok(())
- }
- pub fn copy_dir<T>(src: T, dst: T)
- where
- T: AsRef<Path>,
- {
- fs::remove_dir_all(&dst);
- Self::copy_dir_merge(src, dst);
- }
- fn copy_dir_merge<T>(src: T, dst: T)
- where
- T: AsRef<Path>,
- {
- let _ = fs::create_dir_all(&dst);
- let _ = Self::visit_dirs(&src, |path| {
- let dst = dst.as_ref().join(path.strip_prefix(&src).unwrap());
- if path.is_dir() {
- fs::create_dir_all(dst);
- } else {
- fs::copy(path, dst);
- }
- });
- }
- }
- fn main() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement