dover 0.2.4

A CLI tool for summarizing git diffs of Rust code
Documentation
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
use std::fmt::{Display, Write};
use std::fs;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result};
use colored::Colorize;
use overview::enums::{Enum, Enums, EnumsDiff};
use overview::functions::{Functions, FunctionsDiff};
use overview::impls::{Impls, ImplsDiff};
use overview::traits::{Trait, Traits, TraitsDiff};
use syn::spanned::Spanned;
use syn::{File, Item, ItemFn};
use syn::{ItemUse, Visibility};

use overview::structs::{Struct, Structs, StructsDiff};
use overview::uses::{self, Uses, UsesDiff};

mod git;
mod html;
mod overview;

pub use git::{Change as GitChange, ChangedFile, Treeish, get_changed_files};
pub use html::HTML_BOILERPLATE;

const DEFAULT_MAX_COL_W: usize = 50;
const ASCII_LINE_FEED: u8 = 10;

pub trait ByteRange {
    fn old_ranges(&self) -> Vec<Range<usize>>;
    fn new_ranges(&self) -> Vec<Range<usize>>;
}

/// Diff an item with another and return the result.
pub trait Diff {
    type Diff;
    fn diff_with(&self, other: &Self) -> Self::Diff;
}

pub trait View {
    fn as_viewable(&self) -> ViewableDiffs;
}

pub trait Html {
    fn to_html(&self) -> String;
}

#[derive(Debug)]
pub struct ViewableDiffs {
    vds: Vec<ViewableDiff>,
}
impl ViewableDiffs {
    pub fn new(diffs: Vec<ViewableDiff>) -> Self {
        ViewableDiffs { vds: diffs }
    }

    pub fn is_empty(&self) -> bool {
        self.vds.is_empty()
    }

    pub fn empty() -> ViewableDiffs {
        Self { vds: Vec::new() }
    }

    pub fn append(&mut self, mut diffs: ViewableDiffs) {
        self.vds.append(&mut diffs.vds);
    }

    pub fn appendln(&mut self, mut diffs: ViewableDiffs) {
        self.vds.append(&mut diffs.vds);
        self.vds.push(ViewableDiff::newline());
    }

    pub fn collapse(&mut self) {
        if self.vds.is_empty() {
            return;
        }

        let mut collapsed_old = Vec::new();
        let mut collapsed_new = Vec::new();

        for diff in self.vds.iter_mut() {
            if let Some(ref mut old) = diff.old {
                collapsed_old.append(old);
                collapsed_old.push((None, Code("\n".to_string())));
            }
            if let Some(ref mut new) = diff.new {
                collapsed_new.append(new);
                collapsed_new.push((None, Code("\n".to_string())));
            }
        }

        let mut old = None;
        let mut new = None;

        if !collapsed_old.is_empty() {
            old = Some(collapsed_old);
        }
        if !collapsed_new.is_empty() {
            new = Some(collapsed_new);
        }

        self.vds = vec![ViewableDiff { old, new }];
    }
}
impl Html for ViewableDiffs {
    fn to_html(&self) -> String {
        let mut html = String::new();

        for vd in self.vds.iter() {
            html.push_str("<tr>");

            let mut deleted_content = String::new();
            if let Some(ref old) = vd.old {
                for diff in old.iter() {
                    let class = match diff.0 {
                        Some(ExistenceChange::Deleted) => "deleted",
                        Some(ExistenceChange::Added) => unreachable!(),
                        None => "",
                    };
                    deleted_content.push_str(&format!(
                        "<span class=\"{}\">{}</span>",
                        class,
                        escape_html(&diff.1.to_string())
                    ));
                }
            }
            if deleted_content.is_empty() {
                html.push_str("<td class=\"empty-content\">");
                html.push_str("</td>");
            } else {
                html.push_str("<td>");
                html.push_str("<pre><code>");
                html.push_str(&deleted_content);
                html.push_str("</code></pre>");
                html.push_str("</td>");
            }

            let mut added_content = String::new();

            if let Some(ref new) = vd.new {
                for diff in new.iter() {
                    let class = match diff.0 {
                        Some(ExistenceChange::Deleted) => unreachable!(),
                        Some(ExistenceChange::Added) => "added",
                        None => "",
                    };
                    added_content.push_str(&format!(
                        "<span class=\"{}\">{}</span>",
                        class,
                        escape_html(&diff.1.to_string())
                    ));
                }
            }
            if added_content.is_empty() {
                html.push_str("<td class=\"empty-content\">");
                html.push_str("</td>");
            } else {
                html.push_str("<td>");
                html.push_str("<pre><code>");
                html.push_str(&added_content);
                html.push_str("</code></pre>");
                html.push_str("</td>");
            }
        }

        // let html_with_br = html.replace("\n", "<br>");

        // html_with_br
        html
    }
}

