pending_op.rs

  1use futures::channel::oneshot::Canceled;
  2use git::repository::RepoPath;
  3use std::ops::Add;
  4use sum_tree::{ContextLessSummary, Item, KeyedItem};
  5use worktree::{PathKey, PathSummary};
  6
  7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  8pub enum GitStatus {
  9    Staged,
 10    Unstaged,
 11    Reverted,
 12    Unchanged,
 13}
 14
 15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 16pub enum JobStatus {
 17    Started,
 18    Finished,
 19    Canceled,
 20}
 21
 22#[derive(Clone, Debug, PartialEq, Eq)]
 23pub struct PendingOps {
 24    pub repo_path: RepoPath,
 25    pub ops: Vec<PendingOp>,
 26}
 27
 28#[derive(Clone, Debug, PartialEq, Eq)]
 29pub struct PendingOp {
 30    pub id: PendingOpId,
 31    pub git_status: GitStatus,
 32    pub job_status: JobStatus,
 33}
 34
 35#[derive(Clone, Debug)]
 36pub struct PendingOpsSummary {
 37    pub max_id: PendingOpId,
 38}
 39
 40#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
 41pub struct PendingOpId(pub u16);
 42
 43impl Item for PendingOps {
 44    type Summary = PathSummary<PendingOpsSummary>;
 45
 46    fn summary(&self, _cx: ()) -> Self::Summary {
 47        PathSummary {
 48            max_path: self.repo_path.0.clone(),
 49            item_summary: PendingOpsSummary {
 50                max_id: self.ops.last().map(|op| op.id).unwrap_or_default(),
 51            },
 52        }
 53    }
 54}
 55
 56impl ContextLessSummary for PendingOpsSummary {
 57    fn zero() -> Self {
 58        Self {
 59            max_id: PendingOpId::default(),
 60        }
 61    }
 62
 63    fn add_summary(&mut self, summary: &Self) {
 64        self.max_id = summary.max_id;
 65    }
 66}
 67
 68impl KeyedItem for PendingOps {
 69    type Key = PathKey;
 70
 71    fn key(&self) -> Self::Key {
 72        PathKey(self.repo_path.0.clone())
 73    }
 74}
 75
 76impl Add<u16> for PendingOpId {
 77    type Output = PendingOpId;
 78
 79    fn add(self, rhs: u16) -> Self::Output {
 80        Self(self.0 + rhs)
 81    }
 82}
 83
 84impl PendingOps {
 85    pub fn new(path: &RepoPath) -> Self {
 86        Self {
 87            repo_path: path.clone(),
 88            ops: Vec::new(),
 89        }
 90    }
 91
 92    pub fn op_by_id(&self, id: PendingOpId) -> Option<&PendingOp> {
 93        self.ops.iter().find(|op| op.id == id)
 94    }
 95
 96    pub fn op_by_id_mut(&mut self, id: PendingOpId) -> Option<&mut PendingOp> {
 97        self.ops.iter_mut().find(|op| op.id == id)
 98    }
 99}
100
101impl From<Canceled> for JobStatus {
102    fn from(_err: Canceled) -> Self {
103        Self::Canceled
104    }
105}