From c0b12c72603acadf7a15fdb8da6f4bf553346b6e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 1 Feb 2022 16:38:03 -0600 Subject: [PATCH] refactor: Clarify intent of default_value_if While this doesn't help in the public API (yet), this at least clarifies the intent in the implementation. This is building towards #3020. --- src/build/arg/arg_predicate.rs | 14 ++++++++++++++ src/build/arg/mod.rs | 7 +++++-- src/build/mod.rs | 1 + src/parse/parser.rs | 9 +++++---- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 src/build/arg/arg_predicate.rs diff --git a/src/build/arg/arg_predicate.rs b/src/build/arg/arg_predicate.rs new file mode 100644 index 00000000..58eb5494 --- /dev/null +++ b/src/build/arg/arg_predicate.rs @@ -0,0 +1,14 @@ +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub(crate) enum ArgPredicate<'help> { + IsPresent, + Equals(&'help std::ffi::OsStr), +} + +impl<'help> From> for ArgPredicate<'help> { + fn from(other: Option<&'help std::ffi::OsStr>) -> Self { + match other { + Some(other) => Self::Equals(other), + None => Self::IsPresent, + } + } +} diff --git a/src/build/arg/mod.rs b/src/build/arg/mod.rs index 8d0505a0..a649b2d1 100644 --- a/src/build/arg/mod.rs +++ b/src/build/arg/mod.rs @@ -1,3 +1,4 @@ +mod arg_predicate; #[cfg(debug_assertions)] pub mod debug_asserts; mod possible_value; @@ -9,6 +10,7 @@ mod value_hint; pub use self::possible_value::PossibleValue; pub use self::settings::{ArgFlags, ArgSettings}; pub use self::value_hint::ValueHint; +pub(crate) use arg_predicate::ArgPredicate; // Std use std::{ @@ -92,7 +94,7 @@ pub struct Arg<'help> { pub(crate) validator_os: Option>>>, pub(crate) val_delim: Option, pub(crate) default_vals: Vec<&'help OsStr>, - pub(crate) default_vals_ifs: Vec<(Id, Option<&'help OsStr>, Option<&'help OsStr>)>, + pub(crate) default_vals_ifs: Vec<(Id, ArgPredicate<'help>, Option<&'help OsStr>)>, pub(crate) default_missing_vals: Vec<&'help OsStr>, #[cfg(feature = "env")] pub(crate) env: Option<(&'help OsStr, Option)>, @@ -3486,7 +3488,8 @@ impl<'help> Arg<'help> { val: Option<&'help OsStr>, default: Option<&'help OsStr>, ) -> Self { - self.default_vals_ifs.push((arg_id.into(), val, default)); + self.default_vals_ifs + .push((arg_id.into(), val.into(), default)); self.takes_value(true) } diff --git a/src/build/mod.rs b/src/build/mod.rs index d23694dd..37502810 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -12,3 +12,4 @@ pub use self::{ arg::{Arg, ArgFlags, ArgSettings, PossibleValue, ValueHint}, arg_group::ArgGroup, }; +pub(crate) use arg::ArgPredicate; diff --git a/src/parse/parser.rs b/src/parse/parser.rs index d9d17dbb..8fe36df3 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1325,10 +1325,11 @@ impl<'help, 'app> Parser<'help, 'app> { if matcher.get(&arg.id).is_none() { for (id, val, default) in arg.default_vals_ifs.iter() { let add = if let Some(a) = matcher.get(id) { - if let Some(v) = val { - a.vals_flatten().any(|value| v == value) - } else { - true + match val { + crate::build::ArgPredicate::Equals(v) => { + a.vals_flatten().any(|value| v == value) + } + crate::build::ArgPredicate::IsPresent => true, } } else { false