Skip to main content

differential_engine/plan/
counts.rs

1//! Added / removed line totals.
2
3use crate::schema;
4
5/// Line totals for a hunk, or any aggregation of hunks.
6///
7/// Canonical enumeration is `-U0`, so a hunk carries no context lines and the
8/// counts are exactly its added and removed lines. Aggregating them was open-
9/// coded in three places in the TUI alone, each re-deciding which schema field
10/// meant which direction.
11#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
12pub struct LineCounts {
13    pub adds: usize,
14    pub dels: usize,
15}
16
17impl LineCounts {
18    pub fn of_hunk(h: &schema::HunkEntry) -> Self {
19        LineCounts {
20            adds: h.new_count as usize,
21            dels: h.old_count as usize,
22        }
23    }
24}
25
26impl std::ops::Add for LineCounts {
27    type Output = LineCounts;
28
29    fn add(self, rhs: LineCounts) -> LineCounts {
30        LineCounts {
31            adds: self.adds + rhs.adds,
32            dels: self.dels + rhs.dels,
33        }
34    }
35}
36
37impl std::iter::Sum for LineCounts {
38    fn sum<I: Iterator<Item = LineCounts>>(iter: I) -> LineCounts {
39        iter.fold(LineCounts::default(), |a, b| a + b)
40    }
41}