diff options
author | Ben S <ogham@bsago.me> | 2015-11-15 21:04:48 +0000 |
---|---|---|
committer | Ben S <ogham@bsago.me> | 2015-11-15 21:04:48 +0000 |
commit | ceae7e747cefa3e5df7dd6fa4f0d6d0c6836f33c (patch) | |
tree | 350098f989087a626d63e5c7e94c6e6f14523c78 | |
parent | e07992d08c22898ca723fcb2383c545226d360ce (diff) |
Rearrange trait definitions in options
This puts the impls for the structs defined in the module first, then impls for the structs defined in the columns module second.
-rw-r--r-- | src/options.rs | 181 |
1 files changed, 91 insertions, 90 deletions
diff --git a/src/options.rs b/src/options.rs index b9828ec..27b1af7 100644 --- a/src/options.rs +++ b/src/options.rs @@ -266,20 +266,6 @@ trait OptionSet: Sized { fn deduce(matches: &getopts::Matches) -> Result<Self, Misfire>; } -impl OptionSet for Columns { - fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> { - Ok(Columns { - size_format: try!(SizeFormat::deduce(matches)), - time_types: try!(TimeTypes::deduce(matches)), - inode: matches.opt_present("inode"), - links: matches.opt_present("links"), - blocks: matches.opt_present("blocks"), - group: matches.opt_present("group"), - git: cfg!(feature="git") && matches.opt_present("git"), - }) - } -} - /// The **file filter** processes a vector of files before outputting them, /// filtering and sorting the files depending on the user’s command-line @@ -347,6 +333,82 @@ impl FileFilter { } +/// What to do when encountering a directory? +#[derive(PartialEq, Debug, Copy, Clone)] +pub enum DirAction { + AsFile, + List, + Recurse(RecurseOptions), +} + +impl DirAction { + pub fn deduce(matches: &getopts::Matches) -> Result<DirAction, Misfire> { + let recurse = matches.opt_present("recurse"); + let list = matches.opt_present("list-dirs"); + let tree = matches.opt_present("tree"); + + match (recurse, list, tree) { + (true, true, _ ) => Err(Misfire::Conflict("recurse", "list-dirs")), + (_, true, true ) => Err(Misfire::Conflict("tree", "list-dirs")), + (true, false, false) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, false)))), + (_ , _, true ) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, true)))), + (false, true, _ ) => Ok(DirAction::AsFile), + (false, false, _ ) => Ok(DirAction::List), + } + } + + pub fn recurse_options(&self) -> Option<RecurseOptions> { + match *self { + DirAction::Recurse(opts) => Some(opts), + _ => None, + } + } + + pub fn treat_dirs_as_files(&self) -> bool { + match *self { + DirAction::AsFile => true, + DirAction::Recurse(RecurseOptions { tree, .. }) => tree, + _ => false, + } + } +} + + +#[derive(PartialEq, Debug, Copy, Clone)] +pub struct RecurseOptions { + pub tree: bool, + pub max_depth: Option<usize>, +} + +impl RecurseOptions { + pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result<RecurseOptions, Misfire> { + let max_depth = if let Some(level) = matches.opt_str("level") { + match level.parse() { + Ok(l) => Some(l), + Err(e) => return Err(Misfire::FailedParse(e)), + } + } + else { + None + }; + + Ok(RecurseOptions { + tree: tree, + max_depth: max_depth, + }) + } + + pub fn is_too_deep(&self, depth: usize) -> bool { + match self.max_depth { + None => false, + Some(d) => { + d <= depth + } + } + } +} + + /// User-supplied field to sort by. #[derive(PartialEq, Debug, Copy, Clone)] pub enum SortField { @@ -382,6 +444,21 @@ impl OptionSet for SortField { } +impl OptionSet for Columns { + fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> { + Ok(Columns { + size_format: try!(SizeFormat::deduce(matches)), + time_types: try!(TimeTypes::deduce(matches)), + inode: matches.opt_present("inode"), + links: matches.opt_present("links"), + blocks: matches.opt_present("blocks"), + group: matches.opt_present("group"), + git: cfg!(feature="git") && matches.opt_present("git"), + }) + } +} + + impl OptionSet for SizeFormat { /// Determine which file size to use in the file size column based on @@ -454,82 +531,6 @@ impl OptionSet for TimeTypes { } -/// What to do when encountering a directory? -#[derive(PartialEq, Debug, Copy, Clone)] -pub enum DirAction { - AsFile, - List, - Recurse(RecurseOptions), -} - -impl DirAction { - pub fn deduce(matches: &getopts::Matches) -> Result<DirAction, Misfire> { - let recurse = matches.opt_present("recurse"); - let list = matches.opt_present("list-dirs"); - let tree = matches.opt_present("tree"); - - match (recurse, list, tree) { - (true, true, _ ) => Err(Misfire::Conflict("recurse", "list-dirs")), - (_, true, true ) => Err(Misfire::Conflict("tree", "list-dirs")), - (true, false, false) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, false)))), - (_ , _, true ) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, true)))), - (false, true, _ ) => Ok(DirAction::AsFile), - (false, false, _ ) => Ok(DirAction::List), - } - } - - pub fn recurse_options(&self) -> Option<RecurseOptions> { - match *self { - DirAction::Recurse(opts) => Some(opts), - _ => None, - } - } - - pub fn treat_dirs_as_files(&self) -> bool { - match *self { - DirAction::AsFile => true, - DirAction::Recurse(RecurseOptions { tree, .. }) => tree, - _ => false, - } - } -} - - -#[derive(PartialEq, Debug, Copy, Clone)] -pub struct RecurseOptions { - pub tree: bool, - pub max_depth: Option<usize>, -} - -impl RecurseOptions { - pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result<RecurseOptions, Misfire> { - let max_depth = if let Some(level) = matches.opt_str("level") { - match level.parse() { - Ok(l) => Some(l), - Err(e) => return Err(Misfire::FailedParse(e)), - } - } - else { - None - }; - - Ok(RecurseOptions { - tree: tree, - max_depth: max_depth, - }) - } - - pub fn is_too_deep(&self, depth: usize) -> bool { - match self.max_depth { - None => false, - Some(d) => { - d <= depth - } - } - } -} - - /// One of these things could happen instead of listing files. #[derive(PartialEq, Debug)] pub enum Misfire { |