Skip to content

Commit

Permalink
Merge pull request #4991 from epage/bit
Browse files Browse the repository at this point in the history
refactor(builder): Remove bitflags dependency
  • Loading branch information
epage authored Jun 30, 2023
2 parents 3f09458 + 27431a4 commit 1938cb2
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 335 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion clap_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ bench = false

[dependencies]
clap_lex = { path = "../clap_lex", version = "0.5.0" }
bitflags = "2.3.3"
unicase = { version = "2.6.0", optional = true }
strsim = { version = "0.10.0", optional = true }
anstream = { version = "0.3.0", optional = true }
Expand Down
148 changes: 29 additions & 119 deletions clap_builder/src/builder/app_settings.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
// Std
use std::ops::BitOr;

#[allow(unused)]
use crate::Arg;
#[allow(unused)]
use crate::Command;

// Third party
use bitflags::bitflags;
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct AppFlags(u32);

impl AppFlags {
pub(crate) fn set(&mut self, setting: AppSettings) {
self.0 |= setting.bit();
}

pub(crate) fn unset(&mut self, setting: AppSettings) {
self.0 &= !setting.bit();
}

pub(crate) fn is_set(&self, setting: AppSettings) -> bool {
self.0 & setting.bit() != 0
}

pub(crate) fn insert(&mut self, other: Self) {
self.0 |= other.0;
}
}

