Просмотр исходного кода

fix(clippy): more than 3 bools in a struct

Hulxv 2 лет назад
Родитель
Сommit
6ab13b99fd
2 измененных файлов с 38 добавлено и 16 удалено
  1. 23 12
      src/fs/filter.rs
  2. 15 4
      src/options/filter.rs

+ 23 - 12
src/fs/filter.rs

@@ -9,6 +9,23 @@ use crate::fs::DotFilter;
 use crate::fs::File;
 
 
+
+/// Flags used to manage the **file filter** process
+#[derive(PartialEq, Eq, Debug, Clone)]
+pub enum FileFilterFlags {
+    /// Whether to reverse the sorting order. This would sort the largest
+    /// files first, or files starting with Z, or the most-recently-changed
+    /// ones, depending on the sort field.
+    Reverse,
+
+    /// Whether to only show directories.
+    OnlyDirs,
+
+    /// Whether to only show files.
+    OnlyFiles
+}
+
+
 /// The **file filter** processes a list of files before displaying them to
 /// the user, by removing files they don’t want to see, and putting the list
 /// in the desired order.
@@ -33,16 +50,8 @@ pub struct FileFilter {
     /// The metadata field to sort by.
     pub sort_field: SortField,
 
-    /// Whether to reverse the sorting order. This would sort the largest
-    /// files first, or files starting with Z, or the most-recently-changed
-    /// ones, depending on the sort field.
-    pub reverse: bool,
-
-    /// Whether to only show directories.
-    pub only_dirs: bool,
-
-    /// Whether to only show files.
-    pub only_files: bool,
+    // Flags that the file filtering process follow
+    pub flags: Vec<FileFilterFlags>,
 
     /// Which invisible “dot” files to include when listing a directory.
     ///
@@ -69,9 +78,11 @@ impl FileFilter {
     /// Remove every file in the given vector that does *not* pass the
     /// filter predicate for files found inside a directory.
     pub fn filter_child_files(&self, files: &mut Vec<File<'_>>) {
+        use FileFilterFlags::{OnlyDirs, OnlyFiles};
+        
         files.retain(|f| ! self.ignore_patterns.is_ignored(&f.name));
 
-        match (self.only_dirs, self.only_files) {
+        match (self.flags.contains(&OnlyDirs), self.flags.contains(&OnlyFiles)) {
             (true, false) => {
                 // On pass -'-only-dirs' flag only
                 files.retain(File::is_directory);
@@ -108,7 +119,7 @@ impl FileFilter {
             self.sort_field.compare_files(a.as_ref(), b.as_ref())
         });
 
-        if self.reverse {
+        if self.flags.contains(&FileFilterFlags::Reverse) {
             files.reverse();
         }
 

+ 15 - 4
src/options/filter.rs

@@ -1,7 +1,7 @@
 //! Parsing the options for `FileFilter`.
 
 use crate::fs::DotFilter;
-use crate::fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns, GitIgnore};
+use crate::fs::filter::{FileFilter,FileFilterFlags, SortField, SortCase, IgnorePatterns, GitIgnore};
 
 use crate::options::{flags, OptionsError};
 use crate::options::parser::MatchedFlags;
@@ -11,11 +11,22 @@ impl FileFilter {
 
     /// Determines which of all the file filter options to use.
     pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
+        use FileFilterFlags as FFF;
+        let mut filter_flags:Vec<FileFilterFlags> = vec![];
+            
+        for (has,flag) in &[
+            (matches.has(&flags::REVERSE)?, FFF::Reverse),
+            (matches.has(&flags::ONLY_DIRS)?, FFF::OnlyDirs),
+            (matches.has(&flags::ONLY_FILES)?, FFF::OnlyFiles),
+        ] {
+            if *has { 
+                filter_flags.push(flag.clone());
+            }
+        }
+
         Ok(Self {
             list_dirs_first:  matches.has(&flags::DIRS_FIRST)?,
-            reverse:          matches.has(&flags::REVERSE)?,
-            only_dirs:        matches.has(&flags::ONLY_DIRS)?,
-            only_files:        matches.has(&flags::ONLY_FILES)?,
+            flags: filter_flags,
             sort_field:       SortField::deduce(matches)?,
             dot_filter:       DotFilter::deduce(matches)?,
             ignore_patterns:  IgnorePatterns::deduce(matches)?,