From 2efaf7ec453c817fc3fa5b6dd868f0dac6bcaa40 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 15 Nov 2015 14:57:32 +0000 Subject: [PATCH] Options and FileFilter are also deducible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We may as well use this trait now that it’s available! --- src/options.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/options.rs b/src/options.rs index e16d698..7563485 100644 --- a/src/options.rs +++ b/src/options.rs @@ -83,15 +83,6 @@ impl Options { 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() { vec![ ".".to_string() ] } @@ -99,14 +90,8 @@ impl Options { 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 @@ -121,6 +106,20 @@ impl Options { } } +impl OptionSet for Options { + fn deduce(matches: &getopts::Matches) -> Result { + 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)] pub enum View { @@ -276,6 +275,19 @@ pub struct FileFilter { sort_field: SortField, } +impl OptionSet for FileFilter { + fn deduce(matches: &getopts::Matches) -> Result { + 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 { /// Remove every file in the given vector that does *not* pass the