Skip to content

Commit 43f178b

Browse files
committed
Remove Summary
1 parent a24df13 commit 43f178b

File tree

5 files changed

+124
-147
lines changed

5 files changed

+124
-147
lines changed

src/bin/main.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,11 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32, failure:
225225
let mut session = Session::new(config, Some(out));
226226
format_and_emit_report(&mut session, Input::Text(input));
227227

228-
let exit_code =
229-
if session.summary.has_operational_errors() || session.summary.has_parsing_errors() {
230-
1
231-
} else {
232-
0
233-
};
228+
let exit_code = if session.has_operational_errors() || session.has_parsing_errors() {
229+
1
230+
} else {
231+
0
232+
};
234233
Ok(exit_code)
235234
}
236235

@@ -254,10 +253,10 @@ fn format(
254253
for file in files {
255254
if !file.exists() {
256255
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
257-
session.summary.add_operational_error();
256+
session.add_operational_error();
258257
} else if file.is_dir() {
259258
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
260-
session.summary.add_operational_error();
259+
session.add_operational_error();
261260
} else {
262261
// Check the file directory if the config-path could not be read or not provided
263262
if config_path.is_none() {
@@ -290,9 +289,9 @@ fn format(
290289
file.write_all(toml.as_bytes())?;
291290
}
292291

293-
let exit_code = if session.summary.has_operational_errors()
294-
|| session.summary.has_parsing_errors()
295-
|| ((session.summary.has_diff || session.summary.has_check_errors()) && options.check)
292+
let exit_code = if session.has_operational_errors()
293+
|| session.has_parsing_errors()
294+
|| ((session.has_diff() || session.has_check_errors()) && options.check)
296295
{
297296
1
298297
} else {
@@ -322,7 +321,7 @@ fn format_and_emit_report<T: Write>(session: &mut Session<T>, input: Input) {
322321
}
323322
Err(msg) => {
324323
eprintln!("Error writing files: {}", msg);
325-
session.summary.add_operational_error();
324+
session.add_operational_error();
326325
}
327326
}
328327
}

src/formatting.rs

Lines changed: 51 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ impl<'b, T: Write + 'b> Session<'b, T> {
4646
let config = &self.config.clone();
4747
let format_result = format_project(input, config, self);
4848

49-
format_result.map(|(report, summary)| {
50-
self.summary.add(summary);
49+
format_result.map(|report| {
50+
{
51+
let new_errors = &report.internal.borrow().1;
52+
53+
self.errors.add(new_errors);
54+
}
5155
report
5256
})
5357
})
@@ -59,8 +63,7 @@ fn format_project<T: FormatHandler>(
5963
input: Input,
6064
config: &Config,
6165
handler: &mut T,
62-
) -> Result<(FormatReport, Summary), ErrorKind> {
63-
let mut summary = Summary::default();
66+
) -> Result<FormatReport, ErrorKind> {
6467
let mut timer = Timer::Initialized(Instant::now());
6568

6669
let main_file = input.file_name();
@@ -69,21 +72,15 @@ fn format_project<T: FormatHandler>(
6972
// Parse the crate.
7073
let codemap = Rc::new(CodeMap::new(FilePathMapping::empty()));
7174
let mut parse_session = make_parse_sess(codemap.clone(), config);
72-
let krate = parse_crate(input, &parse_session, config, &mut summary)?;
75+
let mut report = FormatReport::new();
76+
let krate = parse_crate(input, &parse_session, config, &mut report)?;
7377
timer = timer.done_parsing();
7478

7579
// Suppress error output if we have to do any further parsing.
7680
let silent_emitter = silent_emitter(codemap);
7781
parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);
7882

79-
let mut context = FormatContext::new(
80-
&krate,
81-
FormatReport::new(),
82-
summary,
83-
parse_session,
84-
config,
85-
handler,
86-
);
83+
let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
8784

8885
let files = modules::list_files(&krate, context.parse_session.codemap())?;
8986
for (path, module) in files {
@@ -104,36 +101,20 @@ fn format_project<T: FormatHandler>(
104101
)
105102
});
106103

107-
context.summarise_errors();
108-
Ok((context.report, context.summary))
104+
Ok(context.report)
109105
}
110106

111107
// Used for formatting files.
112108
#[derive(new)]
113109
struct FormatContext<'a, T: FormatHandler + 'a> {
114110
krate: &'a ast::Crate,
115111
report: FormatReport,
116-
summary: Summary,
117112
parse_session: ParseSess,
118113
config: &'a Config,
119114
handler: &'a mut T,
120115
}
121116

122117
impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
123-
// Moves errors from the report to the summary.
124-
fn summarise_errors(&mut self) {
125-
if self.report.has_warnings() {
126-
self.summary.add_formatting_error();
127-
}
128-
let report_errs = &self.report.internal.borrow().1;
129-
if report_errs.has_check_errors {
130-
self.summary.add_check_error();
131-
}
132-
if report_errs.has_operational_errors {
133-
self.summary.add_operational_error();
134-
}
135-
}
136-
137118
// Formats a single file/module.
138119
fn format_file(
139120
&mut self,
@@ -188,16 +169,22 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
188169
replace_with_system_newlines(&mut visitor.buffer, &self.config);
189170

190171
if visitor.macro_rewrite_failure {
191-
self.summary.add_macro_format_failure();
172+
self.report.add_macro_format_failure();
192173
}
193174

194-
self.handler.handle_formatted_file(path, visitor.buffer)
175+
self.handler
176+
.handle_formatted_file(path, visitor.buffer, &mut self.report)
195177
}
196178
}
197179

198180
// Handle the results of formatting.
199181
trait FormatHandler {
200-
fn handle_formatted_file(&mut self, path: FileName, result: String) -> Result<(), ErrorKind>;
182+
fn handle_formatted_file(
183+
&mut self,
184+
path: FileName,
185+
result: String,
186+
report: &mut FormatReport,
187+
) -> Result<(), ErrorKind>;
201188
}
202189

203190
impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
@@ -206,10 +193,11 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
206193
&mut self,
207194
path: FileName,
208195
mut result: String,
196+
report: &mut FormatReport,
209197
) -> Result<(), ErrorKind> {
210198
if let Some(ref mut out) = self.out {
211199
match filemap::write_file(&mut result, &path, out, &self.config) {
212-
Ok(b) if b => self.summary.add_diff(),
200+
Ok(b) if b => report.add_diff(),
213201
Err(e) => {
214202
// Create a new error with path_str to help users see which files failed
215203
let err_msg = format!("{}: {}", path, e);
@@ -298,114 +286,56 @@ pub(crate) type FormatErrorMap = HashMap<FileName, Vec<FormattingError>>;
298286

299287
#[derive(Default, Debug)]
300288
pub(crate) struct ReportedErrors {
301-
pub(crate) has_operational_errors: bool,
302-
pub(crate) has_check_errors: bool,
303-
}
304-
305-
/// A single span of changed lines, with 0 or more removed lines
306-
/// and a vector of 0 or more inserted lines.
307-
#[derive(Debug, PartialEq, Eq)]
308-
pub(crate) struct ModifiedChunk {
309-
/// The first to be removed from the original text
310-
pub line_number_orig: u32,
311-
/// The number of lines which have been replaced
312-
pub lines_removed: u32,
313-
/// The new lines
314-
pub lines: Vec<String>,
315-
}
316-
317-
/// Set of changed sections of a file.
318-
#[derive(Debug, PartialEq, Eq)]
319-
pub(crate) struct ModifiedLines {
320-
/// The set of changed chunks.
321-
pub chunks: Vec<ModifiedChunk>,
322-
}
323-
324-
/// A summary of a Rustfmt run.
325-
#[derive(Debug, Default, Clone, Copy)]
326-
pub struct Summary {
327289
// Encountered e.g. an IO error.
328-
has_operational_errors: bool,
290+
pub(crate) has_operational_errors: bool,
329291

330292
// Failed to reformat code because of parsing errors.
331-
has_parsing_errors: bool,
293+
pub(crate) has_parsing_errors: bool,
332294

333295
// Code is valid, but it is impossible to format it properly.
334-
has_formatting_errors: bool,
296+
pub(crate) has_formatting_errors: bool,
335297

336298
// Code contains macro call that was unable to format.
337299
pub(crate) has_macro_format_failure: bool,
338300

339301
// Failed a check, such as the license check or other opt-in checking.
340-
has_check_errors: bool,
302+
pub(crate) has_check_errors: bool,
341303

342304
/// Formatted code differs from existing code (--check only).
343-
pub has_diff: bool,
305+
pub(crate) has_diff: bool,
344306
}
345307

346-
impl Summary {
347-
pub fn has_operational_errors(&self) -> bool {
348-
self.has_operational_errors
349-
}
350-
351-
pub fn has_parsing_errors(&self) -> bool {
352-
self.has_parsing_errors
353-
}
354-
355-
pub fn has_formatting_errors(&self) -> bool {
356-
self.has_formatting_errors
357-
}
358-
359-
pub fn has_check_errors(&self) -> bool {
360-
self.has_check_errors
361-
}
362-
363-
pub(crate) fn has_macro_formatting_failure(&self) -> bool {
364-
self.has_macro_format_failure
365-
}
366-
367-
pub fn add_operational_error(&mut self) {
368-
self.has_operational_errors = true;
369-
}
370-
371-
pub(crate) fn add_parsing_error(&mut self) {
372-
self.has_parsing_errors = true;
373-
}
374-
375-
pub(crate) fn add_formatting_error(&mut self) {
376-
self.has_formatting_errors = true;
377-
}
378-
379-
pub(crate) fn add_check_error(&mut self) {
380-
self.has_check_errors = true;
381-
}
382-
383-
pub(crate) fn add_diff(&mut self) {
384-
self.has_diff = true;
385-
}
386-
387-
pub(crate) fn add_macro_format_failure(&mut self) {
388-
self.has_macro_format_failure = true;
389-
}
390-
391-
pub fn has_no_errors(&self) -> bool {
392-
!(self.has_operational_errors
393-
|| self.has_parsing_errors
394-
|| self.has_formatting_errors
395-
|| self.has_diff)
396-
}
397-
308+
impl ReportedErrors {
398309
/// Combine two summaries together.
399-
pub fn add(&mut self, other: Summary) {
310+
pub fn add(&mut self, other: &ReportedErrors) {
400311
self.has_operational_errors |= other.has_operational_errors;
312+
self.has_parsing_errors |= other.has_parsing_errors;
401313
self.has_formatting_errors |= other.has_formatting_errors;
402314
self.has_macro_format_failure |= other.has_macro_format_failure;
403-
self.has_parsing_errors |= other.has_parsing_errors;
404315
self.has_check_errors |= other.has_check_errors;
405316
self.has_diff |= other.has_diff;
406317
}
407318
}
408319

320+
/// A single span of changed lines, with 0 or more removed lines
321+
/// and a vector of 0 or more inserted lines.
322+
#[derive(Debug, PartialEq, Eq)]
323+
pub(crate) struct ModifiedChunk {
324+
/// The first to be removed from the original text
325+
pub line_number_orig: u32,
326+
/// The number of lines which have been replaced
327+
pub lines_removed: u32,
328+
/// The new lines
329+
pub lines: Vec<String>,
330+
}
331+
332+
/// Set of changed sections of a file.
333+
#[derive(Debug, PartialEq, Eq)]
334+
pub(crate) struct ModifiedLines {
335+
/// The set of changed chunks.
336+
pub chunks: Vec<ModifiedChunk>,
337+
}
338+
409339
#[derive(Clone, Copy, Debug)]
410340
enum Timer {
411341
Initialized(Instant),
@@ -647,7 +577,7 @@ fn parse_crate(
647577
input: Input,
648578
parse_session: &ParseSess,
649579
config: &Config,
650-
summary: &mut Summary,
580+
report: &mut FormatReport,
651581
) -> Result<ast::Crate, ErrorKind> {
652582
let input_is_stdin = input.is_text();
653583

@@ -685,7 +615,7 @@ fn parse_crate(
685615
}
686616
}
687617

688-
summary.add_parsing_error();
618+
report.add_parsing_error();
689619
Err(ErrorKind::ParseError)
690620
}
691621

src/git-rustfmt/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn fmt_files(files: &[&str]) -> i32 {
7979
if report.has_warnings() {
8080
eprintln!("{}", report);
8181
}
82-
if !session.summary.has_no_errors() {
82+
if !session.has_no_errors() {
8383
exit_code = 1;
8484
}
8585
}

0 commit comments

Comments
 (0)