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};
16 use checkstyle::{output_checkstyle_file, output_footer, output_header};
17 use config::{Config, NewlineStyle, WriteMode};
18 use rustfmt_diff::{make_diff, print_diff, Mismatch};
20 // A map of the files of a crate, with their new content
21 pub type FileMap = Vec<FileRecord>;
23 pub type FileRecord = (String, String);
25 // Append a newline to the end of each file.
26 pub fn append_newline(s: &mut String) {
30 pub fn write_all_files<T>(
31 file_map: &[FileRecord],
34 ) -> Result<(), io::Error>
38 output_header(out, config.write_mode()).ok();
39 for &(ref filename, ref text) in file_map {
40 write_file(text, filename, out, config)?;
42 output_footer(out, config.write_mode()).ok();
47 // Prints all newlines either as `\n` or as `\r\n`.
48 pub fn write_system_newlines<T>(writer: T, text: &String, config: &Config) -> Result<(), io::Error>
52 // Buffer output, since we're writing a since char at a time.
53 let mut writer = BufWriter::new(writer);
55 let style = if config.newline_style() == NewlineStyle::Native {
62 config.newline_style()
66 NewlineStyle::Unix => write!(writer, "{}", text),
67 NewlineStyle::Windows => {
68 for c in text.chars() {
70 '\n' => write!(writer, "\r\n")?,
72 c => write!(writer, "{}", c)?,
77 NewlineStyle::Native => unreachable!(),
86 ) -> Result<bool, io::Error>
90 fn source_and_formatted_text(
94 ) -> Result<(String, String), io::Error> {
95 let mut f = File::open(filename)?;
96 let mut ori_text = String::new();
97 f.read_to_string(&mut ori_text)?;
98 let mut v = Vec::new();
99 write_system_newlines(&mut v, text, config)?;
100 let fmt_text = String::from_utf8(v).unwrap();
101 Ok((ori_text, fmt_text))
108 ) -> Result<Vec<Mismatch>, io::Error> {
109 let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
110 Ok(make_diff(&ori, &fmt, 3))
113 match config.write_mode() {
114 WriteMode::Replace => {
115 if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
117 // Do a little dance to make writing safer - write to a temp file
118 // rename the original to a .bk, then rename the temp file to the
120 let tmp_name = filename.to_owned() + ".tmp";
121 let bk_name = filename.to_owned() + ".bk";
123 // Write text to temp file
124 let tmp_file = File::create(&tmp_name)?;
125 write_system_newlines(tmp_file, text, config)?;
128 fs::rename(filename, bk_name)?;
129 fs::rename(tmp_name, filename)?;
133 WriteMode::Overwrite => {
134 // Write text directly over original file if there is a diff.
135 let (source, formatted) = source_and_formatted_text(text, filename, config)?;
136 if source != formatted {
137 let file = File::create(filename)?;
138 write_system_newlines(file, text, config)?;
141 WriteMode::Plain => {
142 write_system_newlines(out, text, config)?;
144 WriteMode::Display | WriteMode::Coverage => {
145 println!("{}:\n", filename);
146 write_system_newlines(out, text, config)?;
149 if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
150 let mismatch = make_diff(&ori, &fmt, 3);
151 let has_diff = !mismatch.is_empty();
154 |line_num| format!("Diff in {} at line {}:", filename, line_num),
160 WriteMode::Checkstyle => {
161 let diff = create_diff(filename, text, config)?;
162 output_checkstyle_file(out, filename, diff)?;
166 // when we are not in diff mode, don't indicate differing files