diff --git a/lib/src/ignore/files.rs b/lib/src/ignore/files.rs index 2516149..2cb7637 100644 --- a/lib/src/ignore/files.rs +++ b/lib/src/ignore/files.rs @@ -132,8 +132,7 @@ pub async fn from_origin(path: impl AsRef) -> (Vec, Vec ) .await { - dirs.add_last_file_to_filter(&mut files, &mut errors) - .await; + dirs.add_last_file_to_filter(&mut files, &mut errors).await; } if discover_file( @@ -145,8 +144,7 @@ pub async fn from_origin(path: impl AsRef) -> (Vec, Vec ) .await { - dirs.add_last_file_to_filter(&mut files, &mut errors) - .await; + dirs.add_last_file_to_filter(&mut files, &mut errors).await; } if discover_file( @@ -158,8 +156,7 @@ pub async fn from_origin(path: impl AsRef) -> (Vec, Vec ) .await { - dirs.add_last_file_to_filter(&mut files, &mut errors) - .await; + dirs.add_last_file_to_filter(&mut files, &mut errors).await; } } } @@ -348,8 +345,13 @@ impl DirTourist { .await .map_err(|err| Error::new(ErrorKind::Other, err))?; - filter.add_globs(&["/.git", "/.hg", "/.bzr", "/_darcs", "/.fossil-settings"], Some(base.clone())).await - .map_err(|err| Error::new(ErrorKind::Other, err))?; + filter + .add_globs( + &["/.git", "/.hg", "/.bzr", "/_darcs", "/.fossil-settings"], + Some(base.clone()), + ) + .await + .map_err(|err| Error::new(ErrorKind::Other, err))?; Ok(Self { to_visit: vec![base.clone()], @@ -443,15 +445,15 @@ impl DirTourist { pub(crate) async fn add_last_file_to_filter( &mut self, - files: &mut Vec, - errors: &mut Vec, -) { + files: &mut Vec, + errors: &mut Vec, + ) { if let Some(ig) = files.last() { if let Err(err) = self.filter.add_file(ig).await { errors.push(Error::new(ErrorKind::Other, err)); } } -} + } fn must_skip(&self, mut path: &Path) -> bool { if self.to_skip.contains(path) { diff --git a/lib/src/ignore/filter.rs b/lib/src/ignore/filter.rs index 855f407..d654b5a 100644 --- a/lib/src/ignore/filter.rs +++ b/lib/src/ignore/filter.rs @@ -24,7 +24,7 @@ use super::files::IgnoreFile; /// /// It implements [`Filterer`] so it can be used directly in another filterer; it is not designed to /// be used as a standalone filterer. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct IgnoreFilterer { origin: PathBuf, builder: Option, @@ -32,7 +32,22 @@ pub struct IgnoreFilterer { } impl IgnoreFilterer { + /// Create a new empty filterer. + /// + /// Prefer [`new()`](IgnoreFilterer::new()) if you have ignore files ready to use. + pub fn empty(origin: impl AsRef) -> Self { + let origin = origin.as_ref(); + Self { + builder: Some(GitignoreBuilder::new(origin)), + origin: origin.to_owned(), + compiled: Gitignore::empty(), + } + } + /// Read ignore files from disk and load them for filtering. + /// + /// Use [`empty()`](IgnoreFilterer::empty()) if you want an empty filterer, + /// or to construct one outside an async environment. pub async fn new(origin: impl AsRef, files: &[IgnoreFile]) -> Result { let origin = origin.as_ref(); let _span = trace_span!("build_filterer", ?origin); @@ -159,10 +174,7 @@ impl IgnoreFilterer { trace!("recompiling globset"); let recompiled = builder .build() - .map_err(|err| RuntimeError::IgnoreFileGlob { - file, - err, - })?; + .map_err(|err| RuntimeError::IgnoreFileGlob { file, err })?; trace!( new_ignores=%(recompiled.num_ignores() - pre_ignores), @@ -178,7 +190,11 @@ impl IgnoreFilterer { /// Adds some globs manually, if the builder is available. /// /// Does nothing silently otherwise. - pub async fn add_globs(&mut self, globs: &[&str], applies_in: Option) -> Result<(), RuntimeError> { + pub async fn add_globs( + &mut self, + globs: &[&str], + applies_in: Option, + ) -> Result<(), RuntimeError> { if let Some(ref mut builder) = self.builder { let _span = trace_span!("loading ignore globs", ?globs).entered(); for line in globs { @@ -187,12 +203,12 @@ impl IgnoreFilterer { } trace!(?line, "adding ignore line"); - builder - .add_line(applies_in.clone(), line) - .map_err(|err| RuntimeError::IgnoreFileGlob { + builder.add_line(applies_in.clone(), line).map_err(|err| { + RuntimeError::IgnoreFileGlob { file: "manual glob".into(), err, - })?; + } + })?; } self.recompile("manual glob".into())?;