From 58410ddd9385b9570ba65744096ea6bedbb1e040 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 13 Jan 2016 00:22:30 -0500 Subject: [PATCH] Extract checkstyle output into a separate module. Rename functions a bit now that they are specific to checkstyle output. --- src/checkstyle.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++ src/filemap.rs | 84 +++----------------------------------------- src/lib.rs | 1 + 3 files changed, 94 insertions(+), 79 deletions(-) create mode 100644 src/checkstyle.rs diff --git a/src/checkstyle.rs b/src/checkstyle.rs new file mode 100644 index 000000000000..1eb74a99bbf8 --- /dev/null +++ b/src/checkstyle.rs @@ -0,0 +1,88 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +use rustfmt_diff::{Mismatch, DiffLine}; +use std::io::{self, Write, Read, stdout}; +use WriteMode; + + +pub fn output_heading(mode: WriteMode) -> Result<(), io::Error> { + let stdout = stdout(); + let mut stdout = stdout.lock(); + match mode { + WriteMode::Checkstyle => { + let mut xml_heading = String::new(); + xml_heading.push_str(""); + xml_heading.push_str("\n"); + xml_heading.push_str(""); + try!(write!(stdout, "{}", xml_heading)); + Ok(()) + } + _ => Ok(()), + } +} + +pub fn output_footing(mode: WriteMode) -> Result<(), io::Error> { + let stdout = stdout(); + let mut stdout = stdout.lock(); + match mode { + WriteMode::Checkstyle => { + let mut xml_tail = String::new(); + xml_tail.push_str(""); + try!(write!(stdout, "{}", xml_tail)); + Ok(()) + } + _ => Ok(()), + } +} + +pub fn output_checkstyle_file(mut writer: T, + filename: &str, + diff: Vec) + -> Result<(), io::Error> + where T: Write +{ + try!(write!(writer, "", filename)); + for mismatch in diff { + for line in mismatch.lines { + match line { + DiffLine::Expected(ref str) => { + let message = xml_escape_str(&str); + try!(write!(writer, + "", + mismatch.line_number, + message)); + } + _ => { + // Do nothing with context and expected. + } + } + } + } + try!(write!(writer, "")); + Ok(()) +} + +// Convert special characters into XML entities. +// This is needed for checkstyle output. +fn xml_escape_str(string: &str) -> String { + let mut out = String::new(); + for c in string.chars() { + match c { + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '"' => out.push_str("""), + '\'' => out.push_str("'"), + '&' => out.push_str("&"), + _ => out.push(c), + } + } + out +} diff --git a/src/filemap.rs b/src/filemap.rs index 436f5501882c..92d4267f6e1f 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -18,7 +18,8 @@ use std::fs::{self, File}; use std::io::{self, Write, Read, stdout, BufWriter}; use config::{NewlineStyle, Config, WriteMode}; -use rustfmt_diff::{make_diff, print_diff, Mismatch, DiffLine}; +use rustfmt_diff::{make_diff, print_diff, Mismatch}; +use checkstyle::{output_heading, output_footing, output_checkstyle_file}; // A map of the files of a crate, with their new content pub type FileMap = HashMap; @@ -38,86 +39,11 @@ pub fn write_all_files(file_map: &FileMap, for filename in file_map.keys() { try!(write_file(&file_map[filename], filename, mode, config)); } - output_trailing(mode).ok(); + output_footing(mode).ok(); Ok(()) } -pub fn output_heading(mode: WriteMode) -> Result<(), io::Error> { - let stdout = stdout(); - let mut stdout = stdout.lock(); - match mode { - WriteMode::Checkstyle => { - let mut xml_heading = String::new(); - xml_heading.push_str(""); - xml_heading.push_str("\n"); - xml_heading.push_str(""); - try!(write!(stdout, "{}", xml_heading)); - Ok(()) - } - _ => Ok(()), - } -} - -pub fn output_trailing(mode: WriteMode) -> Result<(), io::Error> { - let stdout = stdout(); - let mut stdout = stdout.lock(); - match mode { - WriteMode::Checkstyle => { - let mut xml_tail = String::new(); - xml_tail.push_str(""); - try!(write!(stdout, "{}", xml_tail)); - Ok(()) - } - _ => Ok(()), - } -} - -pub fn output_checkstyle_file(mut writer: T, - filename: &str, - diff: Vec) - -> Result<(), io::Error> - where T: Write -{ - try!(write!(writer, "", filename)); - for mismatch in diff { - for line in mismatch.lines { - match line { - DiffLine::Expected(ref str) => { - let message = xml_escape_str(&str); - // TODO XML encode str here. - try!(write!(writer, - "", - mismatch.line_number, - message)); - } - _ => { - // Do nothing with context and expected. - } - } - } - } - try!(write!(writer, "")); - Ok(()) -} - -// Convert special characters into XML entities. -// This is needed for checkstyle output. -fn xml_escape_str(string: &str) -> String { - let mut out = String::new(); - for c in string.chars() { - match c { - '<' => out.push_str("<"), - '>' => out.push_str(">"), - '"' => out.push_str("""), - '\'' => out.push_str("'"), - '&' => out.push_str("&"), - _ => out.push(c), - } - } - out -} // Prints all newlines either as `\n` or as `\r\n`. pub fn write_system_newlines(writer: T, @@ -178,8 +104,8 @@ pub fn write_file(text: &StringBuffer, text: &StringBuffer, config: &Config) -> Result, io::Error> { - let ori_text, fmt_text = try!(source_and_formatted_text(text, filename, config)); - Ok(make_diff(&ori_text, &fmt_text, 3)) + let (ori, fmt) = try!(source_and_formatted_text(text, filename, config)); + Ok(make_diff(&ori, &fmt, 3)) } match mode { diff --git a/src/lib.rs b/src/lib.rs index fccc0f0934c9..6d60c0f278df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,7 @@ mod utils; pub mod config; pub mod filemap; mod visitor; +mod checkstyle; mod items; mod missed_spans; mod lists;