Remove Summary
This commit is contained in:
parent
a24df1397e
commit
43f178bd58
5 changed files with 124 additions and 147 deletions
|
@ -225,8 +225,7 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32, failure:
|
|||
let mut session = Session::new(config, Some(out));
|
||||
format_and_emit_report(&mut session, Input::Text(input));
|
||||
|
||||
let exit_code =
|
||||
if session.summary.has_operational_errors() || session.summary.has_parsing_errors() {
|
||||
let exit_code = if session.has_operational_errors() || session.has_parsing_errors() {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
|
@ -254,10 +253,10 @@ fn format(
|
|||
for file in files {
|
||||
if !file.exists() {
|
||||
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
|
||||
session.summary.add_operational_error();
|
||||
session.add_operational_error();
|
||||
} else if file.is_dir() {
|
||||
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
|
||||
session.summary.add_operational_error();
|
||||
session.add_operational_error();
|
||||
} else {
|
||||
// Check the file directory if the config-path could not be read or not provided
|
||||
if config_path.is_none() {
|
||||
|
@ -290,9 +289,9 @@ fn format(
|
|||
file.write_all(toml.as_bytes())?;
|
||||
}
|
||||
|
||||
let exit_code = if session.summary.has_operational_errors()
|
||||
|| session.summary.has_parsing_errors()
|
||||
|| ((session.summary.has_diff || session.summary.has_check_errors()) && options.check)
|
||||
let exit_code = if session.has_operational_errors()
|
||||
|| session.has_parsing_errors()
|
||||
|| ((session.has_diff() || session.has_check_errors()) && options.check)
|
||||
{
|
||||
1
|
||||
} else {
|
||||
|
@ -322,7 +321,7 @@ fn format_and_emit_report<T: Write>(session: &mut Session<T>, input: Input) {
|
|||
}
|
||||
Err(msg) => {
|
||||
eprintln!("Error writing files: {}", msg);
|
||||
session.summary.add_operational_error();
|
||||
session.add_operational_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,8 +46,12 @@ impl<'b, T: Write + 'b> Session<'b, T> {
|
|||
let config = &self.config.clone();
|
||||
let format_result = format_project(input, config, self);
|
||||
|
||||
format_result.map(|(report, summary)| {
|
||||
self.summary.add(summary);
|
||||
format_result.map(|report| {
|
||||
{
|
||||
let new_errors = &report.internal.borrow().1;
|
||||
|
||||
self.errors.add(new_errors);
|
||||
}
|
||||
report
|
||||
})
|
||||
})
|
||||
|
@ -59,8 +63,7 @@ fn format_project<T: FormatHandler>(
|
|||
input: Input,
|
||||
config: &Config,
|
||||
handler: &mut T,
|
||||
) -> Result<(FormatReport, Summary), ErrorKind> {
|
||||
let mut summary = Summary::default();
|
||||
) -> Result<FormatReport, ErrorKind> {
|
||||
let mut timer = Timer::Initialized(Instant::now());
|
||||
|
||||
let main_file = input.file_name();
|
||||
|
@ -69,21 +72,15 @@ fn format_project<T: FormatHandler>(
|
|||
// Parse the crate.
|
||||
let codemap = Rc::new(CodeMap::new(FilePathMapping::empty()));
|
||||
let mut parse_session = make_parse_sess(codemap.clone(), config);
|
||||
let krate = parse_crate(input, &parse_session, config, &mut summary)?;
|
||||
let mut report = FormatReport::new();
|
||||
let krate = parse_crate(input, &parse_session, config, &mut report)?;
|
||||
timer = timer.done_parsing();
|
||||
|
||||
// Suppress error output if we have to do any further parsing.
|
||||
let silent_emitter = silent_emitter(codemap);
|
||||
parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);
|
||||
|
||||
let mut context = FormatContext::new(
|
||||
&krate,
|
||||
FormatReport::new(),
|
||||
summary,
|
||||
parse_session,
|
||||
config,
|
||||
handler,
|
||||
);
|
||||
let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
|
||||
|
||||
let files = modules::list_files(&krate, context.parse_session.codemap())?;
|
||||
for (path, module) in files {
|
||||
|
@ -104,8 +101,7 @@ fn format_project<T: FormatHandler>(
|
|||
)
|
||||
});
|
||||
|
||||
context.summarise_errors();
|
||||
Ok((context.report, context.summary))
|
||||
Ok(context.report)
|
||||
}
|
||||
|
||||
// Used for formatting files.
|
||||
|
@ -113,27 +109,12 @@ fn format_project<T: FormatHandler>(
|
|||
struct FormatContext<'a, T: FormatHandler + 'a> {
|
||||
krate: &'a ast::Crate,
|
||||
report: FormatReport,
|
||||
summary: Summary,
|
||||
parse_session: ParseSess,
|
||||
config: &'a Config,
|
||||
handler: &'a mut T,
|
||||
}
|
||||
|
||||
impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
|
||||
// Moves errors from the report to the summary.
|
||||
fn summarise_errors(&mut self) {
|
||||
if self.report.has_warnings() {
|
||||
self.summary.add_formatting_error();
|
||||
}
|
||||
let report_errs = &self.report.internal.borrow().1;
|
||||
if report_errs.has_check_errors {
|
||||
self.summary.add_check_error();
|
||||
}
|
||||
if report_errs.has_operational_errors {
|
||||
self.summary.add_operational_error();
|
||||
}
|
||||
}
|
||||
|
||||
// Formats a single file/module.
|
||||
fn format_file(
|
||||
&mut self,
|
||||
|
@ -188,16 +169,22 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
|
|||
replace_with_system_newlines(&mut visitor.buffer, &self.config);
|
||||
|
||||
if visitor.macro_rewrite_failure {
|
||||
self.summary.add_macro_format_failure();
|
||||
self.report.add_macro_format_failure();
|
||||
}
|
||||
|
||||
self.handler.handle_formatted_file(path, visitor.buffer)
|
||||
self.handler
|
||||
.handle_formatted_file(path, visitor.buffer, &mut self.report)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the results of formatting.
|
||||
trait FormatHandler {
|
||||
fn handle_formatted_file(&mut self, path: FileName, result: String) -> Result<(), ErrorKind>;
|
||||
fn handle_formatted_file(
|
||||
&mut self,
|
||||
path: FileName,
|
||||
result: String,
|
||||
report: &mut FormatReport,
|
||||
) -> Result<(), ErrorKind>;
|
||||
}
|
||||
|
||||
impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
|
||||
|
@ -206,10 +193,11 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
|
|||
&mut self,
|
||||
path: FileName,
|
||||
mut result: String,
|
||||
report: &mut FormatReport,
|
||||
) -> Result<(), ErrorKind> {
|
||||
if let Some(ref mut out) = self.out {
|
||||
match filemap::write_file(&mut result, &path, out, &self.config) {
|
||||
Ok(b) if b => self.summary.add_diff(),
|
||||
Ok(b) if b => report.add_diff(),
|
||||
Err(e) => {
|
||||
// Create a new error with path_str to help users see which files failed
|
||||
let err_msg = format!("{}: {}", path, e);
|
||||
|
@ -298,8 +286,35 @@ pub(crate) type FormatErrorMap = HashMap<FileName, Vec<FormattingError>>;
|
|||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct ReportedErrors {
|
||||
// Encountered e.g. an IO error.
|
||||
pub(crate) has_operational_errors: bool,
|
||||
|
||||
// Failed to reformat code because of parsing errors.
|
||||
pub(crate) has_parsing_errors: bool,
|
||||
|
||||
// Code is valid, but it is impossible to format it properly.
|
||||
pub(crate) has_formatting_errors: bool,
|
||||
|
||||
// Code contains macro call that was unable to format.
|
||||
pub(crate) has_macro_format_failure: bool,
|
||||
|
||||
// Failed a check, such as the license check or other opt-in checking.
|
||||
pub(crate) has_check_errors: bool,
|
||||
|
||||
/// Formatted code differs from existing code (--check only).
|
||||
pub(crate) has_diff: bool,
|
||||
}
|
||||
|
||||
impl ReportedErrors {
|
||||
/// Combine two summaries together.
|
||||
pub fn add(&mut self, other: &ReportedErrors) {
|
||||
self.has_operational_errors |= other.has_operational_errors;
|
||||
self.has_parsing_errors |= other.has_parsing_errors;
|
||||
self.has_formatting_errors |= other.has_formatting_errors;
|
||||
self.has_macro_format_failure |= other.has_macro_format_failure;
|
||||
self.has_check_errors |= other.has_check_errors;
|
||||
self.has_diff |= other.has_diff;
|
||||
}
|
||||
}
|
||||
|
||||
/// A single span of changed lines, with 0 or more removed lines
|
||||
|
@ -321,91 +336,6 @@ pub(crate) struct ModifiedLines {
|
|||
pub chunks: Vec<ModifiedChunk>,
|
||||
}
|
||||
|
||||
/// A summary of a Rustfmt run.
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct Summary {
|
||||
// Encountered e.g. an IO error.
|
||||
has_operational_errors: bool,
|
||||
|
||||
// Failed to reformat code because of parsing errors.
|
||||
has_parsing_errors: bool,
|
||||
|
||||
// Code is valid, but it is impossible to format it properly.
|
||||
has_formatting_errors: bool,
|
||||
|
||||
// Code contains macro call that was unable to format.
|
||||
pub(crate) has_macro_format_failure: bool,
|
||||
|
||||
// Failed a check, such as the license check or other opt-in checking.
|
||||
has_check_errors: bool,
|
||||
|
||||
/// Formatted code differs from existing code (--check only).
|
||||
pub has_diff: bool,
|
||||
}
|
||||
|
||||
impl Summary {
|
||||
pub fn has_operational_errors(&self) -> bool {
|
||||
self.has_operational_errors
|
||||
}
|
||||
|
||||
pub fn has_parsing_errors(&self) -> bool {
|
||||
self.has_parsing_errors
|
||||
}
|
||||
|
||||
pub fn has_formatting_errors(&self) -> bool {
|
||||
self.has_formatting_errors
|
||||
}
|
||||
|
||||
pub fn has_check_errors(&self) -> bool {
|
||||
self.has_check_errors
|
||||
}
|
||||
|
||||
pub(crate) fn has_macro_formatting_failure(&self) -> bool {
|
||||
self.has_macro_format_failure
|
||||
}
|
||||
|
||||
pub fn add_operational_error(&mut self) {
|
||||
self.has_operational_errors = true;
|
||||
}
|
||||
|
||||
pub(crate) fn add_parsing_error(&mut self) {
|
||||
self.has_parsing_errors = true;
|
||||
}
|
||||
|
||||
pub(crate) fn add_formatting_error(&mut self) {
|
||||
self.has_formatting_errors = true;
|
||||
}
|
||||
|
||||
pub(crate) fn add_check_error(&mut self) {
|
||||
self.has_check_errors = true;
|
||||
}
|
||||
|
||||
pub(crate) fn add_diff(&mut self) {
|
||||
self.has_diff = true;
|
||||
}
|
||||
|
||||
pub(crate) fn add_macro_format_failure(&mut self) {
|
||||
self.has_macro_format_failure = true;
|
||||
}
|
||||
|
||||
pub fn has_no_errors(&self) -> bool {
|
||||
!(self.has_operational_errors
|
||||
|| self.has_parsing_errors
|
||||
|| self.has_formatting_errors
|
||||
|| self.has_diff)
|
||||
}
|
||||
|
||||
/// Combine two summaries together.
|
||||
pub fn add(&mut self, other: Summary) {
|
||||
self.has_operational_errors |= other.has_operational_errors;
|
||||
self.has_formatting_errors |= other.has_formatting_errors;
|
||||
self.has_macro_format_failure |= other.has_macro_format_failure;
|
||||
self.has_parsing_errors |= other.has_parsing_errors;
|
||||
self.has_check_errors |= other.has_check_errors;
|
||||
self.has_diff |= other.has_diff;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Timer {
|
||||
Initialized(Instant),
|
||||
|
@ -647,7 +577,7 @@ fn parse_crate(
|
|||
input: Input,
|
||||
parse_session: &ParseSess,
|
||||
config: &Config,
|
||||
summary: &mut Summary,
|
||||
report: &mut FormatReport,
|
||||
) -> Result<ast::Crate, ErrorKind> {
|
||||
let input_is_stdin = input.is_text();
|
||||
|
||||
|
@ -685,7 +615,7 @@ fn parse_crate(
|
|||
}
|
||||
}
|
||||
|
||||
summary.add_parsing_error();
|
||||
report.add_parsing_error();
|
||||
Err(ErrorKind::ParseError)
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,7 @@ fn fmt_files(files: &[&str]) -> i32 {
|
|||
if report.has_warnings() {
|
||||
eprintln!("{}", report);
|
||||
}
|
||||
if !session.summary.has_no_errors() {
|
||||
if !session.has_no_errors() {
|
||||
exit_code = 1;
|
||||
}
|
||||
}
|
||||
|
|
58
src/lib.rs
58
src/lib.rs
|
@ -49,7 +49,7 @@ use syntax::ast;
|
|||
|
||||
use comment::LineClasses;
|
||||
use failure::Fail;
|
||||
use formatting::{FileMap, FormatErrorMap, FormattingError, ReportedErrors, Summary};
|
||||
use formatting::{FileMap, FormatErrorMap, FormattingError, ReportedErrors};
|
||||
use issues::Issue;
|
||||
use shape::Indent;
|
||||
|
||||
|
@ -179,6 +179,9 @@ impl FormatReport {
|
|||
|
||||
fn track_errors(&self, new_errors: &[FormattingError]) {
|
||||
let errs = &mut self.internal.borrow_mut().1;
|
||||
if !new_errors.is_empty() {
|
||||
errs.has_formatting_errors = true;
|
||||
}
|
||||
if errs.has_operational_errors && errs.has_check_errors {
|
||||
return;
|
||||
}
|
||||
|
@ -199,6 +202,18 @@ impl FormatReport {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_diff(&mut self) {
|
||||
self.internal.borrow_mut().1.has_diff = true;
|
||||
}
|
||||
|
||||
fn add_macro_format_failure(&mut self) {
|
||||
self.internal.borrow_mut().1.has_macro_format_failure = true;
|
||||
}
|
||||
|
||||
fn add_parsing_error(&mut self) {
|
||||
self.internal.borrow_mut().1.has_parsing_errors = true;
|
||||
}
|
||||
|
||||
fn warning_count(&self) -> usize {
|
||||
self.internal
|
||||
.borrow()
|
||||
|
@ -210,7 +225,7 @@ impl FormatReport {
|
|||
|
||||
/// Whether any warnings or errors are present in the report.
|
||||
pub fn has_warnings(&self) -> bool {
|
||||
self.warning_count() > 0
|
||||
self.internal.borrow().1.has_formatting_errors
|
||||
}
|
||||
|
||||
/// Print the report to a terminal using colours and potentially other
|
||||
|
@ -351,7 +366,7 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<String> {
|
|||
{
|
||||
let mut session = Session::new(config, Some(&mut out));
|
||||
let result = session.format(input);
|
||||
let formatting_error = session.summary.has_macro_formatting_failure()
|
||||
let formatting_error = session.errors.has_macro_format_failure
|
||||
|| session.out.as_ref().unwrap().is_empty() && !snippet.is_empty();
|
||||
if formatting_error || result.is_err() {
|
||||
return None;
|
||||
|
@ -443,7 +458,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<String> {
|
|||
pub struct Session<'b, T: Write + 'b> {
|
||||
pub config: Config,
|
||||
pub out: Option<&'b mut T>,
|
||||
pub summary: Summary,
|
||||
pub(crate) errors: ReportedErrors,
|
||||
filemap: FileMap,
|
||||
}
|
||||
|
||||
|
@ -456,7 +471,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
|
|||
Session {
|
||||
config,
|
||||
out,
|
||||
summary: Summary::default(),
|
||||
errors: ReportedErrors::default(),
|
||||
filemap: FileMap::new(),
|
||||
}
|
||||
}
|
||||
|
@ -476,6 +491,39 @@ impl<'b, T: Write + 'b> Session<'b, T> {
|
|||
mem::swap(&mut config, &mut self.config);
|
||||
result
|
||||
}
|
||||
|
||||
pub fn add_operational_error(&mut self) {
|
||||
self.errors.has_operational_errors = true;
|
||||
}
|
||||
|
||||
pub fn has_operational_errors(&self) -> bool {
|
||||
self.errors.has_operational_errors
|
||||
}
|
||||
|
||||
pub fn has_parsing_errors(&self) -> bool {
|
||||
self.errors.has_parsing_errors
|
||||
}
|
||||
|
||||
pub fn has_formatting_errors(&self) -> bool {
|
||||
self.errors.has_formatting_errors
|
||||
}
|
||||
|
||||
pub fn has_check_errors(&self) -> bool {
|
||||
self.errors.has_check_errors
|
||||
}
|
||||
|
||||
pub fn has_diff(&self) -> bool {
|
||||
self.errors.has_diff
|
||||
}
|
||||
|
||||
pub fn has_no_errors(&self) -> bool {
|
||||
!(self.has_operational_errors()
|
||||
|| self.has_parsing_errors()
|
||||
|| self.has_formatting_errors()
|
||||
|| self.has_check_errors()
|
||||
|| self.has_diff())
|
||||
|| self.errors.has_macro_format_failure
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T: Write + 'b> Drop for Session<'b, T> {
|
||||
|
|
|
@ -21,7 +21,7 @@ use std::str::Chars;
|
|||
|
||||
use config::{Color, Config, EmitMode, FileName, ReportTactic};
|
||||
use filemap;
|
||||
use formatting::{FileMap, ModifiedChunk, Summary};
|
||||
use formatting::{FileMap, ModifiedChunk};
|
||||
use rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, OutputWriter};
|
||||
use {FormatReport, Input, Session};
|
||||
|
||||
|
@ -278,7 +278,7 @@ fn stdin_formatting_smoke_test() {
|
|||
{
|
||||
let mut session = Session::new(config, Some(&mut buf));
|
||||
session.format(input).unwrap();
|
||||
assert!(session.summary.has_no_errors());
|
||||
assert!(session.has_no_errors());
|
||||
}
|
||||
//eprintln!("{:?}", );
|
||||
#[cfg(not(windows))]
|
||||
|
@ -318,7 +318,7 @@ fn format_lines_errors_are_reported() {
|
|||
config.set().error_on_line_overflow(true);
|
||||
let mut session = Session::<io::Stdout>::new(config, None);
|
||||
session.format(input).unwrap();
|
||||
assert!(session.summary.has_formatting_errors());
|
||||
assert!(session.has_formatting_errors());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -330,7 +330,7 @@ fn format_lines_errors_are_reported_with_tabs() {
|
|||
config.set().hard_tabs(true);
|
||||
let mut session = Session::<io::Stdout>::new(config, None);
|
||||
session.format(input).unwrap();
|
||||
assert!(session.summary.has_formatting_errors());
|
||||
assert!(session.has_formatting_errors());
|
||||
}
|
||||
|
||||
// For each file, run rustfmt and collect the output.
|
||||
|
@ -419,7 +419,7 @@ fn format_file<P: Into<PathBuf>>(filepath: P, config: Config) -> (bool, FileMap,
|
|||
let input = Input::File(filepath);
|
||||
let mut session = Session::<io::Stdout>::new(config, None);
|
||||
let result = session.format(input).unwrap();
|
||||
let parsing_errors = session.summary.has_parsing_errors();
|
||||
let parsing_errors = session.has_parsing_errors();
|
||||
let mut filemap = FileMap::new();
|
||||
mem::swap(&mut session.filemap, &mut filemap);
|
||||
(parsing_errors, filemap, result)
|
||||
|
@ -767,8 +767,8 @@ impl ConfigCodeBlock {
|
|||
true
|
||||
}
|
||||
|
||||
fn has_parsing_errors(&self, error_summary: Summary) -> bool {
|
||||
if error_summary.has_parsing_errors() {
|
||||
fn has_parsing_errors<T: Write>(&self, session: &Session<T>) -> bool {
|
||||
if session.has_parsing_errors() {
|
||||
write_message(&format!(
|
||||
"\u{261d}\u{1f3fd} Cannot format {}:{}",
|
||||
CONFIGURATIONS_FILE_NAME,
|
||||
|
@ -819,7 +819,7 @@ impl ConfigCodeBlock {
|
|||
{
|
||||
let mut session = Session::new(config, Some(&mut buf));
|
||||
session.format(input).unwrap();
|
||||
if self.has_parsing_errors(session.summary) {
|
||||
if self.has_parsing_errors(&session) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue