@@ -15,7 +15,7 @@ use thiserror::Error;
15
15
16
16
use rustfmt_lib:: {
17
17
emitter:: { emit_format_report, EmitMode , EmitterConfig , Verbosity } ,
18
- load_config, CliOptions , Config , Edition , FileLines , FileName , FormatReport ,
18
+ format_inputs , load_config, CliOptions , Config , Edition , FileLines , FileName ,
19
19
FormatReportFormatterBuilder , Input , OperationSetting ,
20
20
} ;
21
21
@@ -418,10 +418,8 @@ fn format_string(input: String, opt: Opt) -> Result<i32> {
418
418
recursive : opt. recursive ,
419
419
verbosity : Verbosity :: Quiet ,
420
420
} ;
421
- let mut format_report = FormatReport :: new ( ) ;
422
- // FIXME: Add error handling.
423
- format_and_emit_report ( Input :: Text ( input) , & config, setting, & mut format_report) ;
424
- let has_diff = emit_format_report ( format_report, out, opt. emitter_config ( EmitMode :: Stdout ) ) ?;
421
+ let report = rustfmt_lib:: format ( Input :: Text ( input) , & config, setting) ?;
422
+ let has_diff = emit_format_report ( report, out, opt. emitter_config ( EmitMode :: Stdout ) ) ?;
425
423
Ok ( if opt. check && has_diff { 1 } else { 0 } )
426
424
}
427
425
@@ -485,7 +483,7 @@ fn format(opt: Opt) -> Result<i32> {
485
483
return Err ( format_err ! ( "Error: `{}` is a directory" , dir. display( ) ) ) ;
486
484
}
487
485
488
- let ( config , config_path) = load_config ( None , Some ( & opt) ) ?;
486
+ let ( default_config , config_path) = load_config ( None , Some ( & opt) ) ?;
489
487
490
488
if opt. verbose {
491
489
if let Some ( path) = config_path. as_ref ( ) {
@@ -497,37 +495,21 @@ fn format(opt: Opt) -> Result<i32> {
497
495
recursive : opt. recursive ,
498
496
verbosity : opt. verbosity ( ) ,
499
497
} ;
500
- let mut format_report = FormatReport :: new ( ) ;
501
-
502
- for pair in FileConfigPairIter :: new ( & opt, config_path. is_some ( ) ) {
503
- let file = pair. file ;
504
-
505
- if let FileConfig :: Local ( local_config, config_path) = pair. config {
506
- if let Some ( path) = config_path {
507
- if opt. verbose {
508
- println ! (
509
- "Using rustfmt config file {} for {}" ,
510
- path. display( ) ,
511
- file. display( )
512
- ) ;
513
- }
514
- }
515
498
516
- format_and_emit_report (
517
- Input :: File ( file. to_path_buf ( ) ) ,
518
- & local_config,
519
- setting,
520
- & mut format_report,
521
- ) ;
522
- } else {
523
- format_and_emit_report (
524
- Input :: File ( file. to_path_buf ( ) ) ,
525
- & config,
526
- setting,
527
- & mut format_report,
528
- ) ;
529
- }
530
- }
499
+ let inputs = FileConfigPairIter :: new ( & opt, config_path. is_some ( ) ) . collect :: < Vec < _ > > ( ) ;
500
+ let format_report = format_inputs (
501
+ inputs. iter ( ) . map ( |p| {
502
+ (
503
+ Input :: File ( p. file . to_path_buf ( ) ) ,
504
+ if let FileConfig :: Local ( ref config, _) = p. config {
505
+ config
506
+ } else {
507
+ & default_config
508
+ } ,
509
+ )
510
+ } ) ,
511
+ setting,
512
+ ) ?;
531
513
532
514
if format_report. has_errors ( ) {
533
515
eprintln ! (
@@ -538,28 +520,11 @@ fn format(opt: Opt) -> Result<i32> {
538
520
) ;
539
521
}
540
522
541
- let out = & mut stdout ( ) ;
542
- let has_diff = emit_format_report ( format_report, out, opt. emitter_config ( EmitMode :: Files ) ) ?;
523
+ let has_diff = emit_format_report ( format_report, & mut stdout ( ) , opt. emitter_config ( EmitMode :: Files ) ) ?;
543
524
544
525
Ok ( if opt. check && has_diff { 1 } else { 0 } )
545
526
}
546
527
547
- fn format_and_emit_report (
548
- input : Input ,
549
- config : & Config ,
550
- operation_setting : OperationSetting ,
551
- format_report : & mut FormatReport ,
552
- ) {
553
- match rustfmt_lib:: format ( input, config, operation_setting) {
554
- Ok ( report) => {
555
- format_report. merge ( report) ;
556
- }
557
- Err ( err) => {
558
- eprintln ! ( "{}" , err) ;
559
- }
560
- }
561
- }
562
-
563
528
#[ cfg( test) ]
564
529
mod test {
565
530
use super :: * ;
0 commit comments