Print fix results as a single JSON object. ```json { "totals": { "fixes": 3, "dry_run": false }, "files": { "src/Foo.php": { "fixes": 1, "changes": [ { "line": 5, "rule": "unused_import", "description": "..." } ] } } } ```
(results: &[FileFixResult], total_fixes: usize, dry_run: bool)
| 617 | /// } |
| 618 | /// ``` |
| 619 | fn print_fix_json(results: &[FileFixResult], total_fixes: usize, dry_run: bool) { |
| 620 | let mut out = String::from("{\n"); |
| 621 | let _ = writeln!( |
| 622 | out, |
| 623 | " \"totals\": {{ \"fixes\": {}, \"dry_run\": {} }},", |
| 624 | total_fixes, dry_run |
| 625 | ); |
| 626 | |
| 627 | if results.is_empty() { |
| 628 | out.push_str(" \"files\": {}\n"); |
| 629 | } else { |
| 630 | out.push_str(" \"files\": {\n"); |
| 631 | for (i, result) in results.iter().enumerate() { |
| 632 | let _ = write!( |
| 633 | out, |
| 634 | " {}: {{\n \"fixes\": {},\n \"changes\": [\n", |
| 635 | crate::analyse::json_escape(&result.display_path), |
| 636 | result.fixes.len() |
| 637 | ); |
| 638 | for (j, fix) in result.fixes.iter().enumerate() { |
| 639 | let _ = write!( |
| 640 | out, |
| 641 | " {{ \"line\": {}, \"rule\": {}, \"description\": {} }}", |
| 642 | fix.line, |
| 643 | crate::analyse::json_escape(&fix.rule), |
| 644 | crate::analyse::json_escape(&fix.description), |
| 645 | ); |
| 646 | if j + 1 < result.fixes.len() { |
| 647 | out.push(','); |
| 648 | } |
| 649 | out.push('\n'); |
| 650 | } |
| 651 | out.push_str(" ]\n }"); |
| 652 | if i + 1 < results.len() { |
| 653 | out.push(','); |
| 654 | } |
| 655 | out.push('\n'); |
| 656 | } |
| 657 | out.push_str(" }\n"); |
| 658 | } |
| 659 | |
| 660 | out.push('}'); |
| 661 | println!("{out}"); |
| 662 | } |
| 663 | |
| 664 | const BAR_WIDTH: usize = 28; |
| 665 |