use crate::{Control, Git, Participant, Problem, Repository};
use std::{future::Future, path::PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RetainedHistory {
UpToDate,
Unpushed,
NoUpstream,
Detached,
}
#[derive(Clone, Debug)]
pub struct RemovalObservation {
pub administration_identity: String,
pub worktree_entry: (u64, u64),
pub repository: Repository,
pub destination: Option<PathBuf>,
pub branch: Option<String>,
pub dirty_paths: Vec<PathBuf>,
pub dirty_token: Vec<u8>,
pub history: RetainedHistory,
}
impl RemovalObservation {
pub(crate) fn same_worktree(&self, other: &Self) -> bool {
self.destination == other.destination
&& self.administration_identity == other.administration_identity
&& self.worktree_entry == other.worktree_entry
}
}
#[derive(Debug)]
pub enum UnlinkFailure {
BeforeLaunch(Problem),
Command(Problem),
}
impl From<Problem> for UnlinkFailure {
fn from(problem: Problem) -> Self {
Self::BeforeLaunch(problem)
}
}
pub trait RemovalGit: Git {
fn reconcile_recreation(
&self,
participant: &Participant,
branch: &str,
control: &Control,
) -> impl Future<Output = Result<crate::RecoveryObservation, Problem>> + Send;
fn reconcile_restored(
&self,
participant: &Participant,
control: &Control,
) -> impl Future<Output = Result<crate::RecoveryObservation, Problem>> + Send;
fn inspect_removal(
&self,
participant: &Participant,
control: &Control,
) -> impl Future<Output = Result<RemovalObservation, Problem>> + Send;
fn remove_worktree(
&self,
observation: &RemovalObservation,
discard: bool,
control: &Control,
before_unlink: impl FnOnce() -> Result<(), Problem> + Send,
) -> impl Future<Output = Result<(), UnlinkFailure>> + Send;
fn inspect_restoration(
&self,
participant: &Participant,
branch: &str,
control: &Control,
) -> impl Future<Output = Result<Repository, Problem>> + Send;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecisionKind {
RemoveTask,
RemoveParticipants,
DiscardParticipants,
DiscardTask,
DiscardFiles,
RetainedHistory,
LostChanges,
}
#[derive(Clone, Debug)]
pub struct Decision {
pub task: Option<String>,
pub kind: DecisionKind,
pub participant: String,
pub participants: Vec<String>,
pub message: String,
}
pub trait RemovalObserver: crate::Observer {
fn begin_disclosure(&self, _task: &crate::Task, _amendment: Option<&crate::AmendRequest>) {}
fn requires_unchanged_disclosure(&self) -> bool {
false
}
fn disclose(
&self,
_participant: &str,
_observation: &RemovalObservation,
) -> impl Future<Output = Result<(), Problem>> + Send {
async { Ok(()) }
}
fn removed(
&self,
task: &str,
participant: &str,
) -> impl Future<Output = Result<(), Problem>> + Send {
self.phase(task, participant, "removed")
}
fn confirm(
&self,
_decision: Decision,
) -> impl Future<Output = Result<Option<bool>, Problem>> + Send {
async { Ok(None) }
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct RemovalOptions {
pub yes: bool,
pub force: bool,
pub undo: bool,
}