@@ -49,286 +49,3 @@ impl ConfigType for IgnoreList {
49
49
String :: from ( "[<string>,..]" )
50
50
}
51
51
}
52
-
53
- macro_rules! create_config {
54
- ( $( $i: ident: $ty: ty, $def: expr, $stb: expr, $( $dstring: expr ) ,+ ) ;+ $( ; ) * ) => (
55
- #[ cfg( test) ]
56
- use std:: collections:: HashSet ;
57
- use std:: io:: Write ;
58
-
59
- use serde:: { Deserialize , Serialize } ;
60
-
61
- #[ derive( Clone ) ]
62
- #[ allow( unreachable_pub) ]
63
- pub struct Config {
64
- // if a license_template_path has been specified, successfully read, parsed and compiled
65
- // into a regex, it will be stored here
66
- pub license_template: Option <Regex >,
67
- // For each config item, we store a bool indicating whether it has
68
- // been accessed and the value, and a bool whether the option was
69
- // manually initialised, or taken from the default,
70
- $( $i: ( Cell <bool >, bool , $ty, bool ) ) ,+
71
- }
72
-
73
- // Just like the Config struct but with each property wrapped
74
- // as Option<T>. This is used to parse a rustfmt.toml that doesn't
75
- // specify all properties of `Config`.
76
- // We first parse into `PartialConfig`, then create a default `Config`
77
- // and overwrite the properties with corresponding values from `PartialConfig`.
78
- #[ derive( Deserialize , Serialize , Clone ) ]
79
- #[ allow( unreachable_pub) ]
80
- pub struct PartialConfig {
81
- $( pub $i: Option <$ty>) ,+
82
- }
83
-
84
- // Macro hygiene won't allow us to make `set_$i()` methods on Config
85
- // for each item, so this struct is used to give the API to set values:
86
- // `config.set().option(false)`. It's pretty ugly. Consider replacing
87
- // with `config.set_option(false)` if we ever get a stable/usable
88
- // `concat_idents!()`.
89
- #[ allow( unreachable_pub) ]
90
- pub struct ConfigSetter <' a>( & ' a mut Config ) ;
91
-
92
- impl <' a> ConfigSetter <' a> {
93
- $(
94
- #[ allow( unreachable_pub) ]
95
- pub fn $i( & mut self , value: $ty) {
96
- ( self . 0 ) . $i. 2 = value;
97
- match stringify!( $i) {
98
- "max_width" | "use_small_heuristics" => self . 0 . set_heuristics( ) ,
99
- "license_template_path" => self . 0 . set_license_template( ) ,
100
- & _ => ( ) ,
101
- }
102
- }
103
- ) +
104
- }
105
-
106
- // Query each option, returns true if the user set the option, false if
107
- // a default was used.
108
- #[ allow( unreachable_pub) ]
109
- pub struct ConfigWasSet <' a>( & ' a Config ) ;
110
-
111
- impl <' a> ConfigWasSet <' a> {
112
- $(
113
- #[ allow( unreachable_pub) ]
114
- pub fn $i( & self ) -> bool {
115
- ( self . 0 ) . $i. 1
116
- }
117
- ) +
118
- }
119
-
120
- impl Config {
121
- $(
122
- #[ allow( unreachable_pub) ]
123
- pub fn $i( & self ) -> $ty {
124
- self . $i. 0 . set( true ) ;
125
- self . $i. 2 . clone( )
126
- }
127
- ) +
128
-
129
- #[ allow( unreachable_pub) ]
130
- pub fn set( & mut self ) -> ConfigSetter <' _> {
131
- ConfigSetter ( self )
132
- }
133
-
134
- #[ allow( unreachable_pub) ]
135
- pub fn was_set( & self ) -> ConfigWasSet <' _> {
136
- ConfigWasSet ( self )
137
- }
138
-
139
- fn fill_from_parsed_config( mut self , parsed: PartialConfig , dir: & Path ) -> Config {
140
- $(
141
- if let Some ( val) = parsed. $i {
142
- if self . $i. 3 {
143
- self . $i. 1 = true ;
144
- self . $i. 2 = val;
145
- } else {
146
- if crate :: is_nightly_channel!( ) {
147
- self . $i. 1 = true ;
148
- self . $i. 2 = val;
149
- } else {
150
- eprintln!( "Warning: can't set `{} = {:?}`, unstable features are only \
151
- available in nightly channel.", stringify!( $i) , val) ;
152
- }
153
- }
154
- }
155
- ) +
156
- self . set_heuristics( ) ;
157
- self . set_license_template( ) ;
158
- self . set_ignore( dir) ;
159
- self
160
- }
161
-
162
- /// Returns a hash set initialized with every user-facing config option name.
163
- #[ cfg( test) ]
164
- pub ( crate ) fn hash_set( ) -> HashSet <String > {
165
- let mut hash_set = HashSet :: new( ) ;
166
- $(
167
- hash_set. insert( stringify!( $i) . to_owned( ) ) ;
168
- ) +
169
- hash_set
170
- }
171
-
172
- pub ( crate ) fn is_valid_name( name: & str ) -> bool {
173
- match name {
174
- $(
175
- stringify!( $i) => true ,
176
- ) +
177
- _ => false ,
178
- }
179
- }
180
-
181
- #[ allow( unreachable_pub) ]
182
- pub fn is_valid_key_val( key: & str , val: & str ) -> bool {
183
- match key {
184
- $(
185
- stringify!( $i) => val. parse:: <$ty>( ) . is_ok( ) ,
186
- ) +
187
- _ => false ,
188
- }
189
- }
190
-
191
- #[ allow( unreachable_pub) ]
192
- pub fn used_options( & self ) -> PartialConfig {
193
- PartialConfig {
194
- $(
195
- $i: if self . $i. 0 . get( ) {
196
- Some ( self . $i. 2 . clone( ) )
197
- } else {
198
- None
199
- } ,
200
- ) +
201
- }
202
- }
203
-
204
- #[ allow( unreachable_pub) ]
205
- pub fn all_options( & self ) -> PartialConfig {
206
- PartialConfig {
207
- $(
208
- $i: Some ( self . $i. 2 . clone( ) ) ,
209
- ) +
210
- }
211
- }
212
-
213
- #[ allow( unreachable_pub) ]
214
- pub fn override_value( & mut self , key: & str , val: & str )
215
- {
216
- match key {
217
- $(
218
- stringify!( $i) => {
219
- self . $i. 1 = true ;
220
- self . $i. 2 = val. parse:: <$ty>( )
221
- . expect( & format!( "Failed to parse override for {} (\" {}\" ) as a {}" ,
222
- stringify!( $i) ,
223
- val,
224
- stringify!( $ty) ) ) ;
225
- }
226
- ) +
227
- _ => panic!( "Unknown config key in override: {}" , key)
228
- }
229
-
230
- match key {
231
- "max_width" | "use_small_heuristics" => self . set_heuristics( ) ,
232
- "license_template_path" => self . set_license_template( ) ,
233
- & _ => ( ) ,
234
- }
235
- }
236
-
237
- #[ allow( unreachable_pub) ]
238
- pub fn is_hidden_option( name: & str ) -> bool {
239
- const HIDE_OPTIONS : [ & str ; 4 ] =
240
- [ "verbose" , "verbose_diff" , "file_lines" , "width_heuristics" ] ;
241
- HIDE_OPTIONS . contains( & name)
242
- }
243
-
244
- #[ allow( unreachable_pub) ]
245
- pub fn print_docs( out: & mut dyn Write , include_unstable: bool ) {
246
- use std:: cmp;
247
- let max = 0 ;
248
- $( let max = cmp:: max( max, stringify!( $i) . len( ) +1 ) ; ) +
249
- let space_str = " " . repeat( max) ;
250
- writeln!( out, "Configuration Options:" ) . unwrap( ) ;
251
- $(
252
- if $stb || include_unstable {
253
- let name_raw = stringify!( $i) ;
254
-
255
- if !Config :: is_hidden_option( name_raw) {
256
- let mut name_out = String :: with_capacity( max) ;
257
- for _ in name_raw. len( ) ..max-1 {
258
- name_out. push( ' ' )
259
- }
260
- name_out. push_str( name_raw) ;
261
- name_out. push( ' ' ) ;
262
- let mut default_str = format!( "{}" , $def) ;
263
- if default_str. is_empty( ) {
264
- default_str = String :: from( "\" \" " ) ;
265
- }
266
- writeln!( out,
267
- "{}{} Default: {}{}" ,
268
- name_out,
269
- <$ty>:: doc_hint( ) ,
270
- default_str,
271
- if !$stb { " (unstable)" } else { "" } ) . unwrap( ) ;
272
- $(
273
- writeln!( out, "{}{}" , space_str, $dstring) . unwrap( ) ;
274
- ) +
275
- writeln!( out) . unwrap( ) ;
276
- }
277
- }
278
- ) +
279
- }
280
-
281
- fn set_heuristics( & mut self ) {
282
- if self . use_small_heuristics. 2 == Heuristics :: Default {
283
- let max_width = self . max_width. 2 ;
284
- self . set( ) . width_heuristics( WidthHeuristics :: scaled( max_width) ) ;
285
- } else if self . use_small_heuristics. 2 == Heuristics :: Max {
286
- let max_width = self . max_width. 2 ;
287
- self . set( ) . width_heuristics( WidthHeuristics :: set( max_width) ) ;
288
- } else {
289
- self . set( ) . width_heuristics( WidthHeuristics :: null( ) ) ;
290
- }
291
- }
292
-
293
- fn set_license_template( & mut self ) {
294
- if self . was_set( ) . license_template_path( ) {
295
- let lt_path = self . license_template_path( ) ;
296
- if lt_path. len( ) > 0 {
297
- match license:: load_and_compile_template( & lt_path) {
298
- Ok ( re) => self . license_template = Some ( re) ,
299
- Err ( msg) => eprintln!( "Warning for license template file {:?}: {}" ,
300
- lt_path, msg) ,
301
- }
302
- }
303
- }
304
- }
305
-
306
- fn set_ignore( & mut self , dir: & Path ) {
307
- self . ignore. 2 . add_prefix( dir) ;
308
- }
309
-
310
- #[ allow( unreachable_pub) ]
311
- /// Returns `true` if the config key was explicitly set and is the default value.
312
- pub fn is_default( & self , key: & str ) -> bool {
313
- $(
314
- if let stringify!( $i) = key {
315
- return self . $i. 1 && self . $i. 2 == $def;
316
- }
317
- ) +
318
- false
319
- }
320
- }
321
-
322
- // Template for the default configuration
323
- impl Default for Config {
324
- fn default ( ) -> Config {
325
- Config {
326
- license_template: None ,
327
- $(
328
- $i: ( Cell :: new( false ) , false , $def, $stb) ,
329
- ) +
330
- }
331
- }
332
- }
333
- )
334
- }
0 commit comments