]> git.lizzy.rs Git - rust.git/blob - src/checkstyle.rs
Merge pull request #793 from kamalmarhubi/expect-formatting
[rust.git] / src / checkstyle.rs
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.
4 //
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};
12 use config::WriteMode;
13
14
15 pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
16     where T: Write
17 {
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!(out, "{}", xml_heading));
24     }
25     Ok(())
26 }
27
28 pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
29     where T: Write
30 {
31     if mode == WriteMode::Checkstyle {
32         let mut xml_tail = String::new();
33         xml_tail.push_str("</checkstyle>");
34         try!(write!(out, "{}", xml_tail));
35     }
36     Ok(())
37 }
38
39 pub fn output_checkstyle_file<T>(mut writer: T,
40                                  filename: &str,
41                                  diff: Vec<Mismatch>)
42                                  -> Result<(), io::Error>
43     where T: Write
44 {
45     try!(write!(writer, "<file name=\"{}\">", filename));
46     for mismatch in diff {
47         for line in mismatch.lines {
48             match line {
49                 DiffLine::Expected(ref str) => {
50                     let message = xml_escape_str(&str);
51                     try!(write!(writer,
52                                 "<error line=\"{}\" severity=\"warning\" message=\"Should be \
53                                  `{}`\" />",
54                                 mismatch.line_number,
55                                 message));
56                 }
57                 _ => {
58                     // Do nothing with context and expected.
59                 }
60             }
61         }
62     }
63     try!(write!(writer, "</file>"));
64     Ok(())
65 }
66
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() {
72         match c {
73             '<' => out.push_str("&lt;"),
74             '>' => out.push_str("&gt;"),
75             '"' => out.push_str("&quot;"),
76             '\'' => out.push_str("&apos;"),
77             '&' => out.push_str("&amp;"),
78             _ => out.push(c),
79         }
80     }
81     out
82 }