Skip to content
Myr

Refactor file types check to be on impl of FileTypes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Relates to #382

authored by Thayne McCombs · committed by David Peter · parent c06c995 · Browse files · 3 files+30 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−22 removed

  • other:+30 added−22 removed

3 files+30 added−22 removed

modified:src/filetypes.rs+28 added
1use crate::filesystem;2use crate::walk;34/// Whether or not to show5pub struct FileTypes {6 pub files: bool,7 pub directories: bool,
16 unmodified lines
24 empty_only: false,25 }26 }27}2829impl FileTypes {30 pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool {31 if let Some(ref entry_type) = entry.file_type() {32 (!self.files && entry_type.is_file())33 || (!self.directories && entry_type.is_dir())34 || (!self.symlinks && entry_type.is_symlink())35 || (!self.sockets && filesystem::is_socket(*entry_type))36 || (!self.pipes && filesystem::is_pipe(*entry_type))37 || (self.executables_only38 && !entry39 .metadata()40 .map(|m| filesystem::is_executable(&m))41 .unwrap_or(false))42 || (self.empty_only && !filesystem::is_empty(&entry))43 || !(entry_type.is_file()44 || entry_type.is_dir()45 || entry_type.is_symlink()46 || filesystem::is_socket(*entry_type)47 || filesystem::is_pipe(*entry_type))48 } else {49 true50 }51 }52}
modified:src/main.rs+1 added-1 removed
18 unmodified lines
19use anyhow::{anyhow, Context, Result};20use atty::Stream;21use globset::GlobBuilder;22use lscolors::LsColors;23use regex::bytes::{RegexBuilder, RegexSetBuilder};23use normpath::PathExt;24use regex::bytes::{RegexBuilder, RegexSetBuilder};2526use crate::error::print_error;27use crate::exec::CommandTemplate;28use crate::exit_codes::ExitCode;
463 unmodified lines
modified:src/walk.rs+1 added-21 removed
414 unmodified lines
415 }416417 // Filter out unwanted file types.418 if let Some(ref file_types) = config.file_types {419 if let Some(ref entry_type) = entry.file_type() {420 if (!file_types.files && entry_type.is_file())421 || (!file_types.directories && entry_type.is_dir())422 || (!file_types.symlinks && entry_type.is_symlink())423 || (!file_types.sockets && filesystem::is_socket(*entry_type))424 || (!file_types.pipes && filesystem::is_pipe(*entry_type))425 || (file_types.executables_only426 && !entry427 .metadata()428 .map(|m| filesystem::is_executable(&m))429 .unwrap_or(false))430 || (file_types.empty_only && !filesystem::is_empty(&entry))431 || !(entry_type.is_file()432 || entry_type.is_dir()433 || entry_type.is_symlink()434 || filesystem::is_socket(*entry_type)435 || filesystem::is_pipe(*entry_type))436 {437 return ignore::WalkState::Continue;438 }439 } else {419 if file_types.should_ignore(&entry) {420 return ignore::WalkState::Continue;421 }422 }423
64 unmodified lines