impl Display for ViewableDiffs {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // dbg!(self);

        // ---------------------------------------------------------------------------------
        //  None of this is optimized for readability or efficiency. It's barely working.  |
        //                                                                                 |
        //   Edit: A recent bug reminded me how terrible this section is to work in. It    |
        //         needs a complete rewrite.                                               |
        // ---------------------------------------------------------------------------------
        let (mut old_col, mut new_col) = (Vec::new(), Vec::new());

        let old_col_max_width = {
            let mut cur_max = DEFAULT_MAX_COL_W;
            for vd in self.vds.iter() {
                if let Some(ref old) = vd.old {
                    let all_strings: Vec<_> = old.iter().map(|(_, c)| c.0.clone()).collect();
                    let string = all_strings.join("");
                    let local_max = string.lines().map(|l| l.len() + 2).max().unwrap_or(2);
                    cur_max = cur_max.max(local_max);
                }
            }
            cur_max
        };

        for vd in self.vds.iter() {
            let (mut old_section, mut new_section) = (Vec::new(), Vec::new());
            if let Some(old) = &vd.old {
                let mut output_lines = Vec::new();
                let mut line_of_spans = (false, String::new());
                let mut line_of_spans_unformatted_len = 2;

                for (change, code) in old {
                    if code.0.contains("\n") {
                        let mut code_lines = code.0.lines().peekable();
                        let next_span = code_lines.next().unwrap();
                        match change {
                            Some(ExistenceChange::Deleted) => {
                                // println!("writing old del(1) {}", next.red());
                                line_of_spans_unformatted_len += next_span.len();
                                write!(line_of_spans.1, "{}", next_span.red())?;
                                line_of_spans.0 = true;
                            }
                            Some(ExistenceChange::Added) => panic!(),
                            None => {
                                // println!("writing old nil(1) {}", next.normal());
                                line_of_spans_unformatted_len += next_span.len();
                                write!(line_of_spans.1, "{}", next_span.normal())?;
                            }
                        }
                        // println!("pushing old(1) {running_string}");
                        while line_of_spans_unformatted_len < old_col_max_width {
                            line_of_spans.1.push(' ');
                            line_of_spans_unformatted_len += 1;
                        }
                        output_lines.push(format!(
                            "{} {}",
                            if line_of_spans.0 {
                                "-".red()
                            } else {
                                " ".red()
                            },
                            line_of_spans.1.clone()
                        ));
                        line_of_spans.1.clear();
                        line_of_spans.0 = false;

                        line_of_spans_unformatted_len = 2;
                        while let Some(line) = code_lines.next() {
                            match change {
                                Some(ExistenceChange::Deleted) => {
                                    if code_lines.peek().is_some()
                                        || (code_lines.peek().is_none() && code.0.ends_with('\n'))
                                    {
                                        // println!("pushing old del(1) {}", line.red());
                                        let gap = old_col_max_width.saturating_sub(line.len());
                                        let full_line_span =
                                            format!("{} {line}{}", "-".red(), " ".repeat(gap));
                                        output_lines.push(full_line_span.red().to_string());
                                    } else {
                                        // the last piece and not terminated with \n
                                        // println!("writing old del (2){}", line.red());
                                        line_of_spans_unformatted_len += line.len();
                                        write!(line_of_spans.1, "{}", line.red())?;
                                        line_of_spans.0 = true;
                                    }
                                }
                                Some(ExistenceChange::Added) => panic!(),
                                None => {
                                    if code_lines.peek().is_some()
                                        || (code_lines.peek().is_none() && code.0.ends_with('\n'))
                                    {
                                        // println!("pushing old nil(1) {}", line.normal());
                                        let gap = old_col_max_width.saturating_sub(line.len());
                                        let line = format!("  {line}{}", " ".repeat(gap));
                                        output_lines.push(line.normal().to_string())
                                    } else {
                                        // the last piece and not terminated with \n
                                        // println!("writing old nil (2){}", line.normal());
                                        line_of_spans_unformatted_len += line.len();
                                        write!(line_of_spans.1, "{}", line.normal())?;
                                    }
                                }
                            }
                        }
                    } else {
                        match change {
                            Some(ExistenceChange::Deleted) => {
                                // println!("writing old del(3) {}", code.0.red());
                                line_of_spans_unformatted_len += code.0.len();
                                write!(line_of_spans.1, "{}", code.0.red())?;
                                line_of_spans.0 = true;
                            }
                            Some(ExistenceChange::Added) => panic!(),
                            None => {
                                // println!("writing old nil(3){}", code.0.normal());
                                line_of_spans_unformatted_len += code.0.len();
                                write!(line_of_spans.1, "{}", code.0.normal())?;
                            }
                        }
                    }
                }

                if !line_of_spans.1.is_empty() {
                    while line_of_spans_unformatted_len < old_col_max_width {
                        line_of_spans.1.push(' ');
                        line_of_spans_unformatted_len += 1;
                    }
                    output_lines.push(format!(
                        "{} {}",
                        if line_of_spans.0 {
                            "-".red()
                        } else {
                            " ".red()
                        },
                        line_of_spans.1
                    ));
                }

                for line in output_lines.into_iter() {
                    old_section.push(line);
                }
            }

            if let Some(new) = &vd.new {
                let mut output_lines = Vec::new();
                let mut line_of_spans = (false, String::new());
                for (change, code) in new {
                    if code.0.contains("\n") {
                        let mut code_lines = code.0.lines().peekable();
                        let next_span = code_lines.next().unwrap();
                        match change {
                            Some(ExistenceChange::Added) => {
                                // println!("writing new add(1){}", next.green());
                                write!(line_of_spans.1, "{}", next_span.green())?;
                                line_of_spans.0 = true;
                            }
                            Some(ExistenceChange::Deleted) => panic!(),
                            None => {
                                // println!("writing new del(1){}", next.normal());
                                write!(line_of_spans.1, "{}", next_span.normal())?;
                            }
                        }
                        // println!("pushing new(1) {running_string}");
                        output_lines.push(format!(
                            "{} {}",
                            if line_of_spans.0 {
                                "+".green()
                            } else {
                                " ".green()
                            },
                            line_of_spans.1.clone()
                        ));
                        line_of_spans.1.clear();
                        line_of_spans.0 = false;

                        while let Some(full_line_span) = code_lines.next() {
                            match change {
                                Some(ExistenceChange::Added) => {
                                    if code_lines.peek().is_some()
                                        || (code_lines.peek().is_none() && code.0.ends_with('\n'))
                                    {
                                        // println!("pushing new add(2){}", line.green());
                                        output_lines.push(format!(
                                            "{} {}",
                                            "+".green(),
                                            full_line_span.green()
                                        ));
                                    } else {
                                        // the last piece and not terminated with \n
                                        // println!("writing new add(2){}", line.green());
                                        write!(line_of_spans.1, "{}", full_line_span.green())?;
                                        line_of_spans.0 = true;
                                    }
                                }
                                Some(ExistenceChange::Deleted) => panic!(),
                                None => {
                                    if code_lines.peek().is_some()
                                        || (code_lines.peek().is_none() && code.0.ends_with('\n'))
                                    {
                                        // println!("pushing {}", line.normal());
                                        output_lines.push(full_line_span.normal().to_string())
                                    } else {
                                        // the last piece and not terminated with \n
                                        // println!("writing new nil (2){}", line.normal());
                                        write!(line_of_spans.1, "{}", full_line_span.normal())?;
                                    }
                                }
                            }
                        }
                    } else {
                        match change {
                            Some(ExistenceChange::Added) => {
                                // println!("writing {}", code.0.green());
                                write!(line_of_spans.1, "{}", code.0.green())?;
                                line_of_spans.0 = true;
                            }
                            Some(ExistenceChange::Deleted) => panic!(),
                            None => {
                                // println!("writing {}", code.0.normal());
                                write!(line_of_spans.1, "{}", code.0.normal())?;
                            }
                        }
                    }
                }

                if !line_of_spans.1.is_empty() {
                    output_lines.push(format!(
                        "{} {}",
                        if line_of_spans.0 {
                            "+".green()
                        } else {
                            " ".green()
                        },
                        line_of_spans.1
                    ));
                }

                for line in output_lines.into_iter() {
                    new_section.push(line);
                }
            }

            while old_section.len() < new_section.len() {
                old_section.push(" ".repeat(old_col_max_width));
            }

            while new_section.len() < old_section.len() {
                new_section.push(String::new());
            }

            assert!(!(old_section.is_empty() || new_section.is_empty()));
            assert_eq!(old_section.len(), new_section.len());

            old_section.push(" ".repeat(old_col_max_width));
            new_section.push(String::new());

            // dbg!(&old_section);
            // dbg!(&new_section);

            old_col.append(&mut old_section);
            new_col.append(&mut new_section);
        }

