Update checkstyle write mode to take Write arguments.

By accepting Write traits we can write tests using StringBuffer.
This commit is contained in:
Mark Story 2016-01-19 00:02:21 -05:00
parent 66d4faf53f
commit d8c6f5954a
3 changed files with 33 additions and 28 deletions

View File

@ -8,30 +8,30 @@
// 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 std::io::{self, Write, Read};
use config::WriteMode;
pub fn output_header(mode: WriteMode) -> Result<(), io::Error> {
let stdout = stdout();
let mut stdout = stdout.lock();
pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
where T: Write
{
if mode == WriteMode::Checkstyle {
let mut xml_heading = String::new();
xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xml_heading.push_str("\n");
xml_heading.push_str("<checkstyle version=\"4.3\">");
try!(write!(stdout, "{}", xml_heading));
try!(write!(out, "{}", xml_heading));
}
Ok(())
}
pub fn output_footer(mode: WriteMode) -> Result<(), io::Error> {
let stdout = stdout();
let mut stdout = stdout.lock();
pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
where T: Write
{
if mode == WriteMode::Checkstyle {
let mut xml_tail = String::new();
xml_tail.push_str("</checkstyle>");
try!(write!(stdout, "{}", xml_tail));
try!(write!(out, "{}", xml_tail));
}
Ok(())
}

View File

@ -31,15 +31,18 @@ pub fn append_newlines(file_map: &mut FileMap) {
}
}
pub fn write_all_files(file_map: &FileMap,
mode: WriteMode,
config: &Config)
-> Result<(), io::Error> {
output_header(mode).ok();
pub fn write_all_files<T>(file_map: &FileMap,
mut out: T,
mode: WriteMode,
config: &Config)
-> Result<(), io::Error>
where T: Write
{
output_header(&mut out, mode).ok();
for filename in file_map.keys() {
try!(write_file(&file_map[filename], filename, mode, config));
try!(write_file(&file_map[filename], filename, &mut out, mode, config));
}
output_footer(mode).ok();
output_footer(&mut out, mode).ok();
Ok(())
}
@ -81,11 +84,14 @@ pub fn write_system_newlines<T>(writer: T,
}
}
pub fn write_file(text: &StringBuffer,
filename: &str,
mode: WriteMode,
config: &Config)
-> Result<Option<String>, io::Error> {
pub fn write_file<T>(text: &StringBuffer,
filename: &str,
out: &mut T,
mode: WriteMode,
config: &Config)
-> Result<Option<String>, io::Error>
where T: Write
{
fn source_and_formatted_text(text: &StringBuffer,
filename: &str,
@ -155,11 +161,8 @@ pub fn write_file(text: &StringBuffer,
unreachable!("The WriteMode should NEVER Be default at this point!");
}
WriteMode::Checkstyle => {
let stdout = stdout();
let stdout = stdout.lock();
let diff = try!(create_diff(filename, text, config));
// Output the XML tags for the lines that are different.
try!(output_checkstyle_file(stdout, filename, diff));
try!(output_checkstyle_file(out, filename, diff));
}
}

View File

@ -30,6 +30,7 @@ use syntax::codemap::{mk_sp, Span};
use syntax::diagnostic::{EmitterWriter, Handler};
use syntax::parse::{self, ParseSess};
use std::io::stdout;
use std::ops::{Add, Sub};
use std::path::Path;
use std::collections::HashMap;
@ -428,8 +429,8 @@ pub fn run(file: &Path, write_mode: WriteMode, config: &Config) {
let mut result = format(file, config, mode);
print!("{}", fmt_lines(&mut result, config));
let write_result = filemap::write_all_files(&result, mode, config);
let out = stdout();
let write_result = filemap::write_all_files(&result, out, mode, config);
if let Err(msg) = write_result {
println!("Error writing files: {}", msg);
@ -442,7 +443,8 @@ pub fn run_from_stdin(input: String, write_mode: WriteMode, config: &Config) {
let mut result = format_string(input, config, mode);
fmt_lines(&mut result, config);
let write_result = filemap::write_file(&result["stdin"], "stdin", mode, config);
let mut out = stdout();
let write_result = filemap::write_file(&result["stdin"], "stdin", &mut out, mode, config);
if let Err(msg) = write_result {
panic!("Error writing to stdout: {}", msg);