1mod admission;
2mod amendment;
3mod evidence;
4mod history;
5mod placement;
6mod recovery;
7mod workspace;
8
9pub use admission::TaskGuard;
10
11use crate::documents::publish;
12use crate::{Lock, TaskRecord};
13use crate::{canonical_future, exists};
14use std::{
15 fs,
16 path::{Path, PathBuf},
17};
18use workyard_core::{Problem, Task};
19
20pub struct Store {
22 root: PathBuf,
23 state: PathBuf,
24 workspace: Lock,
25}
26
27impl Store {
28 pub fn begin(&self, guard: &TaskGuard, task: &Task) -> Result<(), Problem> {
30 self.verify()?;
31 guard.verify()?;
32 self.archive_previous(guard, &task.name)?;
33 let record = TaskRecord::from(task);
34 publish(&guard.evidence_path, &record)?;
36 let revision = crate::revision::path(&guard.evidence_path);
37 if exists(&revision)? {
38 std::fs::remove_file(revision)
39 .map_err(|error| Problem::io("Release archived membership revision", error))?;
40 }
41 self.publish_effect(guard, task, 0, workyard_core::EffectState::Settled)?;
42 publish(&guard.record_path, &record)
43 }
44
45 pub fn save(&self, guard: &TaskGuard, task: &Task) -> Result<(), Problem> {
47 self.verify()?;
48 guard.verify()?;
49 self.revise(guard, task, crate::revision::WriterUpdate::Preserve)?;
50 publish(&guard.record_path, &TaskRecord::from(task))
51 }
52}
53
54impl workyard_core::State for Store {
55 type Admission = TaskGuard;
56 fn verify(&self) -> Result<(), Problem> {
57 Store::verify(self)
58 }
59
60 fn validate_destination(&self, path: &Path) -> Result<(), Problem> {
61 self.verify()?;
62 if let Some(parent) = path.parent()
65 && !parent.is_dir()
66 && let Some(existing) = parent.ancestors().find(|dir| dir.is_dir())
67 && let Some(name) = path.file_name()
68 {
69 exists(&existing.join(name)).map_err(|problem| {
70 problem.remedy("Choose a shorter task or participant name with --repo-name.")
71 })?;
72 }
73 if canonical_future(path)? != path {
74 return Err(Problem::new(
75 workyard_core::ProblemCode::PathOverlap,
76 format!("Destination crosses a changed or aliased ancestor: {path:?}."),
77 ));
78 }
79
80 if exists(path)? {
81 return Err(Problem::new(
82 workyard_core::ProblemCode::TaskExists,
83 format!("Task destination already exists: {path:?}."),
84 )
85 .remedy(
86 "Choose another task name; create/checkout never adopts an existing destination.",
87 ));
88 }
89
90 Ok(())
91 }
92
93 fn reserve(&self, path: &Path) -> Result<(), Problem> {
94 workyard_core::State::validate_destination(self, path)?;
95 let parent = path.parent().expect("validated task destination");
96 fs::create_dir_all(parent).map_err(|e| Problem::io("Create task prefix directories", e))?;
97 workyard_core::State::validate_destination(self, path)?;
98 fs::create_dir(path)
99 .map_err(|e| Problem::io(format!("Reserve task destination {path:?}"), e))
100 }
101
102 fn admit(&self, name: &str) -> Result<TaskGuard, Problem> {
103 Store::admit(self, name)
104 }
105
106 fn ensure_new(&self, admission: &TaskGuard, name: &str) -> Result<(), Problem> {
107 Store::ensure_new(self, admission, name)
108 }
109
110 fn next_generation(&self, admission: &TaskGuard) -> Result<u64, Problem> {
111 if !exists(&admission.record_path)? {
112 return Ok(1);
113 }
114 let previous = TaskRecord::read(&admission.record_path)?;
115 previous
116 .generation
117 .checked_add(1)
118 .ok_or_else(|| crate::damage::damaged("Task generation exhausted."))
119 }
120
121 fn cleanup_retired(&self, admission: &TaskGuard, task: &Task) -> Result<(), Problem> {
122 self.verify()?;
123 admission.verify()?;
124 match fs::remove_dir(task.root.join(&task.name)) {
125 Ok(()) => Ok(()),
126 Err(error)
127 if matches!(
128 error.kind(),
129 std::io::ErrorKind::NotFound | std::io::ErrorKind::DirectoryNotEmpty
130 ) =>
131 {
132 Ok(())
133 }
134 Err(error) => Err(Problem::io("Remove empty retired task directory", error)),
135 }
136 }
137
138 fn record_path(&self, admission: &TaskGuard) -> PathBuf {
139 admission.record_path.clone()
140 }
141
142 fn begin(&self, admission: &TaskGuard, task: &Task) -> Result<(), Problem> {
143 Store::begin(self, admission, task)
144 }
145
146 fn effect(
147 &self,
148 admission: &TaskGuard,
149 task: &Task,
150 index: usize,
151 settled: bool,
152 ) -> Result<(), Problem> {
153 let state = if settled {
154 workyard_core::EffectState::Settled
155 } else {
156 workyard_core::EffectState::Pending
157 };
158 self.publish_effect(admission, task, index, state)
159 }
160
161 fn save(&self, admission: &TaskGuard, task: &Task) -> Result<(), Problem> {
162 Store::save(self, admission, task)
163 }
164}