From ceae7e747cefa3e5df7dd6fa4f0d6d0c6836f33c Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 15 Nov 2015 21:04:48 +0000 Subject: 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. --- src/options.rs | 181 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 91 insertions(+), 90 deletions(-) (limited to 'src') 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; } -impl OptionSet for Columns { - fn deduce(matches: &getopts::Matches) -> Result { - 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 { + 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 { + 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, +} + +impl RecurseOptions { + pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result { + 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 { + 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 { - 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 { - 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, -} - -impl RecurseOptions { - pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result { - 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 { -- cgit v1.2.3