Allow custom filter function to be passed with WalkOptions

This commit is contained in:
Nick Groenen 2020-12-13 23:15:13 +01:00
parent 8a28d627e4
commit 7027290697
No known key found for this signature in database
GPG Key ID: 4F0AD019928AE098
2 changed files with 25 additions and 6 deletions

View File

@ -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| { vault.clone().into_par_iter().try_for_each(|file| {
let relative_path = file let relative_path = file
.strip_prefix(&self.root.clone()) .strip_prefix(&self.root.clone())

View File

@ -1,15 +1,28 @@
use crate::{ExportError, WalkDirError}; use crate::{ExportError, WalkDirError};
use ignore::{Walk, WalkBuilder}; use ignore::{DirEntry, Walk, WalkBuilder};
use snafu::ResultExt; use snafu::ResultExt;
use std::fmt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
type Result<T, E = ExportError> = std::result::Result<T, E>; 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 struct WalkOptions<'a> {
pub ignore_filename: &'a str, pub ignore_filename: &'a str,
pub ignore_hidden: bool, pub ignore_hidden: bool,
pub honor_gitignore: 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> { impl<'a> WalkOptions<'a> {
@ -18,11 +31,13 @@ impl<'a> WalkOptions<'a> {
ignore_filename: ".export-ignore", ignore_filename: ".export-ignore",
ignore_hidden: true, ignore_hidden: true,
honor_gitignore: true, honor_gitignore: true,
filter_fn: None,
} }
} }
fn build_walker(self, path: &Path) -> Walk { fn build_walker(self, path: &Path) -> Walk {
WalkBuilder::new(path) let mut walker = WalkBuilder::new(path);
walker
.standard_filters(false) .standard_filters(false)
.parents(true) .parents(true)
.hidden(self.ignore_hidden) .hidden(self.ignore_hidden)
@ -30,8 +45,12 @@ impl<'a> WalkOptions<'a> {
.require_git(true) .require_git(true)
.git_ignore(self.honor_gitignore) .git_ignore(self.honor_gitignore)
.git_global(self.honor_gitignore) .git_global(self.honor_gitignore)
.git_exclude(self.honor_gitignore) .git_exclude(self.honor_gitignore);
.build()
if let Some(filter) = self.filter_fn {
walker.filter_entry(filter);
}
walker.build()
} }
} }