        assert_eq!(old_col.len(), new_col.len());
        let left_right = old_col.iter().zip(new_col.iter());
        let mut formatted_output = String::new();

        for (left, right) in left_right {
            let format_str = format!("{left}      {right}\n");
            formatted_output.push_str(&format_str);
        }

        write!(f, "{}", formatted_output.trim_end())
    }
}

#[derive(Debug)]
pub struct ViewableDiff {
    old: Option<Vec<(Option<ExistenceChange>, Code)>>,
    new: Option<Vec<(Option<ExistenceChange>, Code)>>,
}
impl ViewableDiff {
    fn newline() -> ViewableDiff {
        ViewableDiff {
            old: Some(vec![(None, Code("\n".to_string()))]),
            new: Some(vec![(None, Code("\n".to_string()))]),
        }
    }
}
impl Display for ViewableDiff {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum Change {
    #[default]
    Modified,
    Existence(ExistenceChange),
}
impl Display for Change {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Change::Modified => write!(f, "~"),
            Change::Existence(ex) => write!(f, "{ex}"),
        }
    }
}
impl From<ExistenceChange> for Change {
    fn from(existence: ExistenceChange) -> Self {
        Change::Existence(existence)
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Copy)]
pub enum ExistenceChange {
    Added,
    Deleted,
}
impl Display for ExistenceChange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExistenceChange::Added => write!(f, "+"),
            ExistenceChange::Deleted => write!(f, "-"),
        }
    }
}

#[derive(Debug)]
pub struct Code(String);
impl Display for Code {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

fn get_overview(path: PathBuf, source: String) -> Result<Overview> {
    let file: File = syn::parse_file(&source).context("Error parsing {path}")?;
    let source = SourceFile::from(source);
    let mut use_statements = Vec::new();
    let mut functions = Vec::new();
    let mut structs = Vec::new();
    let mut enums = Vec::new();
    let mut traits = Vec::new();
    let mut impls = Vec::new();

    for item in file.items {
        match item {
            Item::Use(item_use @ ItemUse { .. }) => {
                use_statements.push(item_use);
            }
            Item::Fn(item_fn @ ItemFn { .. }) => {
                functions.push(item_fn);
            }
            Item::Struct(item_struct) => {
                structs.push(item_struct);
            }
            Item::Enum(item_enum) => {
                enums.push(item_enum);
            }
            Item::Trait(item_trait) => {
                traits.push(item_trait);
            }
            Item::Impl(item_impl) => {
                impls.push(item_impl);
            }
            _ => {}
        }
    }

    let traits = traits
        .into_iter()
        .map(|t| Trait::new(t, source.clone()))
        .collect();
    let traits = Traits::from(traits);

    let structs = structs
        .into_iter()
        .map(|s| Struct::new(s, source.clone()))
        .collect();
    let structs = Structs::from(structs);

    let enums = enums
        .into_iter()
        .map(|e| Enum::new(e, source.clone()))
        .collect();
    let enums = Enums::from(enums);

    let functions = Functions::new_freestanding(functions, source.clone());
    let impls = Impls::new(impls, source);

    let mut use_paths = Vec::new();
    for r#use in use_statements.iter() {
        // let visibility = import.vis;
        let tree = &r#use.tree;

        let paths = uses::get_paths_from_usetree(tree);
        use_paths.extend(paths.into_iter());
    }

    let overview = Overview {
        path,
        uses: Uses::from(use_paths),
        structs,
        enums,
        traits,
        functions,
        impls,
    };
    Ok(overview)
}

