pending_op.rs

 1use git::repository::RepoPath;
 2use std::ops::Add;
 3use sum_tree::{ContextLessSummary, Dimension, Item, KeyedItem};
 4use worktree::PathSummary;
 5
 6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 7pub enum PendingOpStatus {
 8    Staged,
 9    Unstaged,
10    Reverted,
11    Unchanged,
12}
13
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct PendingOp {
16    pub repo_path: RepoPath,
17    pub id: PendingOpId,
18    pub status: PendingOpStatus,
19    pub finished: bool,
20}
21
22#[derive(Clone, Debug)]
23pub struct PendingOpSummary {
24    pub max_id: PendingOpId,
25    pub staged_count: usize,
26    pub unstaged_count: usize,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
30pub struct PendingOpId(pub usize);
31
32impl Item for PendingOp {
33    type Summary = PathSummary<PendingOpSummary>;
34
35    fn summary(&self, _cx: ()) -> Self::Summary {
36        PathSummary {
37            max_path: self.repo_path.0.clone(),
38            item_summary: PendingOpSummary {
39                max_id: self.id,
40                staged_count: (self.status == PendingOpStatus::Staged) as usize,
41                unstaged_count: (self.status == PendingOpStatus::Unstaged) as usize,
42            },
43        }
44    }
45}
46
47impl ContextLessSummary for PendingOpSummary {
48    fn zero() -> Self {
49        Self {
50            max_id: PendingOpId(0),
51            staged_count: 0,
52            unstaged_count: 0,
53        }
54    }
55
56    fn add_summary(&mut self, summary: &Self) {
57        self.max_id = summary.max_id;
58        self.staged_count += summary.staged_count;
59        self.unstaged_count += summary.unstaged_count;
60    }
61}
62
63impl KeyedItem for PendingOp {
64    type Key = PendingOpId;
65
66    fn key(&self) -> Self::Key {
67        self.id
68    }
69}
70
71impl Dimension<'_, PathSummary<PendingOpSummary>> for PendingOpId {
72    fn zero(_cx: ()) -> Self {
73        Self(0)
74    }
75
76    fn add_summary(&mut self, summary: &PathSummary<PendingOpSummary>, _cx: ()) {
77        *self = summary.item_summary.max_id;
78    }
79}
80
81impl Add<usize> for PendingOpId {
82    type Output = PendingOpId;
83
84    fn add(self, rhs: usize) -> Self::Output {
85        Self(self.0 + rhs)
86    }
87}