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.
14 use std::io::{self, Write};
16 use checkstyle::output_checkstyle_file;
17 use config::{Config, EmitMode, FileName, Verbosity};
18 use rustfmt_diff::{make_diff, output_modified, print_diff};
23 // Append a newline to the end of each file.
24 pub fn append_newline(s: &mut String) {
29 pub(crate) fn write_all_files<T>(
30 file_map: &[FileRecord],
33 ) -> Result<(), io::Error>
37 if config.emit_mode() == EmitMode::Checkstyle {
38 write!(out, "{}", ::checkstyle::header())?;
40 for &(ref filename, ref text) in file_map {
41 write_file(text, filename, out, config)?;
43 if config.emit_mode() == EmitMode::Checkstyle {
44 write!(out, "{}", ::checkstyle::footer())?;
55 ) -> Result<bool, io::Error>
59 let filename_to_path = || match *filename {
60 FileName::Real(ref path) => path,
61 _ => panic!("cannot format `{}` and emit to files", filename),
64 match config.emit_mode() {
65 EmitMode::Files if config.make_backup() => {
66 let filename = filename_to_path();
67 let ori = fs::read_to_string(filename)?;
68 if ori != formatted_text {
69 // Do a little dance to make writing safer - write to a temp file
70 // rename the original to a .bk, then rename the temp file to the
72 let tmp_name = filename.with_extension("tmp");
73 let bk_name = filename.with_extension("bk");
75 fs::write(&tmp_name, formatted_text)?;
76 fs::rename(filename, bk_name)?;
77 fs::rename(tmp_name, filename)?;
81 // Write text directly over original file if there is a diff.
82 let filename = filename_to_path();
83 let ori = fs::read_to_string(filename)?;
84 if ori != formatted_text {
85 fs::write(filename, formatted_text)?;
88 EmitMode::Stdout | EmitMode::Coverage => {
89 if config.verbose() != Verbosity::Quiet {
90 println!("{}:\n", filename);
92 write!(out, "{}", formatted_text)?;
94 EmitMode::ModifiedLines => {
95 let filename = filename_to_path();
96 let ori = fs::read_to_string(filename)?;
97 let mismatch = make_diff(&ori, formatted_text, 0);
98 let has_diff = !mismatch.is_empty();
99 output_modified(out, mismatch);
102 EmitMode::Checkstyle => {
103 let filename = filename_to_path();
104 let ori = fs::read_to_string(filename)?;
105 let diff = make_diff(&ori, formatted_text, 3);
106 output_checkstyle_file(out, filename, diff)?;
109 let filename = filename_to_path();
110 let ori = fs::read_to_string(filename)?;
111 let mismatch = make_diff(&ori, formatted_text, 3);
112 let has_diff = !mismatch.is_empty();
115 |line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
122 // when we are not in diff mode, don't indicate differing files