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

Options and FileFilter are also deducible

We may as well use this trait now that it’s available!
Ben S 10 лет назад
Родитель
Сommit
2efaf7ec45
1 измененных файлов с 29 добавлено и 17 удалено
  1. 29 17
      src/options.rs

+ 29 - 17
src/options.rs

@@ -83,15 +83,6 @@ impl Options {
             return Err(Misfire::Version);
             return Err(Misfire::Version);
         }
         }
 
 
-        let sort_field = try!(SortField::deduce(&matches));
-
-        let filter = FileFilter {
-            list_dirs_first: matches.opt_present("group-directories-first"),
-            reverse:         matches.opt_present("reverse"),
-            show_invisibles: matches.opt_present("all"),
-            sort_field:      sort_field,
-        };
-
         let path_strs = if matches.free.is_empty() {
         let path_strs = if matches.free.is_empty() {
             vec![ ".".to_string() ]
             vec![ ".".to_string() ]
         }
         }
@@ -99,14 +90,8 @@ impl Options {
             matches.free.clone()
             matches.free.clone()
         };
         };
 
 
-        let dir_action = try!(DirAction::deduce(&matches));
-        let view = try!(View::deduce(&matches, filter, dir_action));
-
-        Ok((Options {
-            dir_action: dir_action,
-            view:       view,
-            filter:     filter,
-        }, path_strs))
+        let options = try!(Options::deduce(&matches));
+        Ok((options, path_strs))
     }
     }
 
 
     /// Whether the View specified in this set of options includes a Git
     /// Whether the View specified in this set of options includes a Git
@@ -121,6 +106,20 @@ impl Options {
     }
     }
 }
 }
 
 
+impl OptionSet for Options {
+    fn deduce(matches: &getopts::Matches) -> Result<Options, Misfire> {
+        let dir_action = try!(DirAction::deduce(&matches));
+        let filter = try!(FileFilter::deduce(&matches));
+        let view = try!(View::deduce(&matches, filter, dir_action));
+
+        Ok(Options {
+            dir_action: dir_action,
+            view:       view,
+            filter:     filter,
+        })
+    }
+}
+
 
 
 #[derive(PartialEq, Debug, Copy, Clone)]
 #[derive(PartialEq, Debug, Copy, Clone)]
 pub enum View {
 pub enum View {
@@ -276,6 +275,19 @@ pub struct FileFilter {
     sort_field: SortField,
     sort_field: SortField,
 }
 }
 
 
+impl OptionSet for FileFilter {
+    fn deduce(matches: &getopts::Matches) -> Result<FileFilter, Misfire> {
+        let sort_field = try!(SortField::deduce(&matches));
+
+        Ok(FileFilter {
+            list_dirs_first: matches.opt_present("group-directories-first"),
+            reverse:         matches.opt_present("reverse"),
+            show_invisibles: matches.opt_present("all"),
+            sort_field:      sort_field,
+        })
+    }
+}
+
 impl FileFilter {
 impl FileFilter {
 
 
     /// Remove every file in the given vector that does *not* pass the
     /// Remove every file in the given vector that does *not* pass the