@@ -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 FmtError = Box < error:: Error + Send + Sync > ;
32
+ type FmtResult < T > = std:: result:: Result < T , FmtError > ;
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 ) -> FmtResult < 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 ( FmtError :: 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 ) -> FmtResult < ( 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
+ -> FmtResult < ( 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,7 +117,7 @@ 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 ) -> FmtResult < ( ) > {
123
121
config. verbose = matches. opt_present ( "verbose" ) ;
124
122
config. skip_children = matches. opt_present ( "skip-children" ) ;
125
123
@@ -130,7 +128,10 @@ fn update_config(config: &mut Config, matches: &Matches) -> Result<(), String> {
130
128
config. write_mode = write_mode;
131
129
Ok ( ( ) )
132
130
}
133
- Some ( Err ( _) ) => Err ( format ! ( "Invalid write-mode: {}" , write_mode. expect( "cannot happen" ) ) ) ,
131
+ Some ( Err ( _) ) => {
132
+ Err ( FmtError :: from ( format ! ( "Invalid write-mode: {}" ,
133
+ write_mode. expect( "cannot happen" ) ) ) )
134
+ }
134
135
}
135
136
}
136
137
@@ -157,35 +158,18 @@ fn make_opts() -> Options {
157
158
opts
158
159
}
159
160
160
- fn execute ( ) -> i32 {
161
- let opts = make_opts ( ) ;
161
+ fn execute ( opts : & Options ) -> FmtResult < ( ) > {
162
+ let matches = try! ( opts . parse ( env :: args ( ) . skip ( 1 ) ) ) ;
162
163
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
- }
164
+ match try!( determine_operation ( & matches) ) {
178
165
Operation :: Help => {
179
166
print_usage ( & opts, "" ) ;
180
- 0
181
167
}
182
168
Operation :: Version => {
183
169
print_version ( ) ;
184
- 0
185
170
}
186
171
Operation :: ConfigHelp => {
187
172
Config :: print_docs ( ) ;
188
- 0
189
173
}
190
174
Operation :: Stdin { input, config_path } => {
191
175
// try to read config from local directory
@@ -196,7 +180,6 @@ fn execute() -> i32 {
196
180
config. write_mode = WriteMode :: Plain ;
197
181
198
182
run ( Input :: Text ( input) , & config) ;
199
- 0
200
183
}
201
184
Operation :: Format { files, config_path } => {
202
185
let mut config = Config :: default ( ) ;
@@ -227,21 +210,26 @@ fn execute() -> i32 {
227
210
config = config_tmp;
228
211
}
229
212
230
- if let Err ( e) = update_config ( & mut config, & matches) {
231
- print_usage ( & opts, & e) ;
232
- return 1 ;
233
- }
213
+ try!( update_config ( & mut config, & matches) ) ;
234
214
run ( Input :: File ( file) , & config) ;
235
215
}
236
- 0
237
216
}
238
217
}
218
+ Ok ( ( ) )
239
219
}
240
220
241
221
fn main ( ) {
242
222
let _ = env_logger:: init ( ) ;
243
- let exit_code = execute ( ) ;
244
223
224
+ let opts = make_opts ( ) ;
225
+
226
+ let exit_code = match execute ( & opts) {
227
+ Ok ( ..) => 0 ,
228
+ Err ( e) => {
229
+ print_usage ( & opts, & e. to_string ( ) ) ;
230
+ 1
231
+ }
232
+ } ;
245
233
// Make sure standard output is flushed before we exit.
246
234
std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
247
235
@@ -267,17 +255,17 @@ fn print_version() {
267
255
option_env!( "CARGO_PKG_VERSION_PRE" ) . unwrap_or( "" ) ) ;
268
256
}
269
257
270
- fn determine_operation ( matches : & Matches ) -> Operation {
258
+ fn determine_operation ( matches : & Matches ) -> FmtResult < Operation > {
271
259
if matches. opt_present ( "h" ) {
272
- return Operation :: Help ;
260
+ return Ok ( Operation :: Help ) ;
273
261
}
274
262
275
263
if matches. opt_present ( "config-help" ) {
276
- return Operation :: ConfigHelp ;
264
+ return Ok ( Operation :: ConfigHelp ) ;
277
265
}
278
266
279
267
if matches. opt_present ( "version" ) {
280
- return Operation :: Version ;
268
+ return Ok ( Operation :: Version ) ;
281
269
}
282
270
283
271
// Read the config_path and convert to parent dir if a file is provided.
@@ -294,21 +282,18 @@ fn determine_operation(matches: &Matches) -> Operation {
294
282
if matches. free . is_empty ( ) {
295
283
296
284
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
- }
285
+ try!( io:: stdin ( ) . read_to_string ( & mut buffer) ) ;
301
286
302
- return Operation :: Stdin {
287
+ return Ok ( Operation :: Stdin {
303
288
input : buffer,
304
289
config_path : config_path,
305
- } ;
290
+ } ) ;
306
291
}
307
292
308
293
let files: Vec < _ > = matches. free . iter ( ) . map ( PathBuf :: from) . collect ( ) ;
309
294
310
- Operation :: Format {
295
+ Ok ( Operation :: Format {
311
296
files : files,
312
297
config_path : config_path,
313
- }
298
+ } )
314
299
}
0 commit comments