From 66da2652e00c3e6623fd9fb89a1308778332e44b Mon Sep 17 00:00:00 2001 From: MartinFillon Date: Fri, 8 Sep 2023 01:26:51 +0200 Subject: [PATCH] feat(Parser): :sparkles: reimplemented strictness --- src/options/dir_action.rs | 33 ++++++++++----------- src/options/mod.rs | 9 ++++-- src/options/view.rs | 62 +++++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/src/options/dir_action.rs b/src/options/dir_action.rs index 1fc605a17..f4325d876 100644 --- a/src/options/dir_action.rs +++ b/src/options/dir_action.rs @@ -12,24 +12,23 @@ impl DirAction { /// There are three possible actions, and they overlap somewhat: the /// `--tree` flag is another form of recursion, so those two are allowed /// to both be present, but the `--list-dirs` flag is used separately. - pub fn deduce(matches: &Opts, can_tree: bool) -> Result { + pub fn deduce(matches: &Opts, can_tree: bool, strictness: bool) -> Result { let recurse = matches.recurse; - let as_file = matches.recurse; - let tree = matches.recurse; - - // TODO reimplement strictness - // if matches.is_strict() { - // // Early check for --level when it wouldn’t do anything - // if ! recurse && ! tree && matches.count(&flags::LEVEL) > 0 { - // return Err(OptionsError::Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE)); - // } - // else if recurse && as_file { - // return Err(OptionsError::Conflict(&flags::RECURSE, &flags::LIST_DIRS)); - // } - // else if tree && as_file { - // return Err(OptionsError::Conflict(&flags::TREE, &flags::LIST_DIRS)); - // } - // } + let as_file = matches.list_dirs; + let tree = matches.tree > 0; + + if strictness { + // Early check for --level when it wouldn’t do anything + if ! recurse && ! tree && matches.level.is_some() { + return Err(OptionsError::Useless2("LEVEL".to_string(), "RECURSE".to_string(), "TREE".to_string())); + } + else if recurse && as_file { + return Err(OptionsError::Conflict("RECURSE".to_string(), "LIST_DIRS".to_string())); + } + else if tree && as_file { + return Err(OptionsError::Conflict("TREE".to_string(), "LIST_DIRS".to_string())); + } + } if tree && can_tree { // Tree is only appropriate in details mode, so this has to diff --git a/src/options/mod.rs b/src/options/mod.rs index 92ffe3892..15bcb4f48 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -139,8 +139,13 @@ impl Options { ))); } - let view = View::deduce(matches, vars)?; - let dir_action = DirAction::deduce(matches, matches!(view.mode, Mode::Details(_)))?; + let strictness = match vars.get(&vars::EXA_STRICT) { + None => false, + Some(s) => ! s.is_empty() + }; + + let view = View::deduce(matches, vars, strictness)?; + let dir_action = DirAction::deduce(matches, matches!(view.mode, Mode::Details(_)), strictness)?; let filter = FileFilter::deduce(matches)?; let theme = ThemeOptions::deduce(matches, vars)?; diff --git a/src/options/view.rs b/src/options/view.rs index bf3f34489..d73601d14 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -9,8 +9,8 @@ use crate::options::parser::Opts; impl View { - pub fn deduce(matches: &Opts, vars: &V) -> Result { - let mode = Mode::deduce(matches, vars)?; + pub fn deduce(matches: &Opts, vars: &V, strictness: bool) -> Result { + let mode = Mode::deduce(matches, vars, strictness)?; let width = TerminalWidth::deduce(matches, vars)?; let file_style = FileStyle::deduce(matches, vars)?; let deref_links = matches.dereference; @@ -29,17 +29,14 @@ impl Mode { /// /// This is complicated a little by the fact that `--grid` and `--tree` /// can also combine with `--long`, so care has to be taken to use the - pub fn deduce(matches: &Opts, vars: &V) -> Result { - // let flag = matches.has_where_any(|f| f.matches(&flags::LONG) || f.matches(&flags::ONE_LINE) - // || f.matches(&flags::GRID) || f.matches(&flags::TREE)); - + pub fn deduce(matches: &Opts, vars: &V, stricness: bool) -> Result { let _flag = matches.long || matches.oneline || matches.grid || (matches.tree > 0); - // if ! flag { - // Self::strict_check_long_flags(matches)?; - // let grid = grid::Options::deduce(matches)?; - // return Ok(Self::Grid(grid)); - // }; + if ! _flag { + Self::strict_check_long_flags(matches, stricness)?; + let grid = grid::Options::deduce(matches)?; + return Ok(Self::Grid(grid)); + }; if (matches.long && !matches.grid && (matches.tree == 0)) || (matches.tree > 0 && matches.long) @@ -62,7 +59,7 @@ impl Mode { return Ok(Self::Details(details)); } - Self::strict_check_long_flags(matches)?; + Self::strict_check_long_flags(matches, stricness)?; if (matches.tree > 0) && !matches.oneline { let _ = matches.tree > 0; @@ -79,26 +76,33 @@ impl Mode { Ok(Self::Grid(grid)) } - fn strict_check_long_flags(_matches: &Opts) -> Result<(), OptionsError> { + fn strict_check_long_flags(matches: &Opts, stricness: bool) -> Result<(), OptionsError> { // If --long hasn’t been passed, then check if we need to warn the // user about flags that won’t have any effect. // TODO strict handling - // if matches.is_strict() { - // for option in &[ &flags::BINARY, &flags::BYTES, &flags::INODE, &flags::LINKS, - // &flags::HEADER, &flags::BLOCKSIZE, &flags::TIME, &flags::GROUP, &flags::NUMERIC ] { - // if matches.has(option)? { - // return Err(OptionsError::Useless(option, false, &flags::LONG)); - // } - // } - - // if matches.has(&flags::GIT)? && !matches.has(&flags::NO_GIT)? { - // return Err(OptionsError::Useless(&flags::GIT, false, &flags::LONG)); - // } - // else if matches.has(&flags::LEVEL)? && ! matches.has(&flags::RECURSE)? && ! matches.has(&flags::TREE)? { - // return Err(OptionsError::Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE)); - // } - // } - + if stricness && !matches.long{ + if matches.tree > 0 { + return Err(OptionsError::Useless("TREE".to_string(), false, "LONG".to_string())); + } else if matches.binary { + return Err(OptionsError::Useless("BINARY".to_string(), false, "LONG".to_string())); + } else if matches.bytes { + return Err(OptionsError::Useless("BYTES".to_string(), false, "LONG".to_string())); + } else if matches.inode { + return Err(OptionsError::Useless("INODE".to_string(), false, "LONG".to_string())); + } else if matches.links { + return Err(OptionsError::Useless("LINKS".to_string(), false, "LONG".to_string())); + } else if matches.header { + return Err(OptionsError::Useless("HEADER".to_string(), false, "LONG".to_string())); + } else if matches.blocksize { + return Err(OptionsError::Useless("BLOCKSIZE".to_string(), false, "LONG".to_string())); + } else if matches.time.is_some() { + return Err(OptionsError::Useless("TIME".to_string(), false, "LONG".to_string())); + } else if matches.group { + return Err(OptionsError::Useless("GROUP".to_string(), false, "LONG".to_string())); + } else if matches.numeric { + return Err(OptionsError::Useless("NUMERIC".to_string(), false, "LONG".to_string())); + } + } Ok(()) } }