Options and FileFilter are also deducible

We may as well use this trait now that it’s available!
This commit is contained in:
Ben S 2015-11-15 14:57:32 +00:00
parent 534d3c3fa5
commit 2efaf7ec45

View File

@ -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 options = try!(Options::deduce(&matches));
let view = try!(View::deduce(&matches, filter, dir_action)); Ok((options, path_strs))
Ok((Options {
dir_action: dir_action,
view: view,
filter: filter,
}, 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