1 use std::collections::VecDeque;
13 pub lines: Vec<DiffLine>,
17 fn new(line_number: u32) -> Mismatch {
18 Mismatch { line_number: line_number, lines: Vec::new() }
22 // Produces a diff between the expected output and actual output of rustfmt.
23 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
24 let mut line_number = 1;
25 let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
26 let mut lines_since_mismatch = context_size + 1;
27 let mut results = Vec::new();
28 let mut mismatch = Mismatch::new(0);
30 for result in diff::lines(expected, actual) {
32 diff::Result::Left(str) => {
33 if lines_since_mismatch >= context_size {
34 results.push(mismatch);
35 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
38 while let Some(line) = context_queue.pop_front() {
39 mismatch.lines.push(DiffLine::Context(line.to_owned()));
42 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
43 lines_since_mismatch = 0;
45 diff::Result::Right(str) => {
46 if lines_since_mismatch >= context_size {
47 results.push(mismatch);
48 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
51 while let Some(line) = context_queue.pop_front() {
52 mismatch.lines.push(DiffLine::Context(line.to_owned()));
55 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
57 lines_since_mismatch = 0;
59 diff::Result::Both(str, _) => {
60 if context_queue.len() >= context_size {
61 let _ = context_queue.pop_front();
64 if lines_since_mismatch < context_size {
65 mismatch.lines.push(DiffLine::Context(str.to_owned()));
67 context_queue.push_back(str);
71 lines_since_mismatch += 1;
76 results.push(mismatch);
82 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
83 where F: Fn(u32) -> String
85 let mut t = term::stdout().unwrap();
86 for mismatch in diff {
87 t.fg(term::color::BRIGHT_WHITE).unwrap();
88 let title = get_section_title(mismatch.line_number);
89 writeln!(t, "{}", title).unwrap();
91 for line in mismatch.lines {
93 DiffLine::Context(ref str) => {
94 t.fg(term::color::WHITE).unwrap();
95 writeln!(t, " {}⏎", str).unwrap();
97 DiffLine::Expected(ref str) => {
98 t.fg(term::color::GREEN).unwrap();
99 writeln!(t, "+{}⏎", str).unwrap();
101 DiffLine::Resulting(ref str) => {
102 t.fg(term::color::RED).unwrap();
103 writeln!(t, "-{}⏎", str).unwrap();