Skip to content

Commit 27431a4

Browse files
committed
refactor(builder): Remove bitflags dependency
This saved 1.3 KiB When color support is enabled, this likely won't save on build times *until* `is-terminal` is removed. At that point, `bitflags` will no longer be in our dependency tree. I did not (yet) reproduce the `Debug` impl.
1 parent 3f09458 commit 27431a4

File tree

7 files changed

+78
-335
lines changed

7 files changed

+78
-335
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clap_builder/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ bench = false
5959

6060
[dependencies]
6161
clap_lex = { path = "../clap_lex", version = "0.5.0" }
62-
bitflags = "2.3.3"
6362
unicase = { version = "2.6.0", optional = true }
6463
strsim = { version = "0.10.0", optional = true }
6564
anstream = { version = "0.3.0", optional = true }
+29-119
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
// Std
2-
use std::ops::BitOr;
3-
41
#[allow(unused)]
52
use crate::Arg;
63
#[allow(unused)]
74
use crate::Command;
85

9-
// Third party
10-
use bitflags::bitflags;
6+
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
7+
pub(crate) struct AppFlags(u32);
8+
9+
impl AppFlags {
10+
pub(crate) fn set(&mut self, setting: AppSettings) {
11+
self.0 |= setting.bit();
12+
}
13+
14+
pub(crate) fn unset(&mut self, setting: AppSettings) {
15+
self.0 &= !setting.bit();
16+
}
17+
18+
pub(crate) fn is_set(&self, setting: AppSettings) -> bool {
19+
self.0 & setting.bit() != 0
20+
}
21+
22+
pub(crate) fn insert(&mut self, other: Self) {
23+
self.0 |= other.0;
24+
}
25+
}
1126

12-
#[doc(hidden)]
13-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
14-
pub(crate) struct AppFlags(Flags);
27+
impl std::ops::BitOr for AppFlags {
28+
type Output = Self;
1529

16-
impl Default for AppFlags {
17-
fn default() -> Self {
18-
AppFlags(Flags::COLOR_AUTO)
30+
fn bitor(mut self, rhs: Self) -> Self::Output {
31+
self.insert(rhs);
32+
self
1933
}
2034
}
2135

@@ -26,7 +40,7 @@ impl Default for AppFlags {
2640
///
2741
/// [`Command`]: crate::Command
2842
#[derive(Debug, PartialEq, Copy, Clone)]
29-
#[non_exhaustive]
43+
#[repr(u8)]
3044
pub(crate) enum AppSettings {
3145
IgnoreErrors,
3246
AllowHyphenValues,
@@ -62,112 +76,8 @@ pub(crate) enum AppSettings {
6276
BinNameBuilt,
6377
}
6478

65-
bitflags! {
66-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
67-
struct Flags: u64 {
68-
const SC_NEGATE_REQS = 1;
69-
const SC_REQUIRED = 1 << 1;
70-
const ARG_REQUIRED_ELSE_HELP = 1 << 2;
71-
const PROPAGATE_VERSION = 1 << 3;
72-
const DISABLE_VERSION_FOR_SC = 1 << 4;
73-
const WAIT_ON_ERROR = 1 << 6;
74-
const DISABLE_VERSION_FLAG = 1 << 10;
75-
const HIDDEN = 1 << 11;
76-
const TRAILING_VARARG = 1 << 12;
77-
const NO_BIN_NAME = 1 << 13;
78-
const ALLOW_UNK_SC = 1 << 14;
79-
const LEADING_HYPHEN = 1 << 16;
80-
const NO_POS_VALUES = 1 << 17;
81-
const NEXT_LINE_HELP = 1 << 18;
82-
const DISABLE_COLORED_HELP = 1 << 20;
83-
const COLOR_ALWAYS = 1 << 21;
84-
const COLOR_AUTO = 1 << 22;
85-
const COLOR_NEVER = 1 << 23;
86-
const DONT_DELIM_TRAIL = 1 << 24;
87-
const ALLOW_NEG_NUMS = 1 << 25;
88-
const DISABLE_HELP_SC = 1 << 27;
89-
const ARGS_NEGATE_SCS = 1 << 29;
90-
const PROPAGATE_VALS_DOWN = 1 << 30;
91-
const ALLOW_MISSING_POS = 1 << 31;
92-
const TRAILING_VALUES = 1 << 32;
93-
const BUILT = 1 << 33;
94-
const BIN_NAME_BUILT = 1 << 34;
95-
const VALID_ARG_FOUND = 1 << 35;
96-
const INFER_SUBCOMMANDS = 1 << 36;
97-
const CONTAINS_LAST = 1 << 37;
98-
const ARGS_OVERRIDE_SELF = 1 << 38;
99-
const HELP_REQUIRED = 1 << 39;
100-
const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40;
101-
const DISABLE_HELP_FLAG = 1 << 41;
102-
const INFER_LONG_ARGS = 1 << 43;
103-
const IGNORE_ERRORS = 1 << 44;
104-
const MULTICALL = 1 << 45;
105-
const EXPAND_HELP_SUBCOMMAND_TREES = 1 << 46;
106-
const NO_OP = 0;
79+
impl AppSettings {
80+
fn bit(self) -> u32 {
81+
1 << (self as u8)
10782
}
10883
}
109-
110-
impl_settings! { AppSettings, AppFlags,
111-
ArgRequiredElseHelp
112-
=> Flags::ARG_REQUIRED_ELSE_HELP,
113-
SubcommandPrecedenceOverArg
114-
=> Flags::SUBCOMMAND_PRECEDENCE_OVER_ARG,
115-
ArgsNegateSubcommands
116-
=> Flags::ARGS_NEGATE_SCS,
117-
AllowExternalSubcommands
118-
=> Flags::ALLOW_UNK_SC,
119-
AllowHyphenValues
120-
=> Flags::LEADING_HYPHEN,
121-
AllowNegativeNumbers
122-
=> Flags::ALLOW_NEG_NUMS,
123-
AllowMissingPositional
124-
=> Flags::ALLOW_MISSING_POS,
125-
ColorAlways
126-
=> Flags::COLOR_ALWAYS,
127-
ColorAuto
128-
=> Flags::COLOR_AUTO,
129-
ColorNever
130-
=> Flags::COLOR_NEVER,
131-
DontDelimitTrailingValues
132-
=> Flags::DONT_DELIM_TRAIL,
133-
DisableColoredHelp
134-
=> Flags::DISABLE_COLORED_HELP,
135-
DisableHelpSubcommand
136-
=> Flags::DISABLE_HELP_SC,
137-
DisableHelpFlag
138-
=> Flags::DISABLE_HELP_FLAG,
139-
DisableVersionFlag
140-
=> Flags::DISABLE_VERSION_FLAG,
141-
PropagateVersion
142-
=> Flags::PROPAGATE_VERSION,
143-
HidePossibleValues
144-
=> Flags::NO_POS_VALUES,
145-
HelpExpected
146-
=> Flags::HELP_REQUIRED,
147-
Hidden
148-
=> Flags::HIDDEN,
149-
Multicall
150-
=> Flags::MULTICALL,
151-
NoBinaryName
152-
=> Flags::NO_BIN_NAME,
153-
SubcommandsNegateReqs
154-
=> Flags::SC_NEGATE_REQS,
155-
SubcommandRequired
156-
=> Flags::SC_REQUIRED,
157-
TrailingVarArg
158-
=> Flags::TRAILING_VARARG,
159-
NextLineHelp
160-
=> Flags::NEXT_LINE_HELP,
161-
IgnoreErrors
162-
=> Flags::IGNORE_ERRORS,
163-
Built
164-
=> Flags::BUILT,
165-
BinNameBuilt
166-
=> Flags::BIN_NAME_BUILT,
167-
InferSubcommands
168-
=> Flags::INFER_SUBCOMMANDS,
169-
AllArgsOverrideSelf
170-
=> Flags::ARGS_OVERRIDE_SELF,
171-
InferLongArgs
172-
=> Flags::INFER_LONG_ARGS
173-
}

clap_builder/src/builder/arg.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -858,21 +858,15 @@ impl Arg {
858858

859859
#[inline]
860860
#[must_use]
861-
pub(crate) fn setting<F>(mut self, setting: F) -> Self
862-
where
863-
F: Into<ArgFlags>,
864-
{
865-
self.settings.insert(setting.into());
861+
pub(crate) fn setting(mut self, setting: ArgSettings) -> Self {
862+
self.settings.set(setting);
866863
self
867864
}
868865

869866
#[inline]
870867
#[must_use]
871-
pub(crate) fn unset_setting<F>(mut self, setting: F) -> Self
872-
where
873-
F: Into<ArgFlags>,
874-
{
875-
self.settings.remove(setting.into());
868+
pub(crate) fn unset_setting(mut self, setting: ArgSettings) -> Self {
869+
self.settings.unset(setting);
876870
self
877871
}
878872
}
+30-85
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
// Std
2-
use std::ops::BitOr;
3-
4-
// Third party
5-
use bitflags::bitflags;
6-
71
#[allow(unused)]
82
use crate::Arg;
93

10-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11-
pub(crate) struct ArgFlags(Flags);
4+
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
5+
pub(crate) struct ArgFlags(u32);
6+
7+
impl ArgFlags {
8+
pub(crate) fn set(&mut self, setting: ArgSettings) {
9+
self.0 |= setting.bit();
10+
}
11+
12+
pub(crate) fn unset(&mut self, setting: ArgSettings) {
13+
self.0 &= !setting.bit();
14+
}
15+
16+
pub(crate) fn is_set(&self, setting: ArgSettings) -> bool {
17+
self.0 & setting.bit() != 0
18+
}
1219

13-
impl Default for ArgFlags {
14-
fn default() -> Self {
15-
Self::empty()
20+
pub(crate) fn insert(&mut self, other: Self) {
21+
self.0 |= other.0;
22+
}
23+
}
24+
25+
impl std::ops::BitOr for ArgFlags {
26+
type Output = Self;
27+
28+
fn bitor(mut self, rhs: Self) -> Self::Output {
29+
self.insert(rhs);
30+
self
1631
}
1732
}
1833

@@ -25,7 +40,7 @@ impl Default for ArgFlags {
2540
/// [`Arg::unset_setting`]: crate::Arg::unset_setting()
2641
/// [`Arg::is_set`]: crate::Arg::is_set()
2742
#[derive(Debug, PartialEq, Copy, Clone)]
28-
#[non_exhaustive]
43+
#[repr(u8)]
2944
pub(crate) enum ArgSettings {
3045
Required,
3146
Global,
@@ -48,55 +63,12 @@ pub(crate) enum ArgSettings {
4863
Exclusive,
4964
}
5065

51-
bitflags! {
52-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
53-
struct Flags: u32 {
54-
const REQUIRED = 1;
55-
const GLOBAL = 1 << 3;
56-
const HIDDEN = 1 << 4;
57-
const TRAILING_VARARG = 1 << 5;
58-
const ALLOW_NEG_NUMS = 1 << 6;
59-
const NEXT_LINE_HELP = 1 << 7;
60-
const DELIM_NOT_SET = 1 << 10;
61-
const HIDE_POS_VALS = 1 << 11;
62-
const ALLOW_TAC_VALS = 1 << 12;
63-
const REQUIRE_EQUALS = 1 << 13;
64-
const LAST = 1 << 14;
65-
const HIDE_DEFAULT_VAL = 1 << 15;
66-
const CASE_INSENSITIVE = 1 << 16;
67-
#[cfg(feature = "env")]
68-
const HIDE_ENV_VALS = 1 << 17;
69-
const HIDDEN_SHORT_H = 1 << 18;
70-
const HIDDEN_LONG_H = 1 << 19;
71-
#[cfg(feature = "env")]
72-
const HIDE_ENV = 1 << 21;
73-
const EXCLUSIVE = 1 << 23;
74-
const NO_OP = 0;
66+
impl ArgSettings {
67+
fn bit(self) -> u32 {
68+
1 << (self as u8)
7569
}
7670
}
7771

78-
impl_settings! { ArgSettings, ArgFlags,
79-
Required => Flags::REQUIRED,
80-
Global => Flags::GLOBAL,
81-
Hidden => Flags::HIDDEN,
82-
NextLineHelp => Flags::NEXT_LINE_HELP,
83-
HidePossibleValues => Flags::HIDE_POS_VALS,
84-
AllowHyphenValues => Flags::ALLOW_TAC_VALS,
85-
AllowNegativeNumbers => Flags::ALLOW_NEG_NUMS,
86-
RequireEquals => Flags::REQUIRE_EQUALS,
87-
Last => Flags::LAST,
88-
TrailingVarArg => Flags::TRAILING_VARARG,
89-
IgnoreCase => Flags::CASE_INSENSITIVE,
90-
#[cfg(feature = "env")]
91-
HideEnv => Flags::HIDE_ENV,
92-
#[cfg(feature = "env")]
93-
HideEnvValues => Flags::HIDE_ENV_VALS,
94-
HideDefaultValue => Flags::HIDE_DEFAULT_VAL,
95-
HiddenShortHelp => Flags::HIDDEN_SHORT_H,
96-
HiddenLongHelp => Flags::HIDDEN_LONG_H,
97-
Exclusive => Flags::EXCLUSIVE
98-
}
99-
10072
#[cfg(test)]
10173
mod test {
10274
use super::*;
@@ -116,31 +88,4 @@ mod test {
11688
let m = m.unset_setting(ArgSettings::Required);
11789
assert!(!m.is_required_set(), "{m:#?}");
11890
}
119-
120-
#[test]
121-
fn setting_bitor() {
122-
let m = Arg::new("setting_bitor")
123-
.setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);
124-
125-
assert!(m.is_required_set());
126-
assert!(m.is_hide_set());
127-
assert!(m.is_last_set());
128-
}
129-
130-
#[test]
131-
fn unset_setting_bitor() {
132-
let m = Arg::new("unset_setting_bitor")
133-
.setting(ArgSettings::Required)
134-
.setting(ArgSettings::Hidden)
135-
.setting(ArgSettings::Last);
136-
137-
assert!(m.is_required_set());
138-
assert!(m.is_hide_set());
139-
assert!(m.is_last_set());
140-
141-
let m = m.unset_setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);
142-
assert!(!m.is_required_set(), "{m:#?}");
143-
assert!(!m.is_hide_set(), "{m:#?}");
144-
assert!(!m.is_last_set(), "{m:#?}");
145-
}
14691
}

0 commit comments

Comments
 (0)