// 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 or the MIT license // , at your // 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}; use config::WriteMode; 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(""); write!(out, "{}", xml_heading)?; } Ok(()) } 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(""); write!(out, "{}", xml_tail)?; } Ok(()) } pub fn output_checkstyle_file(mut writer: T, filename: &str, diff: Vec) -> Result<(), io::Error> where T: Write { write!(writer, "", filename)?; for mismatch in diff { for line in mismatch.lines { // Do nothing with `DiffLine::Context` and `DiffLine::Resulting`. if let DiffLine::Expected(ref str) = line { let message = xml_escape_str(str); write!(writer, "", mismatch.line_number, message)?; } } } write!(writer, "")?; 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 }