summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/options.rs181
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 {