Skip to content

Commit c4a5e83

Browse files
committed
Add format_inputs
1 parent ee4735e commit c4a5e83

File tree

3 files changed

+33
-56
lines changed

3 files changed

+33
-56
lines changed

rustfmt-core/rustfmt-bin/src/bin/main.rs

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use thiserror::Error;
1515

1616
use 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)]
564529
mod test {
565530
use super::*;

rustfmt-core/rustfmt-lib/src/formatting/report.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ impl FormatReport {
100100
Rc::clone(&self.format_result)
101101
}
102102

103-
/// FIXME(topecongiro): reduce visibility.
104-
pub fn merge(&mut self, other: Self) {
103+
pub(crate) fn merge(&mut self, other: Self) {
105104
self.format_result
106105
.borrow_mut()
107106
.append(&mut other.format_result.borrow_mut());

rustfmt-core/rustfmt-lib/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod format_report_formatter;
3333
mod formatting;
3434
mod release_channel;
3535
pub mod result;
36+
3637
#[cfg(test)]
3738
mod test;
3839

@@ -54,6 +55,18 @@ pub fn format(
5455
format_input_inner(input, config, operation_setting)
5556
}
5657

58+
pub fn format_inputs<'a>(
59+
inputs: impl Iterator<Item = (Input, &'a Config)>,
60+
operation_setting: OperationSetting,
61+
) -> Result<FormatReport, OperationError> {
62+
let mut format_report = FormatReport::new();
63+
for (input, config) in inputs {
64+
let report = format(input, config, operation_setting)?;
65+
format_report.merge(report);
66+
}
67+
Ok(format_report)
68+
}
69+
5770
/// The input to rustfmt.
5871
#[derive(Debug)]
5972
pub enum Input {

0 commit comments

Comments
 (0)