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.
11 use std::io::{self, Write};
13 use config::WriteMode;
14 use rustfmt_diff::{DiffLine, Mismatch};
16 pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
20 if mode == WriteMode::Checkstyle {
21 let mut xml_heading = String::new();
22 xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
23 xml_heading.push_str("\n");
24 xml_heading.push_str("<checkstyle version=\"4.3\">");
25 write!(out, "{}", xml_heading)?;
30 pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
34 if mode == WriteMode::Checkstyle {
35 let mut xml_tail = String::new();
36 xml_tail.push_str("</checkstyle>");
37 write!(out, "{}", xml_tail)?;
42 pub fn output_checkstyle_file<T>(
46 ) -> Result<(), io::Error>
50 write!(writer, "<file name=\"{}\">", filename)?;
51 for mismatch in diff {
52 for line in mismatch.lines {
53 // Do nothing with `DiffLine::Context` and `DiffLine::Resulting`.
54 if let DiffLine::Expected(ref str) = line {
55 let message = xml_escape_str(str);
58 "<error line=\"{}\" severity=\"warning\" message=\"Should be `{}`\" \
66 write!(writer, "</file>")?;
70 // Convert special characters into XML entities.
71 // This is needed for checkstyle output.
72 fn xml_escape_str(string: &str) -> String {
73 let mut out = String::new();
74 for c in string.chars() {
76 '<' => out.push_str("<"),
77 '>' => out.push_str(">"),
78 '"' => out.push_str("""),
79 '\'' => out.push_str("'"),
80 '&' => out.push_str("&"),