Skip to content

Commit

Permalink
feat: make copy_dir() return copied pathes (#199)
Browse files Browse the repository at this point in the history
This patch makes the `copy_dir()` function return the pathes it copied
(targets, not sources).

This is needed so the fn is more useful when using it to rewrite copy
functionality later.
  • Loading branch information
matthiasbeyer authored Oct 9, 2023
1 parent d287b5c commit 826de84
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs,
path::{Path, StripPrefixError},
path::{Path, PathBuf, StripPrefixError},
process::Command,
};

Expand Down Expand Up @@ -135,13 +135,18 @@ fn extract(
/// to filter the files and directories to copy.
///
/// The copy process also ignores hidden files and directories by default.
///
/// # Return
///
/// The returned `Vec<PathBuf>` contains the pathes of the copied files.
/// If a directory is created in this function, the path to the directory is _not_ returned.
fn copy_dir(
from: &Path,
to: &Path,
include_globs: &[&str],
exclude_globs: &[&str],
use_gitignore: bool,
) -> Result<(), SourceError> {
) -> Result<Vec<PathBuf>, SourceError> {
// Create the to path because we're going to copy the contents only
create_all(to, true).unwrap();

Expand Down Expand Up @@ -175,14 +180,16 @@ fn copy_dir(
&& !exclude_globs.is_match(entry.path())
})
.build()
.try_for_each(|entry| {
.map(|entry| {
let entry = entry?;
let path = entry.path();
let stripped_path = path.strip_prefix(from)?;
let dest_path = to.join(stripped_path);

if path.is_dir() {
create_all(&dest_path, true).map_err(SourceError::FileSystemError)
create_all(&dest_path, true)
.map(|_| None) // We do not return pathes to directories that are created
.map_err(SourceError::FileSystemError)
} else {
let file_options = fs_extra::file::CopyOptions {
overwrite: options.overwrite,
Expand All @@ -197,9 +204,11 @@ fn copy_dir(
path.to_string_lossy(),
dest_path.to_string_lossy()
);
Ok(())
Ok(Some(dest_path))
}
})
.filter_map(|res| res.transpose())
.collect()
}

#[cfg(test)]
Expand Down

0 comments on commit 826de84

Please sign in to comment.