- fn source_and_formatted_text(
- text: &str,
- filename: &str,
- config: &Config,
- ) -> Result<(String, String), io::Error> {
- let mut f = File::open(filename)?;
- let mut ori_text = String::new();
- f.read_to_string(&mut ori_text)?;
- let mut v = Vec::new();
- write_system_newlines(&mut v, text, config)?;
- let fmt_text = String::from_utf8(v).unwrap();
- Ok((ori_text, fmt_text))
- }
-
- fn create_diff(
- filename: &str,
- text: &str,
- config: &Config,
- ) -> Result<Vec<Mismatch>, io::Error> {
- let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
- Ok(make_diff(&ori, &fmt, 3))
- }
-
- match config.write_mode() {
- WriteMode::Replace => {
- if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
- if fmt != ori {
- // Do a little dance to make writing safer - write to a temp file
- // rename the original to a .bk, then rename the temp file to the
- // original.
- let tmp_name = filename.to_owned() + ".tmp";
- let bk_name = filename.to_owned() + ".bk";
- {
- // Write text to temp file
- let tmp_file = File::create(&tmp_name)?;
- write_system_newlines(tmp_file, text, config)?;
- }
+ let filename_to_path = || match *filename {
+ FileName::Real(ref path) => path,
+ _ => panic!("cannot format `{}` and emit to files", filename),
+ };