1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::*;
use crate::config::Config;
use crate::rustfmt_diff::{make_diff, print_diff};
pub(crate) struct DiffEmitter {
config: Config,
}
impl DiffEmitter {
pub(crate) fn new(config: Config) -> Self {
Self { config }
}
}
impl Emitter for DiffEmitter {
fn emit_formatted_file(
&mut self,
output: &mut dyn Write,
FormattedFile {
filename,
original_text,
formatted_text,
}: FormattedFile<'_>,
) -> Result<EmitterResult, io::Error> {
const CONTEXT_SIZE: usize = 3;
let mismatch = make_diff(original_text, formatted_text, CONTEXT_SIZE);
let has_diff = !mismatch.is_empty();
if has_diff {
if self.config.print_misformatted_file_names() {
writeln!(output, "{}", filename)?;
} else {
print_diff(
mismatch,
|line_num| format!("Diff in {} at line {}:", filename, line_num),
&self.config,
);
}
} else if original_text != formatted_text {
writeln!(output, "Incorrect newline style in {}", filename)?;
return Ok(EmitterResult { has_diff: true });
}
Ok(EmitterResult { has_diff })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::FileName;
use std::path::PathBuf;
#[test]
fn does_not_print_when_no_files_reformatted() {
let mut writer = Vec::new();
let config = Config::default();
let mut emitter = DiffEmitter::new(config);
let result = emitter
.emit_formatted_file(
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
original_text: "fn empty() {}\n",
formatted_text: "fn empty() {}\n",
},
)
.unwrap();
assert_eq!(result.has_diff, false);
assert_eq!(writer.len(), 0);
}
#[test]
fn prints_file_names_when_config_is_enabled() {
let bin_file = "src/bin.rs";
let bin_original = "fn main() {\nprintln!(\"Hello, world!\");\n}";
let bin_formatted = "fn main() {\n println!(\"Hello, world!\");\n}";
let lib_file = "src/lib.rs";
let lib_original = "fn greet() {\nprintln!(\"Greetings!\");\n}";
let lib_formatted = "fn greet() {\n println!(\"Greetings!\");\n}";
let mut writer = Vec::new();
let mut config = Config::default();
config.set().print_misformatted_file_names(true);
let mut emitter = DiffEmitter::new(config);
let _ = emitter
.emit_formatted_file(
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: bin_original,
formatted_text: bin_formatted,
},
)
.unwrap();
let _ = emitter
.emit_formatted_file(
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: lib_original,
formatted_text: lib_formatted,
},
)
.unwrap();
assert_eq!(
String::from_utf8(writer).unwrap(),
format!("{}\n{}\n", bin_file, lib_file),
)
}
#[test]
fn prints_newline_message_with_only_newline_style_diff() {
let mut writer = Vec::new();
let config = Config::default();
let mut emitter = DiffEmitter::new(config);
let _ = emitter
.emit_formatted_file(
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
original_text: "fn empty() {}\n",
formatted_text: "fn empty() {}\r\n",
},
)
.unwrap();
assert_eq!(
String::from_utf8(writer).unwrap(),
String::from("Incorrect newline style in src/lib.rs\n")
);
}
}