Skip to content

Commit

Permalink
Allow custom filter function to be passed with WalkOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zoni committed Dec 13, 2020
1 parent 8a28d62 commit 7027290
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a> Exporter<'a> {
});
}

let vault = vault_contents(self.root.as_path(), self.walk_options)?;
let vault = vault_contents(self.root.as_path(), self.walk_options.clone())?;
vault.clone().into_par_iter().try_for_each(|file| {
let relative_path = file
.strip_prefix(&self.root.clone())
Expand Down
29 changes: 24 additions & 5 deletions src/walker.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
use crate::{ExportError, WalkDirError};
use ignore::{Walk, WalkBuilder};
use ignore::{DirEntry, Walk, WalkBuilder};
use snafu::ResultExt;
use std::fmt;
use std::path::{Path, PathBuf};

type Result<T, E = ExportError> = std::result::Result<T, E>;
type FilterFn = dyn Fn(&DirEntry) -> bool + Send + Sync + 'static;

#[derive(Debug, Clone, Copy)]
#[derive(Clone)]
pub struct WalkOptions<'a> {
pub ignore_filename: &'a str,
pub ignore_hidden: bool,
pub honor_gitignore: bool,
pub filter_fn: Option<Box<&'static FilterFn>>,
}

impl<'a> fmt::Debug for WalkOptions<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("WalkOptions")
.field("ignore_filename", &self.ignore_filename)
.field("ignore_hidden", &self.ignore_hidden)
.field("honor_gitignore", &self.honor_gitignore)
.finish()
}
}

impl<'a> WalkOptions<'a> {
Expand All @@ -18,20 +31,26 @@ impl<'a> WalkOptions<'a> {
ignore_filename: ".export-ignore",
ignore_hidden: true,
honor_gitignore: true,
filter_fn: None,
}
}

fn build_walker(self, path: &Path) -> Walk {
WalkBuilder::new(path)
let mut walker = WalkBuilder::new(path);
walker
.standard_filters(false)
.parents(true)
.hidden(self.ignore_hidden)
.add_custom_ignore_filename(self.ignore_filename)
.require_git(true)
.git_ignore(self.honor_gitignore)
.git_global(self.honor_gitignore)
.git_exclude(self.honor_gitignore)
.build()
.git_exclude(self.honor_gitignore);

if let Some(filter) = self.filter_fn {
walker.filter_entry(filter);
}
walker.build()
}
}

Expand Down

0 comments on commit 7027290

Please sign in to comment.