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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use super::*;
use crate::rustfmt_diff::{make_diff, DiffLine, Mismatch};
use serde::Serialize;
use serde_json::to_string as to_json_string;
use std::io::{self, Write};

#[derive(Debug, Default)]
pub(crate) struct JsonEmitter {
    mismatched_files: Vec<MismatchedFile>,
}

#[derive(Debug, Default, PartialEq, Serialize)]
struct MismatchedBlock {
    original_begin_line: u32,
    original_end_line: u32,
    expected_begin_line: u32,
    expected_end_line: u32,
    original: String,
    expected: String,
}

#[derive(Debug, Default, PartialEq, Serialize)]
struct MismatchedFile {
    name: String,
    mismatches: Vec<MismatchedBlock>,
}

impl Emitter for JsonEmitter {
    fn emit_footer(&self, output: &mut dyn Write) -> Result<(), io::Error> {
        writeln!(output, "{}", &to_json_string(&self.mismatched_files)?)
    }

    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 = 0;
        let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE);
        let has_diff = !diff.is_empty();

        if has_diff {
            self.add_misformatted_file(filename, diff)?;
        }

        Ok(EmitterResult { has_diff })
    }
}

impl JsonEmitter {
    fn add_misformatted_file(
        &mut self,
        filename: &FileName,
        diff: Vec<Mismatch>,
    ) -> Result<(), io::Error> {
        let mut mismatches = vec![];
        for mismatch in diff {
            let original_begin_line = mismatch.line_number_orig;
            let expected_begin_line = mismatch.line_number;
            let mut original_end_line = original_begin_line;
            let mut expected_end_line = expected_begin_line;
            let mut original_line_counter = 0;
            let mut expected_line_counter = 0;
            let mut original = String::new();
            let mut expected = String::new();

            for line in mismatch.lines {
                match line {
                    DiffLine::Expected(msg) => {
                        expected_end_line = expected_begin_line + expected_line_counter;
                        expected_line_counter += 1;
                        expected.push_str(&msg);
                        expected.push('\n');
                    }
                    DiffLine::Resulting(msg) => {
                        original_end_line = original_begin_line + original_line_counter;
                        original_line_counter += 1;
                        original.push_str(&msg);
                        original.push('\n');
                    }
                    DiffLine::Context(_) => continue,
                }
            }

            mismatches.push(MismatchedBlock {
                original_begin_line,
                original_end_line,
                expected_begin_line,
                expected_end_line,
                original,
                expected,
            });
        }
        self.mismatched_files.push(MismatchedFile {
            name: format!("{}", filename),
            mismatches,
        });
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::FileName;
    use std::path::PathBuf;

    #[test]
    fn expected_line_range_correct_when_single_line_split() {
        let mut emitter = JsonEmitter {
            mismatched_files: vec![],
        };
        let file = "foo/bar.rs";
        let mismatched_file = MismatchedFile {
            name: String::from(file),
            mismatches: vec![MismatchedBlock {
                original_begin_line: 79,
                original_end_line: 79,
                expected_begin_line: 79,
                expected_end_line: 82,
                original: String::from("fn Foo<T>() where T: Bar {\n"),
                expected: String::from("fn Foo<T>()\nwhere\n    T: Bar,\n{\n"),
            }],
        };
        let mismatch = Mismatch {
            line_number: 79,
            line_number_orig: 79,
            lines: vec![
                DiffLine::Resulting(String::from("fn Foo<T>() where T: Bar {")),
                DiffLine::Expected(String::from("fn Foo<T>()")),
                DiffLine::Expected(String::from("where")),
                DiffLine::Expected(String::from("    T: Bar,")),
                DiffLine::Expected(String::from("{")),
            ],
        };

        let _ = emitter
            .add_misformatted_file(&FileName::Real(PathBuf::from(file)), vec![mismatch])
            .unwrap();

        assert_eq!(emitter.mismatched_files.len(), 1);
        assert_eq!(emitter.mismatched_files[0], mismatched_file);
    }

    #[test]
    fn context_lines_ignored() {
        let mut emitter = JsonEmitter {
            mismatched_files: vec![],
        };
        let file = "src/lib.rs";
        let mismatched_file = MismatchedFile {
            name: String::from(file),
            mismatches: vec![MismatchedBlock {
                original_begin_line: 5,
                original_end_line: 5,
                expected_begin_line: 5,
                expected_end_line: 5,
                original: String::from(
                    "fn foo(_x: &u64) -> Option<&(dyn::std::error::Error + 'static)> {\n",
                ),
                expected: String::from(
                    "fn foo(_x: &u64) -> Option<&(dyn ::std::error::Error + 'static)> {\n",
                ),
            }],
        };
        let mismatch = Mismatch {
            line_number: 5,
            line_number_orig: 5,
            lines: vec![
                DiffLine::Context(String::new()),
                DiffLine::Resulting(String::from(
                    "fn foo(_x: &u64) -> Option<&(dyn::std::error::Error + 'static)> {",
                )),
                DiffLine::Context(String::new()),
                DiffLine::Expected(String::from(
                    "fn foo(_x: &u64) -> Option<&(dyn ::std::error::Error + 'static)> {",
                )),
                DiffLine::Context(String::new()),
            ],
        };

        let _ = emitter
            .add_misformatted_file(&FileName::Real(PathBuf::from(file)), vec![mismatch])
            .unwrap();

        assert_eq!(emitter.mismatched_files.len(), 1);
        assert_eq!(emitter.mismatched_files[0], mismatched_file);
    }

    #[test]
    fn emits_empty_array_on_no_diffs() {
        let mut writer = Vec::new();
        let mut emitter = JsonEmitter::default();
        let _ = emitter.emit_header(&mut writer);
        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();
        let _ = emitter.emit_footer(&mut writer);
        assert_eq!(result.has_diff, false);
        assert_eq!(&writer[..], "[]\n".as_bytes());
    }

    #[test]
    fn emits_array_with_files_with_diffs() {
        let file_name = "src/bin.rs";
        let original = vec![
            "fn main() {",
            "println!(\"Hello, world!\");",
            "}",
            "",
            "#[cfg(test)]",
            "mod tests {",
            "#[test]",
            "fn it_works() {",
            "    assert_eq!(2 + 2, 4);",
            "}",
            "}",
        ];
        let formatted = vec![
            "fn main() {",
            "    println!(\"Hello, world!\");",
            "}",
            "",
            "#[cfg(test)]",
            "mod tests {",
            "    #[test]",
            "    fn it_works() {",
            "        assert_eq!(2 + 2, 4);",
            "    }",
            "}",
        ];
        let mut writer = Vec::new();
        let mut emitter = JsonEmitter::default();
        let _ = emitter.emit_header(&mut writer);
        let result = emitter
            .emit_formatted_file(
                &mut writer,
                FormattedFile {
                    filename: &FileName::Real(PathBuf::from(file_name)),
                    original_text: &original.join("\n"),
                    formatted_text: &formatted.join("\n"),
                },
            )
            .unwrap();
        let _ = emitter.emit_footer(&mut writer);
        let exp_json = to_json_string(&vec![MismatchedFile {
            name: String::from(file_name),
            mismatches: vec![
                MismatchedBlock {
                    original_begin_line: 2,
                    original_end_line: 2,
                    expected_begin_line: 2,
                    expected_end_line: 2,
                    original: String::from("println!(\"Hello, world!\");\n"),
                    expected: String::from("    println!(\"Hello, world!\");\n"),
                },
                MismatchedBlock {
                    original_begin_line: 7,
                    original_end_line: 10,
                    expected_begin_line: 7,
                    expected_end_line: 10,
                    original: String::from(
                        "#[test]\nfn it_works() {\n    assert_eq!(2 + 2, 4);\n}\n",
                    ),
                    expected: String::from(
                        "    #[test]\n    fn it_works() {\n        assert_eq!(2 + 2, 4);\n    }\n",
                    ),
                },
            ],
        }])
        .unwrap();
        assert_eq!(result.has_diff, true);
        assert_eq!(&writer[..], format!("{}\n", exp_json).as_bytes());
    }

    #[test]
    fn emits_valid_json_with_multiple_files() {
        let bin_file = "src/bin.rs";
        let bin_original = vec!["fn main() {", "println!(\"Hello, world!\");", "}"];
        let bin_formatted = vec!["fn main() {", "    println!(\"Hello, world!\");", "}"];
        let lib_file = "src/lib.rs";
        let lib_original = vec!["fn greet() {", "println!(\"Greetings!\");", "}"];
        let lib_formatted = vec!["fn greet() {", "    println!(\"Greetings!\");", "}"];
        let mut writer = Vec::new();
        let mut emitter = JsonEmitter::default();
        let _ = emitter.emit_header(&mut writer);
        let _ = emitter
            .emit_formatted_file(
                &mut writer,
                FormattedFile {
                    filename: &FileName::Real(PathBuf::from(bin_file)),
                    original_text: &bin_original.join("\n"),
                    formatted_text: &bin_formatted.join("\n"),
                },
            )
            .unwrap();
        let _ = emitter
            .emit_formatted_file(
                &mut writer,
                FormattedFile {
                    filename: &FileName::Real(PathBuf::from(lib_file)),
                    original_text: &lib_original.join("\n"),
                    formatted_text: &lib_formatted.join("\n"),
                },
            )
            .unwrap();
        let _ = emitter.emit_footer(&mut writer);
        let exp_bin = MismatchedFile {
            name: String::from(bin_file),
            mismatches: vec![MismatchedBlock {
                original_begin_line: 2,
                original_end_line: 2,
                expected_begin_line: 2,
                expected_end_line: 2,
                original: String::from("println!(\"Hello, world!\");\n"),
                expected: String::from("    println!(\"Hello, world!\");\n"),
            }],
        };

        let exp_lib = MismatchedFile {
            name: String::from(lib_file),
            mismatches: vec![MismatchedBlock {
                original_begin_line: 2,
                original_end_line: 2,
                expected_begin_line: 2,
                expected_end_line: 2,
                original: String::from("println!(\"Greetings!\");\n"),
                expected: String::from("    println!(\"Greetings!\");\n"),
            }],
        };

        let exp_json = to_json_string(&vec![exp_bin, exp_lib]).unwrap();
        assert_eq!(&writer[..], format!("{}\n", exp_json).as_bytes());
    }
}