1
+ use anyhow:: { format_err, Result } ;
1
2
use env_logger;
2
- use failure:: { err_msg, format_err, Error as FailureError , Fail } ;
3
3
use io:: Error as IoError ;
4
+ use thiserror:: Error ;
4
5
5
6
use rustfmt_nightly as rustfmt;
6
7
@@ -59,27 +60,27 @@ enum Operation {
59
60
}
60
61
61
62
/// Rustfmt operations errors.
62
- #[ derive( Fail , Debug ) ]
63
+ #[ derive( Error , Debug ) ]
63
64
pub enum OperationError {
64
65
/// An unknown help topic was requested.
65
- #[ fail ( display = "Unknown help topic: `{}`." , _0 ) ]
66
+ #[ error ( "Unknown help topic: `{0 }`." ) ]
66
67
UnknownHelpTopic ( String ) ,
67
68
/// An unknown print-config option was requested.
68
- #[ fail ( display = "Unknown print-config option: `{}`." , _0 ) ]
69
+ #[ error ( "Unknown print-config option: `{0 }`." ) ]
69
70
UnknownPrintConfigTopic ( String ) ,
70
71
/// Attempt to generate a minimal config from standard input.
71
- #[ fail ( display = "The `--print-config=minimal` option doesn't work with standard input." ) ]
72
+ #[ error ( "The `--print-config=minimal` option doesn't work with standard input." ) ]
72
73
MinimalPathWithStdin ,
73
74
/// An io error during reading or writing.
74
- #[ fail ( display = "io error: {}" , _0 ) ]
75
+ #[ error ( "io error: {0}" ) ]
75
76
IoError ( IoError ) ,
76
77
/// Attempt to use --check with stdin, which isn't currently
77
78
/// supported.
78
- #[ fail ( display = "The `--check` option is not supported with standard input." ) ]
79
+ #[ error ( "The `--check` option is not supported with standard input." ) ]
79
80
CheckWithStdin ,
80
81
/// Attempt to use --emit=json with stdin, which isn't currently
81
82
/// supported.
82
- #[ fail ( display = "Using `--emit` other than stdout is not supported with standard input." ) ]
83
+ #[ error ( "Using `--emit` other than stdout is not supported with standard input." ) ]
83
84
EmitWithStdin ,
84
85
}
85
86
@@ -192,7 +193,7 @@ fn is_nightly() -> bool {
192
193
}
193
194
194
195
// Returned i32 is an exit code
195
- fn execute ( opts : & Options ) -> Result < i32 , FailureError > {
196
+ fn execute ( opts : & Options ) -> Result < i32 > {
196
197
let matches = opts. parse ( env:: args ( ) . skip ( 1 ) ) ?;
197
198
let options = GetOptsOptions :: from_matches ( & matches) ?;
198
199
@@ -214,7 +215,7 @@ fn execute(opts: &Options) -> Result<i32, FailureError> {
214
215
Ok ( 0 )
215
216
}
216
217
Operation :: ConfigOutputDefault { path } => {
217
- let toml = Config :: default ( ) . all_options ( ) . to_toml ( ) . map_err ( err_msg ) ?;
218
+ let toml = Config :: default ( ) . all_options ( ) . to_toml ( ) ?;
218
219
if let Some ( path) = path {
219
220
let mut file = File :: create ( path) ?;
220
221
file. write_all ( toml. as_bytes ( ) ) ?;
@@ -233,7 +234,7 @@ fn execute(opts: &Options) -> Result<i32, FailureError> {
233
234
let file = file. canonicalize ( ) . unwrap_or ( file) ;
234
235
235
236
let ( config, _) = load_config ( Some ( file. parent ( ) . unwrap ( ) ) , Some ( options. clone ( ) ) ) ?;
236
- let toml = config. all_options ( ) . to_toml ( ) . map_err ( err_msg ) ?;
237
+ let toml = config. all_options ( ) . to_toml ( ) ?;
237
238
io:: stdout ( ) . write_all ( toml. as_bytes ( ) ) ?;
238
239
239
240
Ok ( 0 )
@@ -246,7 +247,7 @@ fn execute(opts: &Options) -> Result<i32, FailureError> {
246
247
}
247
248
}
248
249
249
- fn format_string ( input : String , options : GetOptsOptions ) -> Result < i32 , FailureError > {
250
+ fn format_string ( input : String , options : GetOptsOptions ) -> Result < i32 > {
250
251
// try to read config from local directory
251
252
let ( mut config, _) = load_config ( Some ( Path :: new ( "." ) ) , Some ( options. clone ( ) ) ) ?;
252
253
@@ -287,7 +288,7 @@ fn format(
287
288
files : Vec < PathBuf > ,
288
289
minimal_config_path : Option < String > ,
289
290
options : & GetOptsOptions ,
290
- ) -> Result < i32 , FailureError > {
291
+ ) -> Result < i32 > {
291
292
options. verify_file_lines ( & files) ;
292
293
let ( config, config_path) = load_config ( None , Some ( options. clone ( ) ) ) ?;
293
294
@@ -335,7 +336,7 @@ fn format(
335
336
// that were used during formatting as TOML.
336
337
if let Some ( path) = minimal_config_path {
337
338
let mut file = File :: create ( path) ?;
338
- let toml = session. config . used_options ( ) . to_toml ( ) . map_err ( err_msg ) ?;
339
+ let toml = session. config . used_options ( ) . to_toml ( ) ?;
339
340
file. write_all ( toml. as_bytes ( ) ) ?;
340
341
}
341
342
@@ -514,7 +515,7 @@ struct GetOptsOptions {
514
515
}
515
516
516
517
impl GetOptsOptions {
517
- pub fn from_matches ( matches : & Matches ) -> Result < GetOptsOptions , FailureError > {
518
+ pub fn from_matches ( matches : & Matches ) -> Result < GetOptsOptions > {
518
519
let mut options = GetOptsOptions :: default ( ) ;
519
520
options. verbose = matches. opt_present ( "verbose" ) ;
520
521
options. quiet = matches. opt_present ( "quiet" ) ;
@@ -535,7 +536,7 @@ impl GetOptsOptions {
535
536
options. error_on_unformatted = Some ( true ) ;
536
537
}
537
538
if let Some ( ref file_lines) = matches. opt_str ( "file-lines" ) {
538
- options. file_lines = file_lines. parse ( ) . map_err ( err_msg ) ?;
539
+ options. file_lines = file_lines. parse ( ) ?;
539
540
}
540
541
} else {
541
542
let mut unstable_options = vec ! [ ] ;
@@ -684,15 +685,15 @@ impl CliOptions for GetOptsOptions {
684
685
}
685
686
}
686
687
687
- fn edition_from_edition_str ( edition_str : & str ) -> Result < Edition , FailureError > {
688
+ fn edition_from_edition_str ( edition_str : & str ) -> Result < Edition > {
688
689
match edition_str {
689
690
"2015" => Ok ( Edition :: Edition2015 ) ,
690
691
"2018" => Ok ( Edition :: Edition2018 ) ,
691
692
_ => Err ( format_err ! ( "Invalid value for `--edition`" ) ) ,
692
693
}
693
694
}
694
695
695
- fn emit_mode_from_emit_str ( emit_str : & str ) -> Result < EmitMode , FailureError > {
696
+ fn emit_mode_from_emit_str ( emit_str : & str ) -> Result < EmitMode > {
696
697
match emit_str {
697
698
"files" => Ok ( EmitMode :: Files ) ,
698
699
"stdout" => Ok ( EmitMode :: Stdout ) ,
0 commit comments