Skip to main content

workyard_core/ports/
state.rs

1use crate::{Problem, Task};
2use std::path::{Path, PathBuf};
3
4/// Whether managed writers for the recorded effect have stopped.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum EffectState {
7	/// The supervised Git writer may still be running.
8	Pending,
9	/// The supervised effect has finished.
10	Settled,
11}
12
13/// Task admission, publication and effect evidence used by lifecycle operations.
14pub trait State: Sync {
15	/// Guard held while reading or mutating a specific task.
16	type Admission: Send + Sync;
17	/// Revalidate the workspace binding before relying on its state.
18	fn verify(&self) -> Result<(), Problem>;
19
20	/// Check that the proposed workspace path is available.
21	fn validate_destination(&self, path: &Path) -> Result<(), Problem>;
22
23	/// Reserve the proposed destination on the filesystem.
24	fn reserve(&self, path: &Path) -> Result<(), Problem>;
25
26	/// Acquire operation admission for the named task.
27	fn admit(&self, name: &str) -> Result<Self::Admission, Problem>;
28
29	/// Check under admission that the proposed task can be created.
30	fn ensure_new(&self, admission: &Self::Admission, name: &str) -> Result<(), Problem>;
31
32	/// Allocate a fresh incarnation for this task name.
33	fn next_generation(&self, _admission: &Self::Admission) -> Result<u64, Problem> {
34		Err(Problem::new(
35			crate::ProblemCode::IncompatibleState,
36			"State adapter must allocate a fresh task generation.",
37		))
38	}
39
40	/// Clean up retained state belonging to the retired incarnation.
41	fn cleanup_retired(&self, _admission: &Self::Admission, _task: &Task) -> Result<(), Problem> {
42		Ok(())
43	}
44
45	/// Return the authoritative document path for the admitted task.
46	fn record_path(&self, admission: &Self::Admission) -> PathBuf;
47
48	/// Publish initial task state while admission is held.
49	fn begin(&self, admission: &Self::Admission, task: &Task) -> Result<(), Problem>;
50
51	/// Publish updated task state while admission is held.
52	fn save(&self, admission: &Self::Admission, task: &Task) -> Result<(), Problem>;
53
54	/// Named entry point; delegates to the legacy hook for existing adapters.
55	fn record_effect(
56		&self,
57		admission: &Self::Admission,
58		task: &Task,
59		index: usize,
60		state: EffectState,
61	) -> Result<(), Problem> {
62		self.effect(admission, task, index, state == EffectState::Settled)
63	}
64
65	/// Compatibility hook for existing state adapters.
66	fn effect(
67		&self,
68		_admission: &Self::Admission,
69		_task: &Task,
70		_index: usize,
71		_settled: bool,
72	) -> Result<(), Problem> {
73		Ok(())
74	}
75}
76
77/// Access to task records and effect evidence for recovery.
78pub trait RecoveryState: State {
79	/// Reconstruct the admitted task and its outstanding work.
80	fn load(
81		&self,
82		admission: &Self::Admission,
83		name: &str,
84	) -> Result<crate::RecoveryRecord, Problem>;
85}