#[derive(Debug)]
pub struct Overview {
    path: PathBuf,
    uses: Uses,
    structs: Structs,
    enums: Enums,
    traits: Traits,
    functions: Functions,
    impls: Impls,
}
impl Overview {
    pub fn uses(&self) -> &Uses {
        &self.uses
    }
}
impl TryFrom<(PathBuf, String)> for Overview {
    type Error = anyhow::Error;

    fn try_from((path, contents): (PathBuf, String)) -> std::result::Result<Self, Self::Error> {
        get_overview(path, contents)
    }
}
impl TryFrom<PathBuf> for Overview {
    type Error = anyhow::Error;

    fn try_from(path: PathBuf) -> std::result::Result<Self, Self::Error> {
        let contents = fs::read_to_string(&path).context("Error reading file at {path}")?;
        get_overview(path, contents)
    }
}
impl Display for Overview {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let formatted_path = formatted_path(&self.path);
        writeln!(f, "{formatted_path}")?;

        if !self.uses.0.is_empty() {
            writeln!(f, "Imports:")?;
            for import in self.uses.0.iter() {
                writeln!(f, "{import}")?;
            }
        }

        if !self.structs.is_empty() {
            writeln!(f, "\nStructs:")?;
            for st in self.structs.iter() {
                writeln!(f, "{st}")?;
            }
        }

        if !self.enums.is_empty() {
            writeln!(f, "\nEnums:")?;
            for en in self.enums.iter() {
                writeln!(f, "{en}")?;
            }
        }

        if !self.traits.is_empty() {
            writeln!(f, "\nTraits:")?;
            for tr in self.traits.iter() {
                writeln!(f, "{tr}")?;
            }
        }

        if !self.functions.is_empty() {
            writeln!(f, "\nFunctions:")?;
            for func in self.functions.functions().iter() {
                writeln!(f, "{func}")?;
            }
        }

        if !self.impls.is_empty() {
            for imp in self.impls.impls().iter() {
                writeln!(f, "{imp}")?;
            }
        }

        Ok(())
    }
}
impl Diff for Overview {
    type Diff = OverviewDiff;
    fn diff_with(&self, other: &Self) -> Self::Diff {
        let uses_diff = self.uses.diff_with(&other.uses);
        let structs_diff = self.structs.diff_with(&other.structs);
        let enums_diff = self.enums.diff_with(&other.enums);
        let traits_diff = self.traits.diff_with(&other.traits);
        let functions_diff = self.functions.diff_with(&other.functions);
        let impls_diff = self.impls.diff_with(&other.impls);
        let file1 = self.path.clone();
        let file2 = other.path.clone();

        OverviewDiff {
            file1,
            file2,
            uses_diff,
            structs_diff,
            enums_diff,
            traits_diff,
            functions_diff,
            impls_diff,
        }
    }
}

pub struct OverviewDiff {
    file1: PathBuf,
    file2: PathBuf,
    uses_diff: UsesDiff,
    structs_diff: StructsDiff,
    enums_diff: EnumsDiff,
    traits_diff: TraitsDiff,
    functions_diff: FunctionsDiff,
    impls_diff: ImplsDiff,
}
impl OverviewDiff {
    pub fn all_empty(&self) -> bool {
        self.uses_diff.is_empty()
            && self.structs_diff.is_empty()
            && self.enums_diff.is_empty()
            && self.traits_diff.is_empty()
            && self.functions_diff.is_empty()
            && self.impls_diff.is_empty()
    }
}
impl Html for OverviewDiff {
    fn to_html(&self) -> String {
        let mut html = "<table>".to_string();
        html.push_str(&format!(
            "<tr><th colspan=\"2\" class=\"filename\">{}</th></tr>",
            self.file1.display()
        ));

        let viewable_uses = self.uses_diff.as_viewable();
        if !viewable_uses.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Uses</th></tr>");
            html.push_str(&viewable_uses.to_html());
        }

        let viewable_structs = self.structs_diff.as_viewable();
        if !viewable_structs.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Structs</th></tr>");
            html.push_str(&viewable_structs.to_html());
        }

        let viewable_enums = self.enums_diff.as_viewable();
        if !viewable_enums.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Enums</th></tr>");
            html.push_str(&viewable_enums.to_html());
        }

        let viewable_traits = self.traits_diff.as_viewable();
        if !viewable_traits.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Traits</th></tr>");
            html.push_str(&viewable_traits.to_html());
        }

        let viewable_functions = self.functions_diff.as_viewable();
        if !viewable_functions.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Functions</th></tr>");
            html.push_str(&viewable_functions.to_html());
        }

        let viewable_impls = self.impls_diff.as_viewable();
        if !viewable_impls.is_empty() {
            html.push_str("<tr><th colspan=\"2\">Impls</th></tr>");
            html.push_str(&viewable_impls.to_html());
        }

        html
    }
}

