1mod codec;
2mod compatibility;
3mod operation;
4mod placement;
5mod removal;
6pub use removal::RemovalRecord;
7
8pub(crate) use codec::{decode, match_intent, validate};
9pub use operation::OperationRecord;
10
11use crate::path::StoredPath;
12use serde::{Deserialize, Serialize};
13use workyard_core::{Problem, Task};
14
15#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
16#[serde(deny_unknown_fields)]
17pub struct CreationRecord {
18 pub checkout: bool,
19 pub no_fetch: bool,
20 #[serde(rename = "pull")]
21 legacy_pull: bool,
22}
23
24impl From<workyard_core::CreationOptions> for CreationRecord {
25 fn from(value: workyard_core::CreationOptions) -> Self {
26 Self {
27 checkout: value.checkout,
28 no_fetch: value.no_fetch,
29 legacy_pull: false,
30 }
31 }
32}
33
34impl From<CreationRecord> for workyard_core::CreationOptions {
35 fn from(value: CreationRecord) -> Self {
36 Self {
37 checkout: value.checkout,
38 no_fetch: value.no_fetch,
39 }
40 }
41}
42
43#[derive(Clone, Debug, Serialize, Deserialize)]
44#[serde(deny_unknown_fields)]
45pub struct ParticipantRecord {
46 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub last_location: Option<placement::LocationRecord>,
48 #[serde(default, skip_serializing_if = "Vec::is_empty")]
49 pub registration_roots: Vec<StoredPath>,
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub creation: Option<CreationRecord>,
52 pub name: String,
53 pub source: StoredPath,
54 pub common_dir: StoredPath,
55 pub backing_identity: String,
56 #[serde(default)]
57 pub worktree_identity: Option<String>,
58 pub destination: StoredPath,
59 pub remote: Option<String>,
60 pub from: Option<String>,
61 pub phase: String,
62}
63
64#[derive(Clone, Debug, Serialize, Deserialize)]
65pub struct ProblemRecord {
66 pub code: String,
67 pub message: String,
68 pub remedy: String,
69}
70
71impl From<&Problem> for ProblemRecord {
72 fn from(problem: &Problem) -> Self {
73 Self {
74 code: problem.code.as_str().into(),
75 message: problem.message.clone(),
76 remedy: problem.remedy.clone(),
77 }
78 }
79}
80
81#[derive(Clone, Debug, Serialize)]
83pub struct TaskRecord {
84 pub generation: u64,
86 pub removal: Option<RemovalRecord>,
88 pub version: u32,
90 pub task: String,
92 pub tasks_root: StoredPath,
94 pub state_dir: StoredPath,
96 pub checkout: bool,
98 pub no_fetch: bool,
100 #[serde(rename = "pull")]
101 legacy_pull: bool,
102 pub lifecycle: String,
104 pub operation: OperationRecord,
106 pub participants: Vec<ParticipantRecord>,
108 pub problem: Option<ProblemRecord>,
110}
111
112impl From<&Task> for TaskRecord {
113 fn from(task: &Task) -> Self {
114 Self {
115 version: if task.participants.iter().any(|p| p.creation.is_some())
116 || task.operation.kind == Some(workyard_core::OperationKind::Amend)
117 || task.operation.continues == Some(workyard_core::OperationKind::Amend)
118 {
119 4
120 } else {
121 3
122 },
123 generation: task.generation,
124 removal: task.removal.as_ref().map(Into::into),
125 task: task.name.clone(),
126 tasks_root: placement::anchored(&task.root, &task.state_dir),
127 state_dir: task.state_dir.as_path().into(),
128 checkout: task.checkout,
129 no_fetch: task.no_fetch,
130 legacy_pull: false,
131 lifecycle: task.lifecycle.as_str().into(),
132 operation: OperationRecord::from(task.operation),
133 problem: task.problem.as_ref().map(Into::into),
134 participants: task
135 .participants
136 .iter()
137 .map(|p| ParticipantRecord::new(p, &task.state_dir))
138 .collect(),
139 }
140 }
141}
142
143impl ParticipantRecord {
144 fn new(participant: &workyard_core::Participant, state: &std::path::Path) -> Self {
145 Self {
146 last_location: placement::LocationRecord::from_participant(participant, state),
147 creation: participant.creation.map(Into::into),
148 name: participant.name.clone(),
149 source: placement::anchored(&participant.source, state),
150 common_dir: placement::anchored(&participant.common_dir, state),
151 backing_identity: participant.backing_identity.clone(),
152 registration_roots: participant
153 .registration_roots
154 .iter()
155 .map(|p| p.as_path().into())
156 .collect(),
157 worktree_identity: participant.worktree_identity.clone(),
158 destination: placement::anchored(&participant.destination, state),
159 remote: participant.remote.clone(),
160 from: participant.from.clone(),
161 phase: participant.phase.as_str().into(),
162 }
163 }
164}
165
166impl TaskRecord {
167 const SUPPORTED_VERSIONS: &[u64] = &[1, 2, 3, 4];
168
169 pub(crate) fn read(path: &std::path::Path) -> Result<Self, Problem> {
170 Self::read_with_version(path).map(|(record, _)| record)
171 }
172
173 pub(crate) fn read_with_version(path: &std::path::Path) -> Result<(Self, u64), Problem> {
175 crate::documents::read_supported_with_version(path, Self::SUPPORTED_VERSIONS)
176 }
177
178 pub(crate) fn refresh_location(
179 &mut self,
180 participant: &workyard_core::Participant,
181 state: &std::path::Path,
182 ) -> Result<bool, Problem> {
183 let record = self
184 .participants
185 .iter_mut()
186 .find(|record| {
187 record.name == participant.name
188 && record.backing_identity == participant.backing_identity
189 && record.worktree_identity == participant.worktree_identity
190 })
191 .ok_or_else(|| {
192 crate::damage::damaged("Participant identity changed during placement repair.")
193 })?;
194 let current = ParticipantRecord::new(participant, state);
195 let changed = record.source != current.source
196 || record.common_dir != current.common_dir
197 || record.destination != current.destination
198 || record.last_location != current.last_location;
199 if changed {
200 record.source = current.source;
201 record.common_dir = current.common_dir;
202 record.destination = current.destination;
203 record.last_location = current.last_location;
204 }
205 Ok(changed)
206 }
207}