Skip to content

Commit

Permalink
feat(Parser): ✨ reimplemented strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinFillon committed Sep 7, 2023
1 parent f4bb28b commit 66da265
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
33 changes: 16 additions & 17 deletions src/options/dir_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, OptionsError> {
pub fn deduce(matches: &Opts, can_tree: bool, strictness: bool) -> Result<Self, OptionsError> {
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
Expand Down
9 changes: 7 additions & 2 deletions src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
62 changes: 33 additions & 29 deletions src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::options::parser::Opts;


impl View {
pub fn deduce<V: Vars>(matches: &Opts, vars: &V) -> Result<Self, OptionsError> {
let mode = Mode::deduce(matches, vars)?;
pub fn deduce<V: Vars>(matches: &Opts, vars: &V, strictness: bool) -> Result<Self, OptionsError> {
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;
Expand All @@ -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<V: Vars>(matches: &Opts, vars: &V) -> Result<Self, OptionsError> {
// 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<V: Vars>(matches: &Opts, vars: &V, stricness: bool) -> Result<Self, OptionsError> {
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)
Expand All @@ -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;
Expand All @@ -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(())
}
}
Expand Down

0 comments on commit 66da265

Please sign in to comment.