From abb253df8b8bb8638eaa7ac44d06a042882e5f22 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Sat, 19 May 2018 16:44:48 +1200 Subject: [PATCH] Rationalise result and error types --- src/config/summary.rs | 1 - src/lib.rs | 38 ++++++++++--------------------------- src/rewrite.rs | 2 +- src/test/mod.rs | 44 ++++++++++++++++++++----------------------- src/visitor.rs | 4 ++-- 5 files changed, 33 insertions(+), 56 deletions(-) diff --git a/src/config/summary.rs b/src/config/summary.rs index 7b6c25d498b..c7f8124c1c7 100644 --- a/src/config/summary.rs +++ b/src/config/summary.rs @@ -11,7 +11,6 @@ use std::default::Default; use std::time::{Duration, Instant}; -#[must_use] #[derive(Debug, Default, Clone, Copy)] pub struct Summary { // Encountered e.g. an IO error. diff --git a/src/lib.rs b/src/lib.rs index 5a44074c845..a5e1ada8902 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,7 +206,7 @@ fn format_len(&self) -> (usize, usize) { } #[derive(Clone)] -pub struct FormatReport { +struct FormatReport { // Maps stringified file paths to their associated formatting errors. internal: Rc>, } @@ -756,21 +756,21 @@ pub fn format_input( input: Input, config: &Config, out: Option<&mut T>, -) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> { - syntax::with_globals(|| format_input_inner(input, config, out)) +) -> Result { + syntax::with_globals(|| format_input_inner(input, config, out)).map(|tup| tup.0) } fn format_input_inner( input: Input, config: &Config, mut out: Option<&mut T>, -) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> { +) -> Result<(Summary, FileMap, FormatReport), (ErrorKind, Summary)> { let mut summary = Summary::default(); if config.disable_all_formatting() { // When the input is from stdin, echo back the input. if let Input::Text(ref buf) = input { if let Err(e) = io::stdout().write_all(buf.as_bytes()) { - return Err((e, summary)); + return Err((From::from(e), summary)); } } return Ok((summary, FileMap::new(), FormatReport::new())); @@ -890,7 +890,7 @@ fn duration_to_f32(d: Duration) -> f32 { Ok((summary, file_map, report)) } - Err(e) => Err((e, summary)), + Err(e) => Err((From::from(e), summary)), } } @@ -913,33 +913,20 @@ struct ModifiedLines { pub chunks: Vec, } -/// The successful result of formatting via `get_modified_lines()`. -#[cfg(test)] -struct ModifiedLinesResult { - /// The high level summary details - pub summary: Summary, - /// The result Filemap - pub filemap: FileMap, - /// Map of formatting errors - pub report: FormatReport, - /// The sets of updated lines. - pub modified_lines: ModifiedLines, -} - /// Format a file and return a `ModifiedLines` data structure describing /// the changed ranges of lines. #[cfg(test)] fn get_modified_lines( input: Input, config: &Config, -) -> Result { +) -> Result { use std::io::BufRead; let mut data = Vec::new(); let mut config = config.clone(); config.set().write_mode(config::WriteMode::Modified); - let (summary, filemap, report) = format_input(input, &config, Some(&mut data))?; + format_input(input, &config, Some(&mut data))?; let mut lines = data.lines(); let mut chunks = Vec::new(); @@ -963,12 +950,7 @@ fn get_modified_lines( lines: added_lines, }); } - Ok(ModifiedLinesResult { - summary, - filemap, - report, - modified_lines: ModifiedLines { chunks }, - }) + Ok(ModifiedLines { chunks }) } #[derive(Debug)] @@ -982,7 +964,7 @@ pub fn format_and_emit_report(input: Input, config: &Config) -> Result { if report.has_warnings() { match term::stderr() { diff --git a/src/rewrite.rs b/src/rewrite.rs index 0dfae84bfc2..00c03a3c879 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -39,7 +39,7 @@ pub struct RewriteContext<'a> { // When rewriting chain, veto going multi line except the last element pub force_one_line_chain: RefCell, pub snippet_provider: &'a SnippetProvider<'a>, - pub report: FormatReport, + pub(crate) report: FormatReport, } impl<'a> RewriteContext<'a> { diff --git a/src/test/mod.rs b/src/test/mod.rs index b538519ec0b..b4089cc1cfe 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -10,6 +10,8 @@ extern crate assert_cli; +use syntax; + use std::collections::{HashMap, HashSet}; use std::env; use std::fs; @@ -143,7 +145,7 @@ fn modified_test() { let filename = "tests/writemode/source/modified.rs"; let result = get_modified_lines(Input::File(filename.into()), &Config::default()).unwrap(); assert_eq!( - result.modified_lines, + result, ModifiedLines { chunks: vec![ ModifiedChunk { @@ -240,19 +242,13 @@ fn self_tests() { #[test] fn stdin_formatting_smoke_test() { let input = Input::Text("fn main () {}".to_owned()); - let config = Config::default(); - let (error_summary, file_map, _report) = - format_input::(input, &config, None).unwrap(); + let mut config = Config::default(); + config.set().write_mode(WriteMode::Display); + let mut buf: Vec = vec![]; + let error_summary = format_input(input, &config, Some(&mut buf)).unwrap(); assert!(error_summary.has_no_errors()); - for &(ref file_name, ref text) in &file_map { - if let FileName::Custom(ref file_name) = *file_name { - if file_name == "stdin" { - assert_eq!(text.to_string(), "fn main() {}\n"); - return; - } - } - } - panic!("no stdin"); + //eprintln!("{:?}", ); + assert_eq!(buf, "fn main() {}\n".as_bytes()); } // FIXME(#1990) restore this test @@ -284,8 +280,7 @@ fn format_lines_errors_are_reported() { let input = Input::Text(format!("fn {}() {{}}", long_identifier)); let mut config = Config::default(); config.set().error_on_line_overflow(true); - let (error_summary, _file_map, _report) = - format_input::(input, &config, None).unwrap(); + let error_summary = format_input::(input, &config, None).unwrap(); assert!(error_summary.has_formatting_errors()); } @@ -296,8 +291,7 @@ fn format_lines_errors_are_reported_with_tabs() { let mut config = Config::default(); config.set().error_on_line_overflow(true); config.set().hard_tabs(true); - let (error_summary, _file_map, _report) = - format_input::(input, &config, None).unwrap(); + let error_summary = format_input::(input, &config, None).unwrap(); assert!(error_summary.has_formatting_errors()); } @@ -382,7 +376,8 @@ fn read_config(filename: &Path) -> Config { fn format_file>(filepath: P, config: &Config) -> (Summary, FileMap, FormatReport) { let filepath = filepath.into(); let input = Input::File(filepath); - format_input::(input, config, None).unwrap() + //format_input::(input, config, None).unwrap() + syntax::with_globals(|| format_input_inner::(input, config, None)).unwrap() } pub enum IdempotentCheckError { @@ -757,8 +752,7 @@ fn print_diff(&self, compare: Vec) { }); } - fn formatted_has_diff(&self, file_map: &FileMap) -> bool { - let &(ref _file_name, ref text) = file_map.first().unwrap(); + fn formatted_has_diff(&self, text: &str) -> bool { let compare = make_diff(self.code_block.as_ref().unwrap(), text, DIFF_CONTEXT_SIZE); if !compare.is_empty() { self.print_diff(compare); @@ -778,12 +772,14 @@ fn formatted_is_idempotent(&self) -> bool { } let input = Input::Text(self.code_block.as_ref().unwrap().to_owned()); - let config = self.get_block_config(); + let mut config = self.get_block_config(); + config.set().write_mode(WriteMode::Display); + let mut buf: Vec = vec![]; - let (error_summary, file_map, _report) = - format_input::(input, &config, None).unwrap(); + let error_summary = format_input(input, &config, Some(&mut buf)).unwrap(); - !self.has_parsing_errors(error_summary) && !self.formatted_has_diff(&file_map) + !self.has_parsing_errors(error_summary) + && !self.formatted_has_diff(&String::from_utf8(buf).unwrap()) } // Extract a code block from the iterator. Behavior: diff --git a/src/visitor.rs b/src/visitor.rs index a5f0c4441f6..bf1f95381d9 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -70,7 +70,7 @@ pub struct FmtVisitor<'a> { pub snippet_provider: &'a SnippetProvider<'a>, pub line_number: usize, pub skipped_range: Vec<(usize, usize)>, - pub report: FormatReport, + pub(crate) report: FormatReport, } impl<'b, 'a: 'b> FmtVisitor<'a> { @@ -561,7 +561,7 @@ pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> { ) } - pub fn from_codemap( + pub(crate) fn from_codemap( parse_session: &'a ParseSess, config: &'a Config, snippet_provider: &'a SnippetProvider,