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.
13 use std::fs::{self, File};
14 use std::io::{self, BufWriter, Read, Write};
17 use checkstyle::{output_checkstyle_file, output_footer, output_header};
18 use config::{Config, NewlineStyle, WriteMode};
19 use rustfmt_diff::{make_diff, print_diff, Mismatch};
20 use syntax::codemap::FileName;
22 // A map of the files of a crate, with their new content
23 pub type FileMap = Vec<FileRecord>;
25 pub type FileRecord = (FileName, String);
27 // Append a newline to the end of each file.
28 pub fn append_newline(s: &mut String) {
32 pub fn write_all_files<T>(
33 file_map: &[FileRecord],
36 ) -> Result<(), io::Error>
40 output_header(out, config.write_mode()).ok();
41 for &(ref filename, ref text) in file_map {
42 write_file(text, filename, out, config)?;
44 output_footer(out, config.write_mode()).ok();
49 // Prints all newlines either as `\n` or as `\r\n`.
50 pub fn write_system_newlines<T>(writer: T, text: &str, config: &Config) -> Result<(), io::Error>
54 // Buffer output, since we're writing a since char at a time.
55 let mut writer = BufWriter::new(writer);
57 let style = if config.newline_style() == NewlineStyle::Native {
64 config.newline_style()
68 NewlineStyle::Unix => write!(writer, "{}", text),
69 NewlineStyle::Windows => {
70 for c in text.chars() {
72 '\n' => write!(writer, "\r\n")?,
74 c => write!(writer, "{}", c)?,
79 NewlineStyle::Native => unreachable!(),
88 ) -> Result<bool, io::Error>
92 fn source_and_formatted_text(
96 ) -> Result<(String, String), io::Error> {
97 let mut f = File::open(filename)?;
98 let mut ori_text = String::new();
99 f.read_to_string(&mut ori_text)?;
100 let mut v = Vec::new();
101 write_system_newlines(&mut v, text, config)?;
102 let fmt_text = String::from_utf8(v).unwrap();
103 Ok((ori_text, fmt_text))
110 ) -> Result<Vec<Mismatch>, io::Error> {
111 let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
112 Ok(make_diff(&ori, &fmt, 3))
115 let filename_to_path = || match *filename {
116 FileName::Real(ref path) => path,
117 _ => panic!("cannot format `{}` with WriteMode::Replace", filename),
120 match config.write_mode() {
121 WriteMode::Replace => {
122 let filename = filename_to_path();
123 if let Ok((ori, fmt)) = source_and_formatted_text(text, &filename, config) {
125 // Do a little dance to make writing safer - write to a temp file
126 // rename the original to a .bk, then rename the temp file to the
128 let tmp_name = filename.with_extension("tmp");
129 let bk_name = filename.with_extension("bk");
131 // Write text to temp file
132 let tmp_file = File::create(&tmp_name)?;
133 write_system_newlines(tmp_file, text, config)?;
136 fs::rename(filename, bk_name)?;
137 fs::rename(tmp_name, filename)?;
141 WriteMode::Overwrite => {
142 // Write text directly over original file if there is a diff.
143 let filename = filename_to_path();
144 let (source, formatted) = source_and_formatted_text(text, &filename, config)?;
145 if source != formatted {
146 let file = File::create(filename)?;
147 write_system_newlines(file, text, config)?;
150 WriteMode::Plain => {
151 write_system_newlines(out, text, config)?;
153 WriteMode::Display | WriteMode::Coverage => {
154 println!("{}:\n", filename);
155 write_system_newlines(out, text, config)?;
158 let filename = filename_to_path();
159 if let Ok((ori, fmt)) = source_and_formatted_text(text, &filename, config) {
160 let mismatch = make_diff(&ori, &fmt, 3);
161 let has_diff = !mismatch.is_empty();
164 |line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
170 WriteMode::Checkstyle => {
171 let filename = filename_to_path();
172 let diff = create_diff(&filename, text, config)?;
173 output_checkstyle_file(out, filename, diff)?;
177 // when we are not in diff mode, don't indicate differing files