Skip to main content

workyard_application/actions/amend/
contract.rs

1use crate::contracts::{OperationReport, ParticipantChange, ParticipantOutcome, ProblemOutcome};
2use serde::Serialize;
3use trine::NativePath;
4use workyard_core::{AmendResult, OperationKind, Problem};
5
6/// Membership changes and any outstanding problems after an amendment.
7#[derive(Serialize)]
8pub struct AmendOutcome {
9	/// Removal or restoration evidence associated with this task.
10	#[serde(flatten)]
11	pub removal: crate::contracts::removal::RemovalReport,
12	/// Failure to record a project root after participant work succeeded.
13	#[serde(skip_serializing_if = "Option::is_none")]
14	pub registration_problem: Option<ProblemOutcome>,
15	/// Invocation history tracked separately from task lifecycle.
16	pub operation: OperationReport,
17	/// Task selected for this operation.
18	pub task: String,
19	/// Current membership lifecycle of the task.
20	pub lifecycle: Option<&'static str>,
21	/// Directory holding authoritative Workyard state.
22	pub state_dir: Option<NativePath>,
23	/// Members associated with this task or operation.
24	pub participants: Vec<ParticipantOutcome>,
25	/// Changes reported for individual participants.
26	pub changes: Vec<ParticipantChange>,
27	/// Diagnostic describing a failure or an unresolved condition.
28	#[serde(skip_serializing_if = "Option::is_none")]
29	pub problem: Option<ProblemOutcome>,
30}
31
32impl AmendOutcome {
33	/// Build an invocation outcome for a request rejected before effects.
34	pub fn rejected(task: String, problem: Problem) -> Self {
35		Self {
36			removal: Default::default(),
37			registration_problem: None,
38			operation: OperationReport::rejected(OperationKind::Amend, &problem),
39			task,
40			lifecycle: None,
41			state_dir: None,
42			participants: Vec::new(),
43			changes: Vec::new(),
44			problem: Some(problem.into()),
45		}
46	}
47
48	pub(super) fn guide_retirement(
49		&mut self,
50		state: &std::path::Path,
51		explicit_workspace: Option<&std::path::Path>,
52	) {
53		let Some(problem) = self
54			.problem
55			.as_mut()
56			.filter(|p| p.code == workyard_core::ProblemCode::EmptyTask.as_str())
57		else {
58			return;
59		};
60		if let Some(command) = crate::guidance::commands::remove(&self.task, explicit_workspace) {
61			problem.remedy = format!("To retire the task, run {command}");
62		}
63		self.state_dir = NativePath::new(state.to_path_buf()).ok();
64	}
65
66	/// Convert the core operation result into its public representation.
67	pub fn from_result(result: AmendResult) -> Self {
68		Self {
69			removal: Default::default(),
70			registration_problem: None,
71			operation: result.operation.into(),
72			task: result.task.name,
73			lifecycle: Some(result.task.lifecycle.as_str()),
74			state_dir: Some(NativePath::new(result.task.state_dir).expect("validated state path")),
75			participants: result
76				.task
77				.participants
78				.into_iter()
79				.map(Into::into)
80				.collect(),
81			changes: result.changes.into_iter().map(Into::into).collect(),
82			problem: result.problem.map(Into::into),
83		}
84	}
85}