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, output_modified, print_diff, Mismatch};
20 use syntax::codemap::FileName;
24 // Append a newline to the end of each file.
25 pub fn append_newline(s: &mut String) {
29 pub fn write_all_files<T>(
30 file_map: &[FileRecord],
33 ) -> Result<(), io::Error>
37 output_header(out, config.write_mode()).ok();
38 for &(ref filename, ref text) in file_map {
39 write_file(text, filename, out, config)?;
41 output_footer(out, config.write_mode()).ok();
46 // Prints all newlines either as `\n` or as `\r\n`.
47 pub fn write_system_newlines<T>(writer: T, text: &str, config: &Config) -> Result<(), io::Error>
51 // Buffer output, since we're writing a since char at a time.
52 let mut writer = BufWriter::new(writer);
54 let style = if config.newline_style() == NewlineStyle::Native {
61 config.newline_style()
65 NewlineStyle::Unix => write!(writer, "{}", text),
66 NewlineStyle::Windows => {
67 for c in text.chars() {
69 '\n' => write!(writer, "\r\n")?,
71 c => write!(writer, "{}", c)?,
76 NewlineStyle::Native => unreachable!(),
85 ) -> Result<bool, io::Error>
89 fn source_and_formatted_text(
93 ) -> Result<(String, String), io::Error> {
94 let mut f = File::open(filename)?;
95 let mut ori_text = String::new();
96 f.read_to_string(&mut ori_text)?;
97 let mut v = Vec::new();
98 write_system_newlines(&mut v, text, config)?;
99 let fmt_text = String::from_utf8(v).unwrap();
100 Ok((ori_text, fmt_text))
107 ) -> Result<Vec<Mismatch>, io::Error> {
108 let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
109 Ok(make_diff(&ori, &fmt, 3))
112 let filename_to_path = || match *filename {
113 FileName::Real(ref path) => path,
114 _ => panic!("cannot format `{}` with WriteMode::Replace", filename),
117 match config.write_mode() {
118 WriteMode::Replace => {
119 let filename = filename_to_path();
120 if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
122 // Do a little dance to make writing safer - write to a temp file
123 // rename the original to a .bk, then rename the temp file to the
125 let tmp_name = filename.with_extension("tmp");
126 let bk_name = filename.with_extension("bk");
128 // Write text to temp file
129 let tmp_file = File::create(&tmp_name)?;
130 write_system_newlines(tmp_file, text, config)?;
133 fs::rename(filename, bk_name)?;
134 fs::rename(tmp_name, filename)?;
138 WriteMode::Overwrite => {
139 // Write text directly over original file if there is a diff.
140 let filename = filename_to_path();
141 let (source, formatted) = source_and_formatted_text(text, filename, config)?;
142 if source != formatted {
143 let file = File::create(filename)?;
144 write_system_newlines(file, text, config)?;
147 WriteMode::Plain => {
148 write_system_newlines(out, text, config)?;
150 WriteMode::Display | WriteMode::Coverage => {
151 println!("{}:\n", filename);
152 write_system_newlines(out, text, config)?;
155 let filename = filename_to_path();
156 if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
157 let mismatch = make_diff(&ori, &fmt, 3);
158 let has_diff = !mismatch.is_empty();
161 |line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
167 WriteMode::Modified => {
168 let filename = filename_to_path();
169 if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
170 let mismatch = make_diff(&ori, &fmt, 0);
171 let has_diff = !mismatch.is_empty();
172 output_modified(out, mismatch);
176 WriteMode::Checkstyle => {
177 let filename = filename_to_path();
178 let diff = create_diff(filename, text, config)?;
179 output_checkstyle_file(out, filename, diff)?;
183 // when we are not in diff mode, don't indicate differing files