From 115ae93df944d6c08f26e3c20e06e36e0aaba396 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Tue, 10 Aug 2021 01:46:12 -0600 Subject: [PATCH] Refactor file types check to be on impl of FileTypes Relates to #382 --- src/filetypes.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 2 +- src/walk.rs | 22 +--------------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/filetypes.rs b/src/filetypes.rs index 1f445da..2baf0f2 100644 --- a/src/filetypes.rs +++ b/src/filetypes.rs @@ -1,3 +1,6 @@ +use crate::filesystem; +use crate::walk; + /// Whether or not to show pub struct FileTypes { pub files: bool, @@ -22,3 +25,28 @@ impl Default for FileTypes { } } } + +impl FileTypes { + pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool { + if let Some(ref entry_type) = entry.file_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(|m| filesystem::is_executable(&m)) + .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)) + } else { + true + } + } +} diff --git a/src/main.rs b/src/main.rs index 8654688..99470da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,8 @@ use anyhow::{anyhow, Context, Result}; use atty::Stream; use globset::GlobBuilder; use lscolors::LsColors; -use regex::bytes::{RegexBuilder, RegexSetBuilder}; use normpath::PathExt; +use regex::bytes::{RegexBuilder, RegexSetBuilder}; use crate::error::print_error; use crate::exec::CommandTemplate; diff --git a/src/walk.rs b/src/walk.rs index a073873..e2966b3 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -416,27 +416,7 @@ fn spawn_senders( // Filter out unwanted file types. if let Some(ref file_types) = config.file_types { - if let Some(ref entry_type) = entry.file_type() { - if (!file_types.files && entry_type.is_file()) - || (!file_types.directories && entry_type.is_dir()) - || (!file_types.symlinks && entry_type.is_symlink()) - || (!file_types.sockets && filesystem::is_socket(*entry_type)) - || (!file_types.pipes && filesystem::is_pipe(*entry_type)) - || (file_types.executables_only - && !entry - .metadata() - .map(|m| filesystem::is_executable(&m)) - .unwrap_or(false)) - || (file_types.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)) - { - return ignore::WalkState::Continue; - } - } else { + if file_types.should_ignore(&entry) { return ignore::WalkState::Continue; } }