1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 use rustfmt_diff::{Mismatch, DiffLine};
11 use std::io::{self, Write, Read, stdout};
12 use config::WriteMode;
15 pub fn output_header(mode: WriteMode) -> Result<(), io::Error> {
16 let stdout = stdout();
17 let mut stdout = stdout.lock();
18 if mode == WriteMode::Checkstyle {
19 let mut xml_heading = String::new();
20 xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
21 xml_heading.push_str("\n");
22 xml_heading.push_str("<checkstyle version=\"4.3\">");
23 try!(write!(stdout, "{}", xml_heading));
28 pub fn output_footer(mode: WriteMode) -> Result<(), io::Error> {
29 let stdout = stdout();
30 let mut stdout = stdout.lock();
31 if mode == WriteMode::Checkstyle {
32 let mut xml_tail = String::new();
33 xml_tail.push_str("</checkstyle>");
34 try!(write!(stdout, "{}", xml_tail));
39 pub fn output_checkstyle_file<T>(mut writer: T,
42 -> Result<(), io::Error>
45 try!(write!(writer, "<file name=\"{}\">", filename));
46 for mismatch in diff {
47 for line in mismatch.lines {
49 DiffLine::Expected(ref str) => {
50 let message = xml_escape_str(&str);
52 "<error line=\"{}\" severity=\"warning\" message=\"Should be \
58 // Do nothing with context and expected.
63 try!(write!(writer, "</file>"));
67 // Convert special characters into XML entities.
68 // This is needed for checkstyle output.
69 fn xml_escape_str(string: &str) -> String {
70 let mut out = String::new();
71 for c in string.chars() {
73 '<' => out.push_str("<"),
74 '>' => out.push_str(">"),
75 '"' => out.push_str("""),
76 '\'' => out.push_str("'"),
77 '&' => out.push_str("&"),