#[doc(hidden)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) struct AppFlags(Flags);
impl std::ops::BitOr for AppFlags {
type Output = Self;

impl Default for AppFlags {
fn default() -> Self {
AppFlags(Flags::COLOR_AUTO)
fn bitor(mut self, rhs: Self) -> Self::Output {
self.insert(rhs);
self
}
}

Expand All @@ -26,7 +40,7 @@ impl Default for AppFlags {
///
/// [`Command`]: crate::Command
#[derive(Debug, PartialEq, Copy, Clone)]
#[non_exhaustive]
#[repr(u8)]
pub(crate) enum AppSettings {
IgnoreErrors,
AllowHyphenValues,
Expand Down Expand Up @@ -62,112 +76,8 @@ pub(crate) enum AppSettings {
BinNameBuilt,
}

bitflags! {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct Flags: u64 {
const SC_NEGATE_REQS = 1;
const SC_REQUIRED = 1 << 1;
const ARG_REQUIRED_ELSE_HELP = 1 << 2;
const PROPAGATE_VERSION = 1 << 3;
const DISABLE_VERSION_FOR_SC = 1 << 4;
const WAIT_ON_ERROR = 1 << 6;
const DISABLE_VERSION_FLAG = 1 << 10;
const HIDDEN = 1 << 11;
const TRAILING_VARARG = 1 << 12;
const NO_BIN_NAME = 1 << 13;
const ALLOW_UNK_SC = 1 << 14;
const LEADING_HYPHEN = 1 << 16;
const NO_POS_VALUES = 1 << 17;
const NEXT_LINE_HELP = 1 << 18;
const DISABLE_COLORED_HELP = 1 << 20;
const COLOR_ALWAYS = 1 << 21;
const COLOR_AUTO = 1 << 22;
const COLOR_NEVER = 1 << 23;
const DONT_DELIM_TRAIL = 1 << 24;
const ALLOW_NEG_NUMS = 1 << 25;
const DISABLE_HELP_SC = 1 << 27;
const ARGS_NEGATE_SCS = 1 << 29;
const PROPAGATE_VALS_DOWN = 1 << 30;
const ALLOW_MISSING_POS = 1 << 31;
const TRAILING_VALUES = 1 << 32;
const BUILT = 1 << 33;
const BIN_NAME_BUILT = 1 << 34;
const VALID_ARG_FOUND = 1 << 35;
const INFER_SUBCOMMANDS = 1 << 36;
const CONTAINS_LAST = 1 << 37;
const ARGS_OVERRIDE_SELF = 1 << 38;
const HELP_REQUIRED = 1 << 39;
const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40;
const DISABLE_HELP_FLAG = 1 << 41;
const INFER_LONG_ARGS = 1 << 43;
const IGNORE_ERRORS = 1 << 44;
const MULTICALL = 1 << 45;
const EXPAND_HELP_SUBCOMMAND_TREES = 1 << 46;
const NO_OP = 0;
impl AppSettings {
fn bit(self) -> u32 {
1 << (self as u8)
}
}

impl_settings! { AppSettings, AppFlags,
ArgRequiredElseHelp
=> Flags::ARG_REQUIRED_ELSE_HELP,
SubcommandPrecedenceOverArg
=> Flags::SUBCOMMAND_PRECEDENCE_OVER_ARG,
ArgsNegateSubcommands
=> Flags::ARGS_NEGATE_SCS,
AllowExternalSubcommands
=> Flags::ALLOW_UNK_SC,
AllowHyphenValues
=> Flags::LEADING_HYPHEN,
AllowNegativeNumbers
=> Flags::ALLOW_NEG_NUMS,
AllowMissingPositional
=> Flags::ALLOW_MISSING_POS,
ColorAlways
=> Flags::COLOR_ALWAYS,
ColorAuto
=> Flags::COLOR_AUTO,
ColorNever
=> Flags::COLOR_NEVER,
DontDelimitTrailingValues
=> Flags::DONT_DELIM_TRAIL,
DisableColoredHelp
=> Flags::DISABLE_COLORED_HELP,
DisableHelpSubcommand
=> Flags::DISABLE_HELP_SC,
DisableHelpFlag
=> Flags::DISABLE_HELP_FLAG,
DisableVersionFlag
=> Flags::DISABLE_VERSION_FLAG,
PropagateVersion
=> Flags::PROPAGATE_VERSION,
HidePossibleValues
=> Flags::NO_POS_VALUES,
HelpExpected
=> Flags::HELP_REQUIRED,
Hidden
=> Flags::HIDDEN,
Multicall
=> Flags::MULTICALL,
NoBinaryName
=> Flags::NO_BIN_NAME,
SubcommandsNegateReqs
=> Flags::SC_NEGATE_REQS,
SubcommandRequired
=> Flags::SC_REQUIRED,
TrailingVarArg
=> Flags::TRAILING_VARARG,
NextLineHelp
=> Flags::NEXT_LINE_HELP,
IgnoreErrors
=> Flags::IGNORE_ERRORS,
Built
=> Flags::BUILT,
BinNameBuilt
=> Flags::BIN_NAME_BUILT,
InferSubcommands
=> Flags::INFER_SUBCOMMANDS,
AllArgsOverrideSelf
=> Flags::ARGS_OVERRIDE_SELF,
InferLongArgs
=> Flags::INFER_LONG_ARGS
}
14 changes: 4 additions & 10 deletions clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,21 +858,15 @@ impl Arg {

#[inline]
#[must_use]
pub(crate) fn setting<F>(mut self, setting: F) -> Self
where
F: Into<ArgFlags>,
{
self.settings.insert(setting.into());
pub(crate) fn setting(mut self, setting: ArgSettings) -> Self {
self.settings.set(setting);
self
}

#[inline]
#[must_use]
pub(crate) fn unset_setting<F>(mut self, setting: F) -> Self
where
F: Into<ArgFlags>,
{
self.settings.remove(setting.into());
pub(crate) fn unset_setting(mut self, setting: ArgSettings) -> Self {
self.settings.unset(setting);
self
}
}
Expand Down
115 changes: 30 additions & 85 deletions clap_builder/src/builder/arg_settings.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// Std
use std::ops::BitOr;

// Third party
use bitflags::bitflags;

#[allow(unused)]
use crate::Arg;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ArgFlags(Flags);
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct ArgFlags(u32);

impl ArgFlags {
pub(crate) fn set(&mut self, setting: ArgSettings) {
self.0 |= setting.bit();
}

pub(crate) fn unset(&mut self, setting: ArgSettings) {
self.0 &= !setting.bit();
}

pub(crate) fn is_set(&self, setting: ArgSettings) -> bool {
self.0 & setting.bit() != 0
}

impl Default for ArgFlags {
fn default() -> Self {
Self::empty()
pub(crate) fn insert(&mut self, other: Self) {
self.0 |= other.0;
}
}

impl std::ops::BitOr for ArgFlags {
type Output = Self;

fn bitor(mut self, rhs: Self) -> Self::Output {
self.insert(rhs);
self
}
}

Expand All @@ -25,7 +40,7 @@ impl Default for ArgFlags {
/// [`Arg::unset_setting`]: crate::Arg::unset_setting()
/// [`Arg::is_set`]: crate::Arg::is_set()
#[derive(Debug, PartialEq, Copy, Clone)]
#[non_exhaustive]
#[repr(u8)]
pub(crate) enum ArgSettings {
Required,
Global,
Expand All @@ -48,55 +63,12 @@ pub(crate) enum ArgSettings {
Exclusive,
}

bitflags! {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct Flags: u32 {
const REQUIRED = 1;
const GLOBAL = 1 << 3;
const HIDDEN = 1 << 4;
const TRAILING_VARARG = 1 << 5;
const ALLOW_NEG_NUMS = 1 << 6;
const NEXT_LINE_HELP = 1 << 7;
const DELIM_NOT_SET = 1 << 10;
const HIDE_POS_VALS = 1 << 11;
const ALLOW_TAC_VALS = 1 << 12;
const REQUIRE_EQUALS = 1 << 13;
const LAST = 1 << 14;
const HIDE_DEFAULT_VAL = 1 << 15;
const CASE_INSENSITIVE = 1 << 16;
#[cfg(feature = "env")]
const HIDE_ENV_VALS = 1 << 17;
const HIDDEN_SHORT_H = 1 << 18;
const HIDDEN_LONG_H = 1 << 19;
#[cfg(feature = "env")]
const HIDE_ENV = 1 << 21;
const EXCLUSIVE = 1 << 23;
const NO_OP = 0;
impl ArgSettings {
fn bit(self) -> u32 {
1 << (self as u8)
}
}

impl_settings! { ArgSettings, ArgFlags,
Required => Flags::REQUIRED,
Global => Flags::GLOBAL,
Hidden => Flags::HIDDEN,
NextLineHelp => Flags::NEXT_LINE_HELP,
HidePossibleValues => Flags::HIDE_POS_VALS,
AllowHyphenValues => Flags::ALLOW_TAC_VALS,
AllowNegativeNumbers => Flags::ALLOW_NEG_NUMS,
RequireEquals => Flags::REQUIRE_EQUALS,
Last => Flags::LAST,
TrailingVarArg => Flags::TRAILING_VARARG,
IgnoreCase => Flags::CASE_INSENSITIVE,
#[cfg(feature = "env")]
HideEnv => Flags::HIDE_ENV,
#[cfg(feature = "env")]
HideEnvValues => Flags::HIDE_ENV_VALS,
HideDefaultValue => Flags::HIDE_DEFAULT_VAL,
HiddenShortHelp => Flags::HIDDEN_SHORT_H,
HiddenLongHelp => Flags::HIDDEN_LONG_H,
Exclusive => Flags::EXCLUSIVE
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -116,31 +88,4 @@ mod test {
let m = m.unset_setting(ArgSettings::Required);
assert!(!m.is_required_set(), "{m:#?}");
}

#[test]
fn setting_bitor() {
let m = Arg::new("setting_bitor")
.setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);

assert!(m.is_required_set());
assert!(m.is_hide_set());
assert!(m.is_last_set());
}

#[test]
fn unset_setting_bitor() {
let m = Arg::new("unset_setting_bitor")
.setting(ArgSettings::Required)
.setting(ArgSettings::Hidden)
.setting(ArgSettings::Last);

assert!(m.is_required_set());
assert!(m.is_hide_set());
assert!(m.is_last_set());

let m = m.unset_setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);
assert!(!m.is_required_set(), "{m:#?}");
assert!(!m.is_hide_set(), "{m:#?}");
assert!(!m.is_last_set(), "{m:#?}");
}
}
Loading

0 comments on commit 1938cb2

Please sign in to comment.