@@ -20,14 +20,16 @@ extern crate getopts;
20
20
use rustfmt:: { run, Input } ;
21
21
use rustfmt:: config:: { Config , WriteMode } ;
22
22
23
- use std:: env;
23
+ use std:: { env, error } ;
24
24
use std:: fs:: { self , File } ;
25
25
use std:: io:: { self , ErrorKind , Read , Write } ;
26
26
use std:: path:: { Path , PathBuf } ;
27
27
use std:: str:: FromStr ;
28
28
29
29
use getopts:: { Matches , Options } ;
30
30
31
+ type Error = Box < error:: Error + Send + Sync > ;
32
+ type Result < T > = std:: result:: Result < T , Error > ;
31
33
32
34
/// Rustfmt operations.
33
35
enum Operation {
@@ -42,10 +44,6 @@ enum Operation {
42
44
Version ,
43
45
/// Print detailed configuration help.
44
46
ConfigHelp ,
45
- /// Invalid program input.
46
- InvalidInput {
47
- reason : String ,
48
- } ,
49
47
/// No file specified, read from stdin
50
48
Stdin {
51
49
input : String ,
@@ -55,7 +53,7 @@ enum Operation {
55
53
56
54
/// Try to find a project file in the given directory and its parents. Returns the path of a the
57
55
/// nearest project file if one exists, or `None` if no project file was found.
58
- fn lookup_project_file ( dir : & Path ) -> io :: Result < Option < PathBuf > > {
56
+ fn lookup_project_file ( dir : & Path ) -> Result < Option < PathBuf > > {
59
57
let mut current = if dir. is_relative ( ) {
60
58
try!( env:: current_dir ( ) ) . join ( dir)
61
59
} else {
@@ -77,7 +75,7 @@ fn lookup_project_file(dir: &Path) -> io::Result<Option<PathBuf>> {
77
75
// return the error.
78
76
Err ( e) => {
79
77
if e. kind ( ) != ErrorKind :: NotFound {
80
- return Err ( e ) ;
78
+ return Err ( Error :: from ( e ) ) ;
81
79
}
82
80
}
83
81
}
@@ -93,7 +91,7 @@ fn lookup_project_file(dir: &Path) -> io::Result<Option<PathBuf>> {
93
91
///
94
92
/// Returns the `Config` to use, and the path of the project file if there was
95
93
/// one.
96
- fn resolve_config ( dir : & Path ) -> io :: Result < ( Config , Option < PathBuf > ) > {
94
+ fn resolve_config ( dir : & Path ) -> Result < ( Config , Option < PathBuf > ) > {
97
95
let path = try!( lookup_project_file ( dir) ) ;
98
96
if path. is_none ( ) {
99
97
return Ok ( ( Config :: default ( ) , None ) ) ;
@@ -108,7 +106,7 @@ fn resolve_config(dir: &Path) -> io::Result<(Config, Option<PathBuf>)> {
108
106
/// read the given config file path recursively if present else read the project file path
109
107
fn match_cli_path_or_file ( config_path : Option < PathBuf > ,
110
108
input_file : & Path )
111
- -> io :: Result < ( Config , Option < PathBuf > ) > {
109
+ -> Result < ( Config , Option < PathBuf > ) > {
112
110
113
111
if let Some ( config_file) = config_path {
114
112
let ( toml, path) = try!( resolve_config ( config_file. as_ref ( ) ) ) ;
@@ -119,19 +117,19 @@ fn match_cli_path_or_file(config_path: Option<PathBuf>,
119
117
resolve_config ( input_file)
120
118
}
121
119
122
- fn update_config ( config : & mut Config , matches : & Matches ) -> Result < ( ) , String > {
120
+ fn update_config ( config : & mut Config , matches : & Matches ) -> Result < ( ) > {
123
121
config. verbose = matches. opt_present ( "verbose" ) ;
124
122
config. skip_children = matches. opt_present ( "skip-children" ) ;
125
123
126
- let write_mode = matches. opt_str ( "write-mode" ) ;
127
- match matches. opt_str ( "write-mode" ) . map ( |wm| WriteMode :: from_str ( & wm) ) {
128
- None => Ok ( ( ) ) ,
129
- Some ( Ok ( write_mode) ) => {
124
+ if let Some ( ref write_mode) = matches. opt_str ( "write-mode" ) {
125
+ if let Ok ( write_mode) = WriteMode :: from_str ( write_mode) {
130
126
config. write_mode = write_mode;
131
- Ok ( ( ) )
127
+ } else {
128
+ return Err ( Error :: from ( format ! ( "Invalid write-mode: {}" , write_mode) ) ) ;
132
129
}
133
- Some ( Err ( _) ) => Err ( format ! ( "Invalid write-mode: {}" , write_mode. expect( "cannot happen" ) ) ) ,
134
130
}
131
+
132
+ Ok ( ( ) )
135
133
}
136
134
137
135
fn make_opts ( ) -> Options {
@@ -157,35 +155,18 @@ fn make_opts() -> Options {
157
155
opts
158
156
}
159
157
160
- fn execute ( ) -> i32 {
161
- let opts = make_opts ( ) ;
158
+ fn execute ( opts : & Options ) -> Result < ( ) > {
159
+ let matches = try! ( opts . parse ( env :: args ( ) . skip ( 1 ) ) ) ;
162
160
163
- let matches = match opts. parse ( env:: args ( ) . skip ( 1 ) ) {
164
- Ok ( m) => m,
165
- Err ( e) => {
166
- print_usage ( & opts, & e. to_string ( ) ) ;
167
- return 1 ;
168
- }
169
- } ;
170
-
171
- let operation = determine_operation ( & matches) ;
172
-
173
- match operation {
174
- Operation :: InvalidInput { reason } => {
175
- print_usage ( & opts, & reason) ;
176
- 1
177
- }
161
+ match try!( determine_operation ( & matches) ) {
178
162
Operation :: Help => {
179
163
print_usage ( & opts, "" ) ;
180
- 0
181
164
}
182
165
Operation :: Version => {
183
166
print_version ( ) ;
184
- 0
185
167
}
186
168
Operation :: ConfigHelp => {
187
169
Config :: print_docs ( ) ;
188
- 0
189
170
}
190
171
Operation :: Stdin { input, config_path } => {
191
172
// try to read config from local directory
@@ -196,7 +177,6 @@ fn execute() -> i32 {
196
177
config. write_mode = WriteMode :: Plain ;
197
178
198
179
run ( Input :: Text ( input) , & config) ;
199
- 0
200
180
}
201
181
Operation :: Format { files, config_path } => {
202
182
let mut config = Config :: default ( ) ;
@@ -227,21 +207,26 @@ fn execute() -> i32 {
227
207
config = config_tmp;
228
208
}
229
209
230
- if let Err ( e) = update_config ( & mut config, & matches) {
231
- print_usage ( & opts, & e) ;
232
- return 1 ;
233
- }
210
+ try!( update_config ( & mut config, & matches) ) ;
234
211
run ( Input :: File ( file) , & config) ;
235
212
}
236
- 0
237
213
}
238
214
}
215
+ Ok ( ( ) )
239
216
}
240
217
241
218
fn main ( ) {
242
219
let _ = env_logger:: init ( ) ;
243
- let exit_code = execute ( ) ;
244
220
221
+ let opts = make_opts ( ) ;
222
+
223
+ let exit_code = match execute ( & opts) {
224
+ Ok ( ..) => 0 ,
225
+ Err ( e) => {
226
+ print_usage ( & opts, & e. to_string ( ) ) ;
227
+ 1
228
+ }
229
+ } ;
245
230
// Make sure standard output is flushed before we exit.
246
231
std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
247
232
@@ -267,17 +252,17 @@ fn print_version() {
267
252
option_env!( "CARGO_PKG_VERSION_PRE" ) . unwrap_or( "" ) ) ;
268
253
}
269
254
270
- fn determine_operation ( matches : & Matches ) -> Operation {
255
+ fn determine_operation ( matches : & Matches ) -> Result < Operation > {
271
256
if matches. opt_present ( "h" ) {
272
- return Operation :: Help ;
257
+ return Ok ( Operation :: Help ) ;
273
258
}
274
259
275
260
if matches. opt_present ( "config-help" ) {
276
- return Operation :: ConfigHelp ;
261
+ return Ok ( Operation :: ConfigHelp ) ;
277
262
}
278
263
279
264
if matches. opt_present ( "version" ) {
280
- return Operation :: Version ;
265
+ return Ok ( Operation :: Version ) ;
281
266
}
282
267
283
268
// Read the config_path and convert to parent dir if a file is provided.
@@ -294,21 +279,18 @@ fn determine_operation(matches: &Matches) -> Operation {
294
279
if matches. free . is_empty ( ) {
295
280
296
281
let mut buffer = String :: new ( ) ;
297
- match io:: stdin ( ) . read_to_string ( & mut buffer) {
298
- Ok ( ..) => ( ) ,
299
- Err ( e) => return Operation :: InvalidInput { reason : e. to_string ( ) } ,
300
- }
282
+ try!( io:: stdin ( ) . read_to_string ( & mut buffer) ) ;
301
283
302
- return Operation :: Stdin {
284
+ return Ok ( Operation :: Stdin {
303
285
input : buffer,
304
286
config_path : config_path,
305
- } ;
287
+ } ) ;
306
288
}
307
289
308
290
let files: Vec < _ > = matches. free . iter ( ) . map ( PathBuf :: from) . collect ( ) ;
309
291
310
- Operation :: Format {
292
+ Ok ( Operation :: Format {
311
293
files : files,
312
294
config_path : config_path,
313
- }
295
+ } )
314
296
}
0 commit comments