pending_op.rs

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