workyard_core/model/
recovery.rs1use crate::{Repository, Task};
2use std::{collections::BTreeMap, path::PathBuf};
3
4#[derive(Clone, Debug)]
6pub struct RecoveryRecord {
7 pub recorded: Option<Task>,
9 pub needs_publication: bool,
11 pub task: Task,
13 pub pending: Option<usize>,
15 pub relocated_project_roots: BTreeMap<PathBuf, PathBuf>,
17}
18
19#[derive(Clone, Debug)]
21pub struct RecoveryObservation {
22 pub repository: Repository,
24 pub destination: Option<PathBuf>,
26 pub operation_in_progress: bool,
28}
29
30pub struct RecoveryResult {
32 pub admitted_roots: Vec<PathBuf>,
34 pub operation: crate::Operation,
36 pub problem: Option<crate::Problem>,
38 pub task: Task,
40 pub record: PathBuf,
42 pub nothing_to_recover: bool,
44}
45
46impl RecoveryRecord {
47 pub(crate) fn into_amendment_task(mut self) -> Task {
50 for participant in &mut self.task.participants {
51 locate_roots(
52 &mut participant.registration_roots,
53 &self.relocated_project_roots,
54 );
55 }
56 self.task
57 }
58
59 pub(crate) fn newly_admitted_roots(&self, task: &Task) -> Vec<PathBuf> {
60 let mut roots = task.admitted_roots(Some(&self.task));
61 locate_roots(&mut roots, &self.relocated_project_roots);
62 roots
63 }
64
65 pub(crate) fn writer_problem(&self, index: usize) -> crate::Problem {
66 let participant = &self.task.participants[index];
67 let operation = match self.task.operation.continues.or(self.task.operation.kind) {
68 Some(crate::OperationKind::Remove) => "the removal operation",
69 _ => match participant.phase {
70 crate::Phase::Creating => "worktree creation",
71 crate::Phase::Fetching => "the fetch",
72 _ => "the task operation",
73 },
74 };
75 crate::Problem::unconfirmed_git_exit(format_args!(
76 "{operation} for participant {:?} at {:?}",
77 participant.name, participant.destination
78 ))
79 }
80
81 pub(crate) fn matches_published(&self, task: &Task) -> bool {
84 !self.needs_publication
85 && self.recorded.as_ref().is_some_and(|recorded| {
86 recorded.problem.is_none()
87 && recorded.operation.outcome == crate::OperationOutcome::Succeeded
88 && recorded.participants == task.participants
89 && recorded.lifecycle == task.lifecycle
90 && recorded.removal == task.removal
91 })
92 }
93}
94
95fn locate_roots(roots: &mut [PathBuf], locations: &BTreeMap<PathBuf, PathBuf>) {
96 for root in roots {
97 if let Some(located) = locations.get(root) {
98 root.clone_from(located);
99 }
100 }
101}