impl Display for OverviewDiff {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.all_empty() {
            return Ok(());
        }

        const MAX_HEADER_WIDTH: usize = 40;

        let fp1 = &self.file1.to_str().unwrap();
        let fp2 = &self.file2.to_str().unwrap();
        let header = underlined(&format!("{fp1} -> {fp2}"));
        let mut string_builder = String::new();
        writeln!(&mut string_builder, "{header}")?;

        if !self.uses_diff.is_empty() {
            let viewable_uses = self.uses_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "{}",
                underlined(&format!("Use{}", " ".repeat(MAX_HEADER_WIDTH - 3)))
            )?;
            writeln!(&mut string_builder, "{viewable_uses}")?;
        }

        if !self.structs_diff.is_empty() {
            let viewable_structs = self.structs_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "\n{}",
                underlined(&format!("Struct{}", " ".repeat(MAX_HEADER_WIDTH - 7)))
            )?;
            writeln!(&mut string_builder, "{viewable_structs}")?;
        }

        if !self.enums_diff.is_empty() {
            let viewable_enums = self.enums_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "\n{}",
                underlined(&format!("Enum{}", " ".repeat(MAX_HEADER_WIDTH - 5)))
            )?;
            writeln!(&mut string_builder, "{viewable_enums}")?;
        }

        if !self.traits_diff.is_empty() {
            let viewable_traits = self.traits_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "\n{}",
                underlined(&format!("Trait{}", " ".repeat(MAX_HEADER_WIDTH - 6)))
            )?;
            writeln!(&mut string_builder, "{viewable_traits}",)?;
        }

        if !self.functions_diff.is_empty() {
            let viewable_funcs = self.functions_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "\n{}",
                underlined(&format!("Function{}", " ".repeat(MAX_HEADER_WIDTH - 9)))
            )?;
            writeln!(&mut string_builder, "{}", viewable_funcs)?;
        }

        if !self.impls_diff.is_empty() {
            let viewable_impls = self.impls_diff.as_viewable();
            writeln!(
                &mut string_builder,
                "\n{}",
                underlined(&format!("Impl{}", " ".repeat(MAX_HEADER_WIDTH - 5)))
            )?;
            writeln!(&mut string_builder, "{viewable_impls}",)?;
        }

        while string_builder.ends_with('\n') {
            string_builder.pop().unwrap();
        }

        write!(f, "{string_builder}")
    }
}

/// Returns the pathname with an underline of the same length.
///
///  appears as:
///  foo/bar.rs
///  ¯¯¯¯¯¯¯¯¯¯
/// Panics on non-UTF-8 paths.
fn formatted_path(path: &Path) -> String {
    let path = path.to_str().unwrap();
    underlined(path)
}

/// Returns the string with an underline of the same length.
fn underlined(s: &str) -> String {
    let underline = "¯".repeat(s.len());
    format!("{s}\n{underline}")
}

/// Returns the formatted Rust source of the given items as a string.
fn get_source(items: Vec<Item>) -> String {
    let syn_file = File {
        items,
        shebang: None,
        attrs: vec![],
    };

    prettyplease::unparse(&syn_file)
}
impl Diff for Visibility {
    type Diff = Option<VisDiff>;
    fn diff_with(&self, other: &Self) -> Self::Diff {
        if self == other {
            return None;
        }

        Some(VisDiff {
            old: self.clone(),
            new: other.clone(),
        })
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct VisDiff {
    pub old: Visibility,
    pub new: Visibility,
}
impl ByteRange for VisDiff {
    fn old_ranges(&self) -> Vec<Range<usize>> {
        let old_range = self.old.span().byte_range();
        if old_range.is_empty() {
            Vec::new()
        } else {
            vec![old_range]
        }
    }

    fn new_ranges(&self) -> Vec<Range<usize>> {
        let new_range = self.new.span().byte_range();
        if new_range.is_empty() {
            Vec::new()
        } else {
            vec![new_range]
        }
    }
}

/// Cheaply cloneable reference to the original source.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
struct SourceFile(Arc<String>);
impl From<String> for SourceFile {
    fn from(value: String) -> Self {
        let source = Arc::new(value);
        SourceFile(source)
    }
}

#[macro_export]
macro_rules! collect_src_maps {
    ($($arg:expr),* $(,)?) => {{
        let mut old_src_map = Vec::new();
        let mut new_src_map = Vec::new();
        $(
            if let Some(ref diff) = $arg {
                let mut old_ranges = diff.old_ranges();
                let mut new_ranges = diff.new_ranges();

                old_ranges.retain(|r| ! r.is_empty());
                new_ranges.retain(|r| ! r.is_empty());
                if !old_ranges.is_empty() {
                    old_src_map.append(&mut old_ranges);
                }
                if !new_ranges.is_empty() {
                    new_src_map.append(&mut new_ranges);
                }

            }
        )*
        old_src_map.sort_by(|a, b| a.start.cmp(&b.start));
        old_src_map.sort_by(|a, b| a.end.cmp(&b.end));

        new_src_map.sort_by(|a, b| a.start.cmp(&b.start));
        new_src_map.sort_by(|a, b| a.end.cmp(&b.end));
        (old_src_map, new_src_map)
    }};
}

fn collect_diff_changes(
    source_code: &[u8],
    source_map: &[Range<usize>],
    decl_start: usize,
    sig_end: usize,
    ex: ExistenceChange,
) -> Vec<(Option<ExistenceChange>, Code)> {
    let mut i = decl_start;
    let mut src_i = 0;
    let mut diff_changes = Vec::new();

    while i < sig_end {
        let maybe_diff_index = source_map[src_i..].iter().position(|r| r.contains(&i));
        match maybe_diff_index {
            Some(diff_index) => {
                let diff_range = &source_map[src_i..][diff_index];

                // doesn't make sense that we wouldn't be aligned with the start of a range
                assert_eq!(i, diff_range.start);
                let substring = source_code[i..diff_range.end].to_vec();
                let code = Code(String::from_utf8(substring).expect("Off a code boundary"));

                diff_changes.push((Some(ex), code));

                src_i = diff_index + 1;
                i = diff_range.end;
            }
            None => {
                let start = i;
                while i < sig_end {
                    let maybe_diff_index = source_map[src_i..].iter().position(|r| r.contains(&i));
                    if maybe_diff_index.is_some() {
                        break;
                    } else {
                        i += 1
                    }
                }
                // We're either off the end or we've found a new diff. Either way,
                // start..i contains our next range
                let substring = source_code[start..i].to_vec();
                let code = Code(String::from_utf8(substring).expect("Off a code boundary"));

                diff_changes.push((None, code));
            }
        }
    }

    diff_changes
}

// Returns a formatted string to indicate that the full struct isn't being displayed.
fn collect_elided_whitespace(sig_end: usize, source_code: &[u8], item_range_end: usize) -> String {
    let mut fields_start = sig_end;
    while source_code[fields_start].is_ascii_whitespace() && fields_start < item_range_end {
        fields_start += 1;
    }

    let whitespace = String::from_utf8_lossy(&source_code[sig_end..fields_start]);
    format!("{whitespace}..")
}

fn collect_preceding_whitespace(source_code: &[u8], item_start_index: usize) -> String {
    let mut item_diff_whitespace_start = item_start_index as isize - 1;

    while item_diff_whitespace_start > 0 {
        if source_code[item_diff_whitespace_start as usize].is_ascii_whitespace() {
            if source_code[item_diff_whitespace_start as usize] == ASCII_LINE_FEED {
                break;
            } else {
                item_diff_whitespace_start -= 1;
            }
        } else {
            break;
        }
    }

    // TODO: this omits commas between fields (applies to variants and traits, too)
    if !source_code[item_diff_whitespace_start as usize].is_ascii_whitespace() {
        // we hit a non-whitespace character which shouldn't be included in our output
        item_diff_whitespace_start += 1;
    }

    let whitespace_bytes =
        source_code[item_diff_whitespace_start as usize..item_start_index].to_vec();

    String::from_utf8(whitespace_bytes).expect("Off a code boundary")
}

fn escape_html(input: &str) -> String {
    input
        .replace("&", "&amp;")
        .replace("<", "&lt;")
        .replace(">", "&gt;")
        .replace("\"", "&quot;")
        .replace("'", "&#39;")
}