@@ -15,7 +15,7 @@ use thiserror::Error;
1515
1616use rustfmt_lib:: {
1717 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 ,
1919 FormatReportFormatterBuilder , Input , OperationSetting ,
2020} ;
2121
@@ -418,10 +418,8 @@ fn format_string(input: String, opt: Opt) -> Result<i32> {
418418 recursive : opt. recursive ,
419419 verbosity : Verbosity :: Quiet ,
420420 } ;
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 ) ) ?;
425423 Ok ( if opt. check && has_diff { 1 } else { 0 } )
426424}
427425
@@ -485,7 +483,7 @@ fn format(opt: Opt) -> Result<i32> {
485483 return Err ( format_err ! ( "Error: `{}` is a directory" , dir. display( ) ) ) ;
486484 }
487485
488- let ( config , config_path) = load_config ( None , Some ( & opt) ) ?;
486+ let ( default_config , config_path) = load_config ( None , Some ( & opt) ) ?;
489487
490488 if opt. verbose {
491489 if let Some ( path) = config_path. as_ref ( ) {
@@ -497,37 +495,21 @@ fn format(opt: Opt) -> Result<i32> {
497495 recursive : opt. recursive ,
498496 verbosity : opt. verbosity ( ) ,
499497 } ;
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- }
515498
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+ ) ?;
531513
532514 if format_report. has_errors ( ) {
533515 eprintln ! (
@@ -538,28 +520,11 @@ fn format(opt: Opt) -> Result<i32> {
538520 ) ;
539521 }
540522
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 ) ) ?;
543524
544525 Ok ( if opt. check && has_diff { 1 } else { 0 } )
545526}
546527
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-
563528#[ cfg( test) ]
564529mod test {
565530 use super :: * ;
0 commit comments