Skip to main content

workyard_core/recover/
scope.rs

1use crate::{OperationKind, Problem, ProblemCode, Task, TaskLifecycle};
2
3/// Authority granted to a recovery invocation.
4pub enum RecoveryScope {
5	/// Include older active tasks whose records have no operation provenance.
6	Reconcile,
7	/// Continue only an active create or checkout, including a previous continuation.
8	CreateOnly,
9}
10
11impl RecoveryScope {
12	pub(super) fn authorize(self, task: &Task) -> Result<(), Problem> {
13		let owner = task.operation.continues.or(task.operation.kind);
14		let permitted = match owner {
15			Some(OperationKind::Create) => true,
16			None | Some(OperationKind::Recover) => matches!(self, Self::Reconcile),
17			Some(OperationKind::Amend | OperationKind::Remove) => false,
18		};
19		if task.lifecycle == TaskLifecycle::Active && task.removal.is_none() && permitted {
20			return Ok(());
21		}
22
23		let operation = owner.map_or("unknown", OperationKind::as_str);
24		let message = format!(
25			"Creation recovery requires an active create or checkout. Task {:?} is {} with recorded operation {operation}.",
26			task.name,
27			task.lifecycle.as_str()
28		);
29		Err(Problem::new(ProblemCode::InvalidInput, message).remedy(
30			"Inspect this task with status. Repeat the original removal or amendment to continue it. Use CLI or RPC recovery for restoration.",
31		))
32	}
33}