Skip to main content

workyard_core/model/
operation.rs

1/// The operation requested by the caller, distinct from a task's lifecycle.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum OperationKind {
4	/// Establish a task and its linked worktrees.
5	Create,
6	/// Change an existing task's membership.
7	Amend,
8	/// Continue or reconcile recorded work.
9	Recover,
10	/// Unlink participant worktrees.
11	Remove,
12}
13
14impl OperationKind {
15	/// Return the stable spelling used in records or output.
16	pub fn as_str(self) -> &'static str {
17		match self {
18			Self::Amend => "amend",
19			Self::Create => "create",
20			Self::Recover => "recover",
21			Self::Remove => "remove",
22		}
23	}
24}
25
26/// Settlement of one operation, independent of task health.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum OperationOutcome {
29	/// Work has started and final settlement is outstanding.
30	Pending,
31	/// The operation finished successfully.
32	Succeeded,
33	/// The caller interrupted the operation.
34	Cancelled,
35	/// The operation encountered a problem.
36	Failed,
37}
38
39impl OperationOutcome {
40	/// Return the stable spelling used in records or output.
41	pub fn as_str(self) -> &'static str {
42		match self {
43			Self::Pending => "pending",
44			Self::Succeeded => "succeeded",
45			Self::Cancelled => "cancelled",
46			Self::Failed => "failed",
47		}
48	}
49}
50
51/// What Workyard attempted and how that attempt settled.
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub struct Operation {
54	/// Unknown for records written before operation provenance was recorded.
55	pub kind: Option<OperationKind>,
56	/// Recovery continues an existing operation; it does not replace its meaning.
57	pub continues: Option<OperationKind>,
58	/// Settlement of this invocation.
59	pub outcome: OperationOutcome,
60}
61
62impl Operation {
63	/// Start recording a pending create operation.
64	pub fn create() -> Self {
65		Self {
66			kind: Some(OperationKind::Create),
67			continues: None,
68			outcome: OperationOutcome::Pending,
69		}
70	}
71
72	/// A direct operation or a recovery explicitly continuing that operation.
73	pub fn is_continuing(self, kind: OperationKind) -> bool {
74		match (self.kind, self.continues) {
75			(Some(OperationKind::Recover), Some(previous)) => previous == kind,
76			(Some(current), None) => current == kind,
77			_ => false,
78		}
79	}
80
81	/// Start a recovery operation carrying forward the original operation kind.
82	pub fn recovery(self) -> Self {
83		Self {
84			kind: Some(OperationKind::Recover),
85			continues: self
86				.continues
87				.or(self.kind.filter(|kind| *kind != OperationKind::Recover)),
88			outcome: OperationOutcome::Pending,
89		}
90	}
91}