diff --git a/src/checkstyle.rs b/src/checkstyle.rs index e25bfc03ba4..02e214864fa 100644 --- a/src/checkstyle.rs +++ b/src/checkstyle.rs @@ -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(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_heading.push_str("\n"); xml_heading.push_str(""); - 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(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(""); - try!(write!(stdout, "{}", xml_tail)); + try!(write!(out, "{}", xml_tail)); } Ok(()) } diff --git a/src/filemap.rs b/src/filemap.rs index dabf7ade3e1..61ad573f702 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -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(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(writer: T, } } -pub fn write_file(text: &StringBuffer, - filename: &str, - mode: WriteMode, - config: &Config) - -> Result, io::Error> { +pub fn write_file(text: &StringBuffer, + filename: &str, + out: &mut T, + mode: WriteMode, + config: &Config) + -> Result, 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)); } } diff --git a/src/lib.rs b/src/lib.rs index 6d60c0f278d..ad9322ab649 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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);