@@ -3,133 +3,18 @@ use std::fmt;
3
3
use std:: path:: { Path , PathBuf } ;
4
4
5
5
use atty;
6
+ use config_proc_macro:: config_type;
6
7
use serde:: de:: { Deserialize , Deserializer , SeqAccess , Visitor } ;
7
8
8
- use crate :: config:: config_type:: ConfigType ;
9
9
use crate :: config:: lists:: * ;
10
10
use crate :: config:: Config ;
11
11
12
- /// Macro that will stringify the enum variants or a provided textual repr
13
- #[ macro_export]
14
- macro_rules! configuration_option_enum_stringify {
15
- ( $variant: ident) => {
16
- stringify!( $variant)
17
- } ;
18
-
19
- ( $_variant: ident: $value: expr) => {
20
- stringify!( $value)
21
- } ;
22
- }
23
-
24
- /// Macro for deriving implementations of Serialize/Deserialize for enums
25
- #[ macro_export]
26
- macro_rules! impl_enum_serialize_and_deserialize {
27
- ( $e: ident, $( $variant: ident $( : $value: expr) * ) ,* ) => {
28
- impl :: serde:: ser:: Serialize for $e {
29
- fn serialize<S >( & self , serializer: S ) -> Result <S :: Ok , S :: Error >
30
- where S : :: serde:: ser:: Serializer
31
- {
32
- use serde:: ser:: Error ;
33
-
34
- // We don't know whether the user of the macro has given us all options.
35
- #[ allow( unreachable_patterns) ]
36
- match * self {
37
- $(
38
- $e:: $variant => serializer. serialize_str(
39
- configuration_option_enum_stringify!( $variant $( : $value) * )
40
- ) ,
41
- ) *
42
- _ => {
43
- Err ( S :: Error :: custom( format!( "Cannot serialize {:?}" , self ) ) )
44
- }
45
- }
46
- }
47
- }
48
-
49
- impl <' de> :: serde:: de:: Deserialize <' de> for $e {
50
- fn deserialize<D >( d: D ) -> Result <Self , D :: Error >
51
- where D : :: serde:: Deserializer <' de> {
52
- use serde:: de:: { Error , Visitor } ;
53
- use std:: marker:: PhantomData ;
54
- use std:: fmt;
55
- struct StringOnly <T >( PhantomData <T >) ;
56
- impl <' de, T > Visitor <' de> for StringOnly <T >
57
- where T : :: serde:: Deserializer <' de> {
58
- type Value = String ;
59
- fn expecting( & self , formatter: & mut fmt:: Formatter <' _>) -> fmt:: Result {
60
- formatter. write_str( "string" )
61
- }
62
- fn visit_str<E >( self , value: & str ) -> Result <String , E > {
63
- Ok ( String :: from( value) )
64
- }
65
- }
66
- let s = d. deserialize_string( StringOnly :: <D >( PhantomData ) ) ?;
67
- $(
68
- if configuration_option_enum_stringify!( $variant $( : $value) * )
69
- . eq_ignore_ascii_case( & s) {
70
- return Ok ( $e:: $variant) ;
71
- }
72
- ) *
73
- static ALLOWED : & ' static [ & str ] = & [
74
- $( configuration_option_enum_stringify!( $variant $( : $value) * ) , ) * ] ;
75
- Err ( D :: Error :: unknown_variant( & s, ALLOWED ) )
76
- }
77
- }
78
-
79
- impl :: std:: str :: FromStr for $e {
80
- type Err = & ' static str ;
81
-
82
- fn from_str( s: & str ) -> Result <Self , Self :: Err > {
83
- $(
84
- if configuration_option_enum_stringify!( $variant $( : $value) * )
85
- . eq_ignore_ascii_case( s) {
86
- return Ok ( $e:: $variant) ;
87
- }
88
- ) *
89
- Err ( "Bad variant" )
90
- }
91
- }
92
-
93
- impl ConfigType for $e {
94
- fn doc_hint( ) -> String {
95
- let mut variants = Vec :: new( ) ;
96
- $(
97
- variants. push(
98
- configuration_option_enum_stringify!( $variant $( : $value) * )
99
- ) ;
100
- ) *
101
- format!( "[{}]" , variants. join( "|" ) )
102
- }
103
- }
104
- } ;
105
- }
106
-
107
- macro_rules! configuration_option_enum {
108
- ( $e: ident: $( $name: ident $( : $value: expr) * ) ,+ $( , ) * ) => (
109
- #[ derive( Copy , Clone , Eq , PartialEq ) ]
110
- pub enum $e {
111
- $( $name ) ,+
112
- }
113
-
114
- impl :: std:: fmt:: Debug for $e {
115
- fn fmt( & self , f: & mut :: std:: fmt:: Formatter <' _>) -> :: std:: fmt:: Result {
116
- f. write_str( match self {
117
- $(
118
- $e:: $name => configuration_option_enum_stringify!( $name $( : $value) * ) ,
119
- ) +
120
- } )
121
- }
122
- }
123
-
124
- impl_enum_serialize_and_deserialize!( $e, $( $name $( : $value) * ) ,+) ;
125
- ) ;
126
- }
127
-
128
- configuration_option_enum ! { NewlineStyle :
129
- Auto , // Auto-detect based on the raw source input
12
+ #[ config_type]
13
+ pub enum NewlineStyle {
14
+ Auto , // Auto-detect based on the raw source input
130
15
Windows , // \r\n
131
- Unix , // \n
132
- Native , // \r\n in Windows, \n on other platforms
16
+ Unix , // \n
17
+ Native , // \r\n in Windows, \n on other platforms
133
18
}
134
19
135
20
impl NewlineStyle {
@@ -188,15 +73,17 @@ impl NewlineStyle {
188
73
}
189
74
}
190
75
191
- configuration_option_enum ! { BraceStyle :
76
+ #[ config_type]
77
+ pub enum BraceStyle {
192
78
AlwaysNextLine ,
193
79
PreferSameLine ,
194
80
// Prefer same line except where there is a where-clause, in which case force
195
81
// the brace to the next line.
196
82
SameLineWhere ,
197
83
}
198
84
199
- configuration_option_enum ! { ControlBraceStyle :
85
+ #[ config_type]
86
+ pub enum ControlBraceStyle {
200
87
// K&R style, Rust community default
201
88
AlwaysSameLine ,
202
89
// Stroustrup style
@@ -205,15 +92,17 @@ configuration_option_enum! { ControlBraceStyle:
205
92
AlwaysNextLine ,
206
93
}
207
94
208
- configuration_option_enum ! { IndentStyle :
95
+ #[ config_type]
96
+ pub enum IndentStyle {
209
97
// First line on the same line as the opening brace, all lines aligned with
210
98
// the first line.
211
99
Visual ,
212
100
// First line is on a new line and all lines align with block indent.
213
101
Block ,
214
102
}
215
103
216
- configuration_option_enum ! { Density :
104
+ #[ config_type]
105
+ pub enum Density {
217
106
// Fit as much on one line as possible.
218
107
Compressed ,
219
108
// Use more lines.
@@ -222,14 +111,16 @@ configuration_option_enum! { Density:
222
111
Vertical ,
223
112
}
224
113
225
- configuration_option_enum ! { TypeDensity :
114
+ #[ config_type]
115
+ pub enum TypeDensity {
226
116
// No spaces around "=" and "+"
227
117
Compressed ,
228
118
// Spaces around " = " and " + "
229
119
Wide ,
230
120
}
231
121
232
- configuration_option_enum ! { Heuristics :
122
+ #[ config_type]
123
+ pub enum Heuristics {
233
124
// Turn off any heuristics
234
125
Off ,
235
126
// Turn on max heuristics
@@ -249,15 +140,17 @@ impl Density {
249
140
}
250
141
}
251
142
252
- configuration_option_enum ! { ReportTactic :
143
+ #[ config_type]
144
+ pub enum ReportTactic {
253
145
Always ,
254
146
Unnumbered ,
255
147
Never ,
256
148
}
257
149
258
150
// What Rustfmt should emit. Mostly corresponds to the `--emit` command line
259
151
// option.
260
- configuration_option_enum ! { EmitMode :
152
+ #[ config_type]
153
+ pub enum EmitMode {
261
154
// Emits to files.
262
155
Files ,
263
156
// Writes the output to stdout.
@@ -275,7 +168,8 @@ configuration_option_enum! { EmitMode:
275
168
}
276
169
277
170
// Client-preference for coloured output.
278
- configuration_option_enum ! { Color :
171
+ #[ config_type]
172
+ pub enum Color {
279
173
// Always use color, whether it is a piped or terminal output
280
174
Always ,
281
175
// Never use color
@@ -284,7 +178,8 @@ configuration_option_enum! { Color:
284
178
Auto ,
285
179
}
286
180
287
- configuration_option_enum ! { Version :
181
+ #[ config_type]
182
+ pub enum Version {
288
183
// 1.x.y
289
184
One ,
290
185
// 2.x.y
@@ -303,7 +198,8 @@ impl Color {
303
198
}
304
199
305
200
// How chatty should Rustfmt be?
306
- configuration_option_enum ! { Verbosity :
201
+ #[ config_type]
202
+ pub enum Verbosity {
307
203
// Emit more.
308
204
Verbose ,
309
205
Normal ,
@@ -474,9 +370,14 @@ pub trait CliOptions {
474
370
}
475
371
476
372
// The edition of the compiler (RFC 2052)
477
- configuration_option_enum ! { Edition :
478
- Edition2015 : 2015 ,
479
- Edition2018 : 2018 ,
373
+ #[ config_type]
374
+ pub enum Edition {
375
+ #[ value = "2015" ]
376
+ #[ doc_hint = "2015" ]
377
+ Edition2015 ,
378
+ #[ value = "2018" ]
379
+ #[ doc_hint = "2018" ]
380
+ Edition2018 ,
480
381
}
481
382
482
383
impl Default for Edition {
0 commit comments