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 strings::string_buffer::StringBuffer;
16 use std::collections::HashMap;
17 use std::fs::{self, File};
18 use std::io::{self, Write, Read, stdout};
21 use config::{NewlineStyle, Config};
22 use rustfmt_diff::{make_diff, print_diff};
24 // A map of the files of a crate, with their new content
25 pub type FileMap = HashMap<String, StringBuffer>;
27 // Append a newline to the end of each file.
28 pub fn append_newlines(file_map: &mut FileMap) {
29 for (_, s) in file_map.iter_mut() {
34 pub fn write_all_files(file_map: &FileMap,
37 -> Result<(HashMap<String, String>), io::Error> {
38 let mut result = HashMap::new();
39 for filename in file_map.keys() {
40 let one_result = try!(write_file(&file_map[filename], filename, mode, config));
41 if let Some(r) = one_result {
42 result.insert(filename.clone(), r);
49 pub fn write_file(text: &StringBuffer,
53 -> Result<Option<String>, io::Error> {
55 // prints all newlines either as `\n` or as `\r\n`
56 fn write_system_newlines<T>(mut writer: T,
59 -> Result<(), io::Error>
62 let style = if config.newline_style == NewlineStyle::Native {
73 NewlineStyle::Unix => write!(writer, "{}", text),
74 NewlineStyle::Windows => {
75 for (c, _) in text.chars() {
77 '\n' => try!(write!(writer, "\r\n")),
79 c => try!(write!(writer, "{}", c)),
84 NewlineStyle::Native => unreachable!(),
89 WriteMode::Replace => {
90 // Do a little dance to make writing safer - write to a temp file
91 // rename the original to a .bk, then rename the temp file to the
93 let tmp_name = filename.to_owned() + ".tmp";
94 let bk_name = filename.to_owned() + ".bk";
96 // Write text to temp file
97 let tmp_file = try!(File::create(&tmp_name));
98 try!(write_system_newlines(tmp_file, text, config));
101 try!(fs::rename(filename, bk_name));
102 try!(fs::rename(tmp_name, filename));
104 WriteMode::Overwrite => {
105 // Write text directly over original file.
106 let file = try!(File::create(filename));
107 try!(write_system_newlines(file, text, config));
109 WriteMode::NewFile(extn) => {
110 let filename = filename.to_owned() + "." + extn;
111 let file = try!(File::create(&filename));
112 try!(write_system_newlines(file, text, config));
114 WriteMode::Plain => {
115 let stdout = stdout();
116 let stdout = stdout.lock();
117 try!(write_system_newlines(stdout, text, config));
119 WriteMode::Display | WriteMode::Coverage => {
120 println!("{}:\n", filename);
121 let stdout = stdout();
122 let stdout = stdout.lock();
123 try!(write_system_newlines(stdout, text, config));
126 println!("Diff of {}:\n", filename);
127 let mut f = try!(File::open(filename));
128 let mut ori_text = String::new();
129 try!(f.read_to_string(&mut ori_text));
130 let mut v = Vec::new();
131 try!(write_system_newlines(&mut v, text, config));
132 let fmt_text = String::from_utf8(v).unwrap();
133 let diff = make_diff(&ori_text, &fmt_text, 3);
134 print_diff(diff, |line_num| format!("\nDiff at line {}:", line_num));
136 WriteMode::Return => {
137 // io::Write is not implemented for String, working around with
139 let mut v = Vec::new();
140 try!(write_system_newlines(&mut v, text, config));
141 // won't panic, we are writing correct utf8
142 return Ok(Some(String::from_utf8(v).unwrap()));