2016-01-13 00:22:30 -05:00
|
|
|
// 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 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
use rustfmt_diff::{Mismatch, DiffLine};
|
2016-05-03 21:52:55 +09:00
|
|
|
use std::io::{self, Write};
|
2016-01-13 20:56:35 -05:00
|
|
|
use config::WriteMode;
|
2016-01-13 00:22:30 -05:00
|
|
|
|
|
|
|
|
2016-01-19 00:02:21 -05:00
|
|
|
pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-19 00:02:21 -05:00
|
|
|
{
|
2016-01-13 20:56:35 -05:00
|
|
|
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\">");
|
2017-05-08 13:13:49 +09:00
|
|
|
write!(out, "{}", xml_heading)?;
|
2016-01-13 00:22:30 -05:00
|
|
|
}
|
2016-01-13 20:56:35 -05:00
|
|
|
Ok(())
|
2016-01-13 00:22:30 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 00:02:21 -05:00
|
|
|
pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-19 00:02:21 -05:00
|
|
|
{
|
2016-01-13 20:56:35 -05:00
|
|
|
if mode == WriteMode::Checkstyle {
|
|
|
|
let mut xml_tail = String::new();
|
|
|
|
xml_tail.push_str("</checkstyle>");
|
2017-05-08 13:13:49 +09:00
|
|
|
write!(out, "{}", xml_tail)?;
|
2016-01-13 00:22:30 -05:00
|
|
|
}
|
2016-01-13 20:56:35 -05:00
|
|
|
Ok(())
|
2016-01-13 00:22:30 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
pub fn output_checkstyle_file<T>(
|
|
|
|
mut writer: T,
|
|
|
|
filename: &str,
|
|
|
|
diff: Vec<Mismatch>,
|
|
|
|
) -> Result<(), io::Error>
|
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-13 00:22:30 -05:00
|
|
|
{
|
2017-05-08 13:13:49 +09:00
|
|
|
write!(writer, "<file name=\"{}\">", filename)?;
|
2016-01-13 00:22:30 -05:00
|
|
|
for mismatch in diff {
|
|
|
|
for line in mismatch.lines {
|
2016-08-23 23:14:45 +09:00
|
|
|
// Do nothing with `DiffLine::Context` and `DiffLine::Resulting`.
|
|
|
|
if let DiffLine::Expected(ref str) = line {
|
|
|
|
let message = xml_escape_str(str);
|
2017-06-12 15:58:58 +12:00
|
|
|
write!(
|
|
|
|
writer,
|
|
|
|
"<error line=\"{}\" severity=\"warning\" message=\"Should be `{}`\" \
|
2017-06-18 22:45:08 +09:00
|
|
|
/>",
|
2017-06-12 15:58:58 +12:00
|
|
|
mismatch.line_number,
|
|
|
|
message
|
|
|
|
)?;
|
2016-01-13 00:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-08 13:13:49 +09:00
|
|
|
write!(writer, "</file>")?;
|
2016-01-13 00:22:30 -05:00
|
|
|
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
|
|
|
|
}
|