-
-
Notifications
You must be signed in to change notification settings - Fork 948
Refactor filters in spawn_senders #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Asha20
wants to merge
11
commits into
sharkdp:master
Choose a base branch
from
Asha20:pr/refactor_filters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
265298b
Add MinDepth filter
c673798
Add RegexMatch filter
5f4b06d
Add Extensions filter
f5f2919
Add FileTypes filter
98eb81b
Add SkipRoot filter
3d2230e
Remove bool::then for min supported Rust version
33ad2c8
Merge remote-tracking branch 'upstream/master' into pr/refactor_filters
Asha20 319f82b
Simplify code that applies file filters
Asha20 71f94cc
Construct file filters only once
Asha20 f37b417
Pass by reference in Extensions file filter
Asha20 4d530d1
Switch file filter dynamic dispatch to enum
Asha20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use crate::walk::DirEntry; | ||
|
|
||
| use super::{Extensions, FileTypes, MinDepth, RegexMatch, SkipRoot}; | ||
|
|
||
| pub trait Filter: Send + Sync { | ||
| /// Whether the entry should be skipped or not. | ||
| fn should_skip(&self, entry: &DirEntry) -> bool; | ||
| } | ||
|
|
||
| pub enum FilterKind<'a> { | ||
| SkipRoot(SkipRoot), | ||
| MinDepth(MinDepth), | ||
| RegexMatch(RegexMatch), | ||
| Extensions(Extensions<'a>), | ||
| FileTypes(FileTypes), | ||
| } | ||
|
|
||
| impl<'a> FilterKind<'a> { | ||
| pub fn should_skip(&self, entry: &DirEntry) -> bool { | ||
| match self { | ||
| FilterKind::SkipRoot(f) => f.should_skip(entry), | ||
| FilterKind::MinDepth(f) => f.should_skip(entry), | ||
| FilterKind::RegexMatch(f) => f.should_skip(entry), | ||
| FilterKind::Extensions(f) => f.should_skip(entry), | ||
| FilterKind::FileTypes(f) => f.should_skip(entry), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use regex::bytes::RegexSet; | ||
|
|
||
| use crate::filesystem; | ||
|
|
||
| use super::common::Filter; | ||
|
|
||
| pub struct Extensions<'a> { | ||
| extensions: Option<&'a RegexSet>, | ||
| } | ||
|
|
||
| impl<'a> Extensions<'a> { | ||
| pub fn new(extensions: Option<&'a RegexSet>) -> Self { | ||
| Self { extensions } | ||
| } | ||
| } | ||
|
|
||
| impl Filter for Extensions<'_> { | ||
| fn should_skip(&self, entry: &crate::walk::DirEntry) -> bool { | ||
| self.extensions | ||
| .as_ref() | ||
| .map(|exts_regex| { | ||
| entry | ||
| .path() | ||
| .file_name() | ||
| .map(|path_str| !exts_regex.is_match(&filesystem::osstr_to_bytes(path_str))) | ||
| .unwrap_or(true) | ||
| }) | ||
| .unwrap_or_default() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| use crate::filesystem; | ||
| use crate::walk; | ||
|
|
||
| use super::Filter; | ||
|
|
||
| /// Whether or not to show | ||
| #[derive(Clone)] | ||
| pub struct FileTypes { | ||
| pub files: bool, | ||
| pub directories: bool, | ||
| pub symlinks: bool, | ||
| pub sockets: bool, | ||
| pub pipes: bool, | ||
| pub executables_only: bool, | ||
| pub empty_only: bool, | ||
| } | ||
|
|
||
| impl Default for FileTypes { | ||
| fn default() -> FileTypes { | ||
| FileTypes { | ||
| files: false, | ||
| directories: false, | ||
| symlinks: false, | ||
| sockets: false, | ||
| pipes: false, | ||
| executables_only: false, | ||
| empty_only: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Filter for FileTypes { | ||
| fn should_skip(&self, entry: &walk::DirEntry) -> bool { | ||
| entry | ||
| .file_type() | ||
| .map(|ref entry_type| { | ||
| (!self.files && entry_type.is_file()) | ||
| || (!self.directories && entry_type.is_dir()) | ||
| || (!self.symlinks && entry_type.is_symlink()) | ||
| || (!self.sockets && filesystem::is_socket(*entry_type)) | ||
| || (!self.pipes && filesystem::is_pipe(*entry_type)) | ||
| || (self.executables_only | ||
| && !entry | ||
| .metadata() | ||
| .map(filesystem::is_executable) | ||
| .unwrap_or(false)) | ||
| || (self.empty_only && !filesystem::is_empty(entry)) | ||
| || !(entry_type.is_file() | ||
| || entry_type.is_dir() | ||
| || entry_type.is_symlink() | ||
| || filesystem::is_socket(*entry_type) | ||
| || filesystem::is_pipe(*entry_type)) | ||
| }) | ||
| .unwrap_or(true) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use crate::walk::DirEntry; | ||
|
|
||
| use super::common::Filter; | ||
|
|
||
| pub struct MinDepth { | ||
| min_depth: Option<usize>, | ||
| } | ||
|
|
||
| impl MinDepth { | ||
| pub fn new(min_depth: Option<usize>) -> Self { | ||
| Self { min_depth } | ||
| } | ||
| } | ||
|
|
||
| impl Filter for MinDepth { | ||
| fn should_skip(&self, entry: &DirEntry) -> bool { | ||
| self.min_depth | ||
| .map(|min_depth| entry.depth().map_or(true, |d| d < min_depth)) | ||
| .unwrap_or_default() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| use std::{borrow::Cow, ffi::OsStr, sync::Arc}; | ||
|
|
||
| use regex::bytes::Regex; | ||
|
|
||
| use crate::filesystem; | ||
|
|
||
| use super::common::Filter; | ||
|
|
||
| pub struct RegexMatch { | ||
| pattern: Arc<Regex>, | ||
| search_full_path: bool, | ||
| } | ||
|
|
||
| impl RegexMatch { | ||
| pub fn new(pattern: Arc<Regex>, search_full_path: bool) -> Self { | ||
| Self { | ||
| pattern, | ||
| search_full_path, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Filter for RegexMatch { | ||
| fn should_skip(&self, entry: &crate::walk::DirEntry) -> bool { | ||
| let entry_path = entry.path(); | ||
|
|
||
| let search_str: Cow<OsStr> = if self.search_full_path { | ||
| let path_abs_buf = filesystem::path_absolute_form(entry_path) | ||
| .expect("Retrieving absolute path succeeds"); | ||
| Cow::Owned(path_abs_buf.as_os_str().to_os_string()) | ||
| } else { | ||
| match entry_path.file_name() { | ||
| Some(filename) => Cow::Borrowed(filename), | ||
| None => unreachable!( | ||
| "Encountered file system entry without a file name. This should only \ | ||
| happen for paths like 'foo/bar/..' or '/' which are not supposed to \ | ||
| appear in a file system traversal." | ||
| ), | ||
| } | ||
| }; | ||
|
|
||
| !self | ||
| .pattern | ||
| .is_match(&filesystem::osstr_to_bytes(search_str.as_ref())) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use super::Filter; | ||
|
|
||
| pub struct SkipRoot; | ||
|
|
||
| impl Filter for SkipRoot { | ||
| fn should_skip(&self, entry: &crate::walk::DirEntry) -> bool { | ||
| entry.depth().map(|depth| depth == 0).unwrap_or(false) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.