From 8b9f074d6353ffe3116a65b0d824972d6005f4d0 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sat, 14 Nov 2015 23:47:13 +0000 Subject: [PATCH] Inline SortField::from_word With the new OptionSet trait, the from_word constructor doesn't really do much by itself. --- src/options.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/options.rs b/src/options.rs index 79e324f..cc8ae9a 100644 --- a/src/options.rs +++ b/src/options.rs @@ -83,10 +83,7 @@ impl Options { return Err(Misfire::Version); } - let sort_field = match matches.opt_str("sort") { - Some(word) => try!(SortField::from_word(word)), - None => SortField::default(), - }; + let sort_field = try!(SortField::deduce(&matches)); let filter = FileFilter { list_dirs_first: matches.opt_present("group-directories-first"), @@ -336,30 +333,27 @@ impl Default for SortField { impl OptionSet for SortField { fn deduce(matches: &getopts::Matches) -> Result { - match matches.opt_str("sort") { - Some(word) => SortField::from_word(word), - None => Ok(SortField::default()), + if let Some(word) = matches.opt_str("sort") { + match &word[..] { + "name" | "filename" => Ok(SortField::Name), + "size" | "filesize" => Ok(SortField::Size), + "ext" | "extension" => Ok(SortField::Extension), + "mod" | "modified" => Ok(SortField::ModifiedDate), + "acc" | "accessed" => Ok(SortField::AccessedDate), + "cr" | "created" => Ok(SortField::CreatedDate), + "none" => Ok(SortField::Unsorted), + "inode" => Ok(SortField::FileInode), + field => Err(SortField::none(field)) + } + } + else { + Ok(SortField::default()) } } } impl SortField { - /// Find which field to use based on a user-supplied word. - fn from_word(word: String) -> Result { - match &word[..] { - "name" | "filename" => Ok(SortField::Name), - "size" | "filesize" => Ok(SortField::Size), - "ext" | "extension" => Ok(SortField::Extension), - "mod" | "modified" => Ok(SortField::ModifiedDate), - "acc" | "accessed" => Ok(SortField::AccessedDate), - "cr" | "created" => Ok(SortField::CreatedDate), - "none" => Ok(SortField::Unsorted), - "inode" => Ok(SortField::FileInode), - field => Err(SortField::none(field)) - } - } - /// How to display an error when the word didn't match with anything. fn none(field: &str) -> Misfire { Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--sort {}", field)))