From d7f27e8732da5b475e7f83f70c6160098f588823 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 27 Dec 2021 14:50:47 -0600 Subject: [PATCH] refactor: Harden against occur/val ordering This makes some naming more explicit and allows us to increment occurrences before adding values (which is the logical mental order and allows other improvements). --- src/parse/arg_matcher.rs | 7 +++++-- src/parse/matches/matched_arg.rs | 4 ++++ src/parse/parser.rs | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/parse/arg_matcher.rs b/src/parse/arg_matcher.rs index 1d2ce6ca..58a94059 100644 --- a/src/parse/arg_matcher.rs +++ b/src/parse/arg_matcher.rs @@ -176,8 +176,11 @@ impl ArgMatcher { ma.push_index(idx); } - pub(crate) fn arg_have_val(&mut self, arg: &Id) -> bool { - matches!(self.entry(arg), Entry::Occupied(_)) + pub(crate) fn has_val_groups(&mut self, arg: &Id) -> bool { + match self.entry(arg) { + Entry::Occupied(e) => e.get().has_val_groups(), + Entry::Vacant(_) => false, + } } pub(crate) fn needs_more_vals(&self, o: &Arg) -> bool { diff --git a/src/parse/matches/matched_arg.rs b/src/parse/matches/matched_arg.rs index c2d86f44..90752fb4 100644 --- a/src/parse/matches/matched_arg.rs +++ b/src/parse/matches/matched_arg.rs @@ -81,6 +81,10 @@ impl MatchedArg { self.vals.iter().flatten().count() == 0 } + pub(crate) fn has_val_groups(&self) -> bool { + !self.vals.is_empty() + } + // Will be used later #[allow(dead_code)] pub(crate) fn remove_vals(&mut self, len: usize) { diff --git a/src/parse/parser.rs b/src/parse/parser.rs index dd4953c6..14528f2e 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -594,7 +594,7 @@ impl<'help, 'app> Parser<'help, 'app> { // Creating new value group rather than appending when the arg // doesn't have any value. This behaviour is right because // positional arguments are always present continuously. - let append = self.arg_have_val(matcher, p); + let append = self.has_val_groups(matcher, p); self.add_val_to_arg( p, &arg_os, @@ -1463,8 +1463,8 @@ impl<'help, 'app> Parser<'help, 'app> { matcher.add_index_to(&arg.id, self.cur_idx.get(), ty); } - fn arg_have_val(&self, matcher: &mut ArgMatcher, arg: &Arg<'help>) -> bool { - matcher.arg_have_val(&arg.id) + fn has_val_groups(&self, matcher: &mut ArgMatcher, arg: &Arg<'help>) -> bool { + matcher.has_val_groups(&arg.id) } fn parse_flag(&self, flag: &Arg<'help>, matcher: &mut ArgMatcher) -> ParseResult {