Skip to content

Commit 2442f10

Browse files
committed
Use a struct for Config fields
To make it more readable.
1 parent 5f390e4 commit 2442f10

File tree

1 file changed

+77
-72
lines changed

1 file changed

+77
-72
lines changed

src/config/config_type.rs

+77-72
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,20 @@ macro_rules! create_config {
8383
#[derive(Clone)]
8484
#[allow(unreachable_pub)]
8585
pub struct Config {
86-
// For each config item, we store:
87-
//
88-
// - 0: true if the value has been access
89-
// - 1: true if the option was manually initialized
90-
// - 2: the option value
91-
// - 3: true if the option is stable
92-
// - 4: true if the option was set manually from a CLI flag
93-
$($i: (Cell<bool>, bool, <$ty as StyleEditionDefault>::ConfigType, bool, bool)),+
86+
$($i: ConfigOption<<$ty as StyleEditionDefault>::ConfigType>),+
87+
}
88+
89+
#[derive(Clone)]
90+
struct ConfigOption<T> {
91+
value: T,
92+
/// `true` if the value has been accessed
93+
is_used: std::cell::Cell<bool>,
94+
/// `true` if the option is stable
95+
is_stable: bool,
96+
/// `true` if the option was manually initialized
97+
was_set: bool,
98+
/// `true` if the option was set manually from a CLI flag
99+
was_set_cli: bool,
94100
}
95101

96102
// Just like the Config struct but with each property wrapped
@@ -116,7 +122,7 @@ macro_rules! create_config {
116122
$(
117123
#[allow(unreachable_pub)]
118124
pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
119-
(self.0).$i.2 = value;
125+
(self.0).$i.value = value;
120126
match stringify!($i) {
121127
"max_width"
122128
| "use_small_heuristics"
@@ -145,8 +151,8 @@ macro_rules! create_config {
145151
$(
146152
#[allow(unreachable_pub)]
147153
pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
148-
(self.0).$i.2 = value;
149-
(self.0).$i.4 = true;
154+
(self.0).$i.value = value;
155+
(self.0).$i.was_set_cli = true;
150156
match stringify!($i) {
151157
"max_width"
152158
| "use_small_heuristics"
@@ -177,7 +183,7 @@ macro_rules! create_config {
177183
$(
178184
#[allow(unreachable_pub)]
179185
pub fn $i(&self) -> bool {
180-
(self.0).$i.1
186+
(self.0).$i.was_set
181187
}
182188
)+
183189
}
@@ -191,7 +197,7 @@ macro_rules! create_config {
191197
$(
192198
#[allow(unreachable_pub)]
193199
pub fn $i(&self) -> bool {
194-
(self.0).$i.4
200+
(self.0).$i.was_set_cli
195201
}
196202
)+
197203
}
@@ -200,24 +206,24 @@ macro_rules! create_config {
200206
$(
201207
#[allow(unreachable_pub)]
202208
pub fn $i(&self) -> <$ty as StyleEditionDefault>::ConfigType {
203-
self.$i.0.set(true);
204-
self.$i.2.clone()
209+
self.$i.is_used.set(true);
210+
self.$i.value.clone()
205211
}
206212
)+
207213

208214
#[allow(unreachable_pub)]
209215
pub(super) fn default_with_style_edition(style_edition: StyleEdition) -> Config {
210216
Config {
211217
$(
212-
$i: (
213-
Cell::new(false),
214-
false,
215-
<$ty as StyleEditionDefault>::style_edition_default(
216-
style_edition
217-
),
218-
$stb,
219-
false,
218+
$i: ConfigOption {
219+
is_used: Cell::new(false),
220+
was_set: false,
221+
value: <$ty as StyleEditionDefault>::style_edition_default(
222+
style_edition
220223
),
224+
is_stable: $stb,
225+
was_set_cli: false,
226+
},
221227
)+
222228
}
223229
}
@@ -245,12 +251,11 @@ macro_rules! create_config {
245251
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
246252
$(
247253
if let Some(option_value) = parsed.$i {
248-
let option_stable = self.$i.3;
249254
if $crate::config::config_type::is_stable_option_and_value(
250-
stringify!($i), option_stable, &option_value
255+
stringify!($i), self.$i.is_stable, &option_value
251256
) {
252-
self.$i.1 = true;
253-
self.$i.2 = option_value;
257+
self.$i.was_set = true;
258+
self.$i.value = option_value;
254259
}
255260
}
256261
)+
@@ -298,8 +303,8 @@ macro_rules! create_config {
298303
pub fn used_options(&self) -> PartialConfig {
299304
PartialConfig {
300305
$(
301-
$i: if self.$i.0.get() {
302-
Some(self.$i.2.clone())
306+
$i: if self.$i.is_used.get() {
307+
Some(self.$i.value.clone())
303308
} else {
304309
None
305310
},
@@ -311,7 +316,7 @@ macro_rules! create_config {
311316
pub fn all_options(&self) -> PartialConfig {
312317
PartialConfig {
313318
$(
314-
$i: Some(self.$i.2.clone()),
319+
$i: Some(self.$i.value.clone()),
315320
)+
316321
}
317322
}
@@ -340,8 +345,8 @@ macro_rules! create_config {
340345
//
341346
// For now, do not validate whether the option or value is stable,
342347
// just always set it.
343-
self.$i.1 = true;
344-
self.$i.2 = value;
348+
self.$i.was_set = true;
349+
self.$i.value = value;
345350
}
346351
)+
347352
_ => panic!("Unknown config key in override: {}", key)
@@ -422,7 +427,7 @@ macro_rules! create_config {
422427
}
423428

424429
fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
425-
let max_width = self.max_width.2;
430+
let max_width = self.max_width.value;
426431
let get_width_value = |
427432
was_set: bool,
428433
override_value: usize,
@@ -444,73 +449,73 @@ macro_rules! create_config {
444449
};
445450

446451
let fn_call_width = get_width_value(
447-
self.was_set().fn_call_width(),
448-
self.fn_call_width.2,
452+
self.fn_call_width.was_set,
453+
self.fn_call_width.value,
449454
heuristics.fn_call_width,
450455
"fn_call_width",
451456
);
452-
self.fn_call_width.2 = fn_call_width;
457+
self.fn_call_width.value = fn_call_width;
453458

454459
let attr_fn_like_width = get_width_value(
455-
self.was_set().attr_fn_like_width(),
456-
self.attr_fn_like_width.2,
460+
self.attr_fn_like_width.was_set,
461+
self.attr_fn_like_width.value,
457462
heuristics.attr_fn_like_width,
458463
"attr_fn_like_width",
459464
);
460-
self.attr_fn_like_width.2 = attr_fn_like_width;
465+
self.attr_fn_like_width.value = attr_fn_like_width;
461466

462467
let struct_lit_width = get_width_value(
463-
self.was_set().struct_lit_width(),
464-
self.struct_lit_width.2,
468+
self.struct_lit_width.was_set,
469+
self.struct_lit_width.value,
465470
heuristics.struct_lit_width,
466471
"struct_lit_width",
467472
);
468-
self.struct_lit_width.2 = struct_lit_width;
473+
self.struct_lit_width.value = struct_lit_width;
469474

470475
let struct_variant_width = get_width_value(
471-
self.was_set().struct_variant_width(),
472-
self.struct_variant_width.2,
476+
self.struct_variant_width.was_set,
477+
self.struct_variant_width.value,
473478
heuristics.struct_variant_width,
474479
"struct_variant_width",
475480
);
476-
self.struct_variant_width.2 = struct_variant_width;
481+
self.struct_variant_width.value = struct_variant_width;
477482

478483
let array_width = get_width_value(
479-
self.was_set().array_width(),
480-
self.array_width.2,
484+
self.array_width.was_set,
485+
self.array_width.value,
481486
heuristics.array_width,
482487
"array_width",
483488
);
484-
self.array_width.2 = array_width;
489+
self.array_width.value = array_width;
485490

486491
let chain_width = get_width_value(
487-
self.was_set().chain_width(),
488-
self.chain_width.2,
492+
self.chain_width.was_set,
493+
self.chain_width.value,
489494
heuristics.chain_width,
490495
"chain_width",
491496
);
492-
self.chain_width.2 = chain_width;
497+
self.chain_width.value = chain_width;
493498

494499
let single_line_if_else_max_width = get_width_value(
495-
self.was_set().single_line_if_else_max_width(),
496-
self.single_line_if_else_max_width.2,
500+
self.single_line_if_else_max_width.was_set,
501+
self.single_line_if_else_max_width.value,
497502
heuristics.single_line_if_else_max_width,
498503
"single_line_if_else_max_width",
499504
);
500-
self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
505+
self.single_line_if_else_max_width.value = single_line_if_else_max_width;
501506

502507
let single_line_let_else_max_width = get_width_value(
503-
self.was_set().single_line_let_else_max_width(),
504-
self.single_line_let_else_max_width.2,
508+
self.single_line_let_else_max_width.was_set,
509+
self.single_line_let_else_max_width.value,
505510
heuristics.single_line_let_else_max_width,
506511
"single_line_let_else_max_width",
507512
);
508-
self.single_line_let_else_max_width.2 = single_line_let_else_max_width;
513+
self.single_line_let_else_max_width.value = single_line_let_else_max_width;
509514
}
510515

511516
fn set_heuristics(&mut self) {
512-
let max_width = self.max_width.2;
513-
match self.use_small_heuristics.2 {
517+
let max_width = self.max_width.value;
518+
match self.use_small_heuristics.value {
514519
Heuristics::Default =>
515520
self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
516521
Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
@@ -519,17 +524,17 @@ macro_rules! create_config {
519524
}
520525

521526
fn set_ignore(&mut self, dir: &Path) {
522-
self.ignore.2.add_prefix(dir);
527+
self.ignore.value.add_prefix(dir);
523528
}
524529

525530
fn set_merge_imports(&mut self) {
526-
if self.was_set().merge_imports() {
531+
if self.merge_imports.was_set {
527532
eprintln!(
528533
"Warning: the `merge_imports` option is deprecated. \
529534
Use `imports_granularity=\"Crate\"` instead"
530535
);
531-
if !self.was_set().imports_granularity() {
532-
self.imports_granularity.2 = if self.merge_imports() {
536+
if !self.imports_granularity.was_set {
537+
self.imports_granularity.value = if self.merge_imports() {
533538
ImportGranularity::Crate
534539
} else {
535540
ImportGranularity::Preserve
@@ -539,31 +544,31 @@ macro_rules! create_config {
539544
}
540545

541546
fn set_fn_args_layout(&mut self) {
542-
if self.was_set().fn_args_layout() {
547+
if self.fn_args_layout.was_set {
543548
eprintln!(
544549
"Warning: the `fn_args_layout` option is deprecated. \
545550
Use `fn_params_layout`. instead"
546551
);
547-
if !self.was_set().fn_params_layout() {
548-
self.fn_params_layout.2 = self.fn_args_layout();
552+
if !self.fn_params_layout.was_set {
553+
self.fn_params_layout.value = self.fn_args_layout();
549554
}
550555
}
551556
}
552557

553558
fn set_hide_parse_errors(&mut self) {
554-
if self.was_set().hide_parse_errors() {
559+
if self.hide_parse_errors.was_set {
555560
eprintln!(
556561
"Warning: the `hide_parse_errors` option is deprecated. \
557562
Use `show_parse_errors` instead"
558563
);
559-
if !self.was_set().show_parse_errors() {
560-
self.show_parse_errors.2 = self.hide_parse_errors();
564+
if !self.show_parse_errors.was_set {
565+
self.show_parse_errors.value = self.hide_parse_errors();
561566
}
562567
}
563568
}
564569

565570
fn set_version(&mut self) {
566-
if !self.was_set().version() {
571+
if !self.version.was_set {
567572
return;
568573
}
569574

@@ -572,7 +577,7 @@ macro_rules! create_config {
572577
Use `style_edition` instead."
573578
);
574579

575-
if self.was_set().style_edition() || self.was_set_cli().style_edition() {
580+
if self.style_edition.was_set || self.style_edition.was_set_cli {
576581
eprintln!(
577582
"Warning: the deprecated `version` option was \
578583
used in conjunction with the `style_edition` \
@@ -591,7 +596,7 @@ macro_rules! create_config {
591596
style_edition
592597
);
593598
if let stringify!($i) = key {
594-
return self.$i.1 && self.$i.2 == default_value;
599+
return self.$i.was_set && self.$i.value == default_value;
595600
}
596601
)+
597602
false

0 commit comments

Comments
 (0)