workyard_core/model/
task.rs1use crate::Problem;
2use std::path::PathBuf;
3
4#[derive(Clone, Debug, PartialEq, Eq)]
6pub enum Phase {
7 NotStarted,
9 Fetching,
11 Creating,
13 WorktreeCreated,
15 Completed,
17}
18
19impl Phase {
20 pub fn as_str(&self) -> &'static str {
22 match self {
23 Self::NotStarted => "not_started",
24 Self::Fetching => "fetching",
25 Self::Creating => "creating",
26 Self::WorktreeCreated => "worktree_created",
27 Self::Completed => "completed",
28 }
29 }
30}
31
32#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct PlacementHint {
35 pub source: PathBuf,
37 pub common_dir: PathBuf,
39 pub destination: PathBuf,
41 pub workspace: PathBuf,
43 pub worktree_entry: Option<(u64, u64)>,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct Participant {
50 pub placement: Option<PlacementHint>,
52 pub registration_roots: Vec<PathBuf>,
54 pub creation: Option<super::creation::CreationOptions>,
56 pub name: String,
58 pub source: PathBuf,
60 pub common_dir: PathBuf,
62 pub backing_identity: String,
64 pub worktree_identity: Option<String>,
66 pub destination: PathBuf,
68 pub remote: Option<String>,
70 pub from: Option<String>,
72 pub phase: Phase,
74}
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum TaskLifecycle {
79 Active,
81 Retired,
83}
84
85impl TaskLifecycle {
86 pub fn as_str(self) -> &'static str {
88 match self {
89 Self::Active => "active",
90 Self::Retired => "retired",
91 }
92 }
93}
94
95#[derive(Clone, Debug)]
97pub struct Task {
98 pub generation: u64,
100 pub removal: Option<super::removal::Removal>,
102 pub name: String,
104 pub root: PathBuf,
106 pub state_dir: PathBuf,
108 pub participants: Vec<Participant>,
110 pub checkout: bool,
112 pub no_fetch: bool,
114 pub lifecycle: TaskLifecycle,
116 pub operation: super::operation::Operation,
118 pub problem: Option<Problem>,
120}
121
122impl Task {
123 pub(crate) fn preserve_settled_cancellation(&mut self) {
125 if self.operation.outcome == crate::OperationOutcome::Cancelled
126 && self
127 .problem
128 .as_ref()
129 .is_some_and(|problem| problem.code == crate::ProblemCode::Cancelled)
130 && self
131 .participants
132 .iter()
133 .all(|participant| participant.phase == Phase::Completed)
134 {
135 self.operation.outcome = crate::OperationOutcome::Succeeded;
136 self.problem = None;
137 }
138 }
139
140 pub fn admitted_roots(&self, previous: Option<&Self>) -> Vec<PathBuf> {
143 let established: std::collections::HashSet<_> = previous
144 .into_iter()
145 .flat_map(|task| &task.participants)
146 .filter(|p| p.worktree_identity.is_some())
147 .map(|p| &p.backing_identity)
148 .collect();
149 self.participants
150 .iter()
151 .filter(|p| p.worktree_identity.is_some() && !established.contains(&p.backing_identity))
152 .flat_map(|p| p.registration_roots.iter().cloned())
153 .collect()
154 }
155
156 pub fn creation_options(&self, index: usize) -> super::creation::CreationOptions {
158 self.participants[index]
159 .creation
160 .unwrap_or(super::creation::CreationOptions {
161 checkout: self.checkout,
162 no_fetch: self.no_fetch,
163 })
164 }
165}