@@ -65,6 +65,13 @@ impl ConfigType for IgnoreList {
65
65
}
66
66
}
67
67
68
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
69
+ #[ allow( unreachable_pub) ]
70
+ pub enum Clamp {
71
+ Yes ,
72
+ No ,
73
+ }
74
+
68
75
macro_rules! create_config {
69
76
// Options passed into the macro.
70
77
//
@@ -79,6 +86,7 @@ macro_rules! create_config {
79
86
80
87
use serde:: { Deserialize , Serialize } ;
81
88
use $crate:: config:: style_edition:: StyleEditionDefault ;
89
+ use $crate:: config:: config_type:: Clamp ;
82
90
83
91
#[ derive( Clone ) ]
84
92
#[ allow( unreachable_pub) ]
@@ -112,13 +120,16 @@ macro_rules! create_config {
112
120
// with `config.set_option(false)` if we ever get a stable/usable
113
121
// `concat_idents!()`.
114
122
#[ allow( unreachable_pub) ]
115
- pub struct ConfigSetter <' a>( & ' a mut Config ) ;
123
+ pub struct ConfigSetter <' a> {
124
+ config: & ' a mut Config ,
125
+ clamp: Clamp ,
126
+ }
116
127
117
128
impl <' a> ConfigSetter <' a> {
118
129
$(
119
130
#[ allow( unreachable_pub) ]
120
131
pub fn $i( & mut self , value: <$ty as StyleEditionDefault >:: ConfigType ) {
121
- ( self . 0 ) . $i. 2 = value;
132
+ self . config . $i. 2 = value;
122
133
match stringify!( $i) {
123
134
"max_width"
124
135
| "use_small_heuristics"
@@ -129,11 +140,11 @@ macro_rules! create_config {
129
140
| "struct_lit_width"
130
141
| "struct_variant_width"
131
142
| "array_width"
132
- | "chain_width" => self . 0 . set_heuristics( ) ,
133
- "merge_imports" => self . 0 . set_merge_imports( ) ,
134
- "fn_args_layout" => self . 0 . set_fn_args_layout( ) ,
135
- "hide_parse_errors" => self . 0 . set_hide_parse_errors( ) ,
136
- "version" => self . 0 . set_version( ) ,
143
+ | "chain_width" => self . config . set_heuristics( self . clamp ) ,
144
+ "merge_imports" => self . config . set_merge_imports( ) ,
145
+ "fn_args_layout" => self . config . set_fn_args_layout( ) ,
146
+ "hide_parse_errors" => self . config . set_hide_parse_errors( ) ,
147
+ "version" => self . config . set_version( ) ,
137
148
& _ => ( ) ,
138
149
}
139
150
}
@@ -159,7 +170,7 @@ macro_rules! create_config {
159
170
| "struct_lit_width"
160
171
| "struct_variant_width"
161
172
| "array_width"
162
- | "chain_width" => self . 0 . set_heuristics( ) ,
173
+ | "chain_width" => self . 0 . set_heuristics( Clamp :: No ) ,
163
174
"merge_imports" => self . 0 . set_merge_imports( ) ,
164
175
"fn_args_layout" => self . 0 . set_fn_args_layout( ) ,
165
176
"hide_parse_errors" => self . 0 . set_hide_parse_errors( ) ,
@@ -226,7 +237,18 @@ macro_rules! create_config {
226
237
227
238
#[ allow( unreachable_pub) ]
228
239
pub fn set( & mut self ) -> ConfigSetter <' _> {
229
- ConfigSetter ( self )
240
+ ConfigSetter {
241
+ config: self ,
242
+ clamp: Clamp :: No ,
243
+ }
244
+ }
245
+
246
+ #[ allow( unreachable_pub) ]
247
+ pub fn set_clamp( & mut self ) -> ConfigSetter <' _> {
248
+ ConfigSetter {
249
+ config: self ,
250
+ clamp: Clamp :: Yes ,
251
+ }
230
252
}
231
253
232
254
#[ allow( unreachable_pub) ]
@@ -256,7 +278,7 @@ macro_rules! create_config {
256
278
}
257
279
}
258
280
) +
259
- self . set_heuristics( ) ;
281
+ self . set_heuristics( Clamp :: No ) ;
260
282
self . set_ignore( dir) ;
261
283
self . set_merge_imports( ) ;
262
284
self . set_fn_args_layout( ) ;
@@ -359,7 +381,7 @@ macro_rules! create_config {
359
381
| "struct_lit_width"
360
382
| "struct_variant_width"
361
383
| "array_width"
362
- | "chain_width" => self . set_heuristics( ) ,
384
+ | "chain_width" => self . set_heuristics( Clamp :: No ) ,
363
385
"merge_imports" => self . set_merge_imports( ) ,
364
386
"fn_args_layout" => self . set_fn_args_layout( ) ,
365
387
"hide_parse_errors" => self . set_hide_parse_errors( ) ,
@@ -423,26 +445,27 @@ macro_rules! create_config {
423
445
) +
424
446
}
425
447
426
- fn set_width_heuristics( & mut self , heuristics: WidthHeuristics ) {
448
+ fn set_width_heuristics( & mut self , heuristics: WidthHeuristics , clamp : Clamp ) {
427
449
let max_width = self . max_width. 2 ;
428
450
let get_width_value = |
429
451
was_set: bool ,
430
452
override_value: usize ,
431
453
heuristic_value: usize ,
432
454
config_key: & str ,
433
455
| -> usize {
434
- if !was_set {
435
- return heuristic_value;
436
- }
437
- if override_value > max_width {
456
+ let value = if !was_set {
457
+ heuristic_value
458
+ } else {
459
+ override_value
460
+ } ;
461
+ if clamp == Clamp :: No && value > max_width {
438
462
eprintln!(
439
463
"`{0}` cannot have a value that exceeds `max_width`. \
440
464
`{0}` will be set to the same value as `max_width`",
441
465
config_key,
442
466
) ;
443
- return max_width;
444
467
}
445
- override_value
468
+ value . min ( max_width )
446
469
} ;
447
470
448
471
let fn_call_width = get_width_value(
@@ -510,13 +533,14 @@ macro_rules! create_config {
510
533
self . single_line_let_else_max_width. 2 = single_line_let_else_max_width;
511
534
}
512
535
513
- fn set_heuristics( & mut self ) {
536
+ fn set_heuristics( & mut self , clamp : Clamp ) {
514
537
let max_width = self . max_width. 2 ;
515
538
match self . use_small_heuristics. 2 {
516
539
Heuristics :: Default =>
517
- self . set_width_heuristics( WidthHeuristics :: scaled( max_width) ) ,
518
- Heuristics :: Max => self . set_width_heuristics( WidthHeuristics :: set( max_width) ) ,
519
- Heuristics :: Off => self . set_width_heuristics( WidthHeuristics :: null( ) ) ,
540
+ self . set_width_heuristics( WidthHeuristics :: scaled( max_width) , clamp) ,
541
+ Heuristics :: Max =>
542
+ self . set_width_heuristics( WidthHeuristics :: set( max_width) , clamp) ,
543
+ Heuristics :: Off => self . set_width_heuristics( WidthHeuristics :: null( ) , clamp) ,
520
544
} ;
521
545
}
522
546
0 commit comments