Skip to main content

workyard_application/actions/remove/
contract.rs

1use serde::Serialize;
2use trine::NativePath;
3use workyard_core::{OperationKind, Problem, Task};
4
5pub use crate::contracts::{OperationReport, ParticipantOutcome};
6use crate::contracts::{ProblemOutcome, removal};
7
8/// Retirement outcome, observed participants and any outstanding problems.
9#[derive(Serialize)]
10pub struct RemoveOutcome {
11	#[serde(skip)]
12	pub(crate) explicit_workspace: Option<NativePath>,
13	/// User-facing health classification for the observed task.
14	#[serde(skip_serializing_if = "Option::is_none")]
15	pub condition: Option<crate::contracts::Condition>,
16	/// Whether the invocation entered the mutation phase.
17	#[serde(skip_serializing_if = "Option::is_none")]
18	pub mutation_started: Option<bool>,
19	/// Removal or restoration evidence associated with this task.
20	#[serde(flatten)]
21	pub removal: removal::RemovalReport,
22	/// Invocation history tracked separately from task lifecycle.
23	pub operation: OperationReport,
24	/// None when this invocation could not establish task membership.
25	pub lifecycle: Option<&'static str>,
26	/// Task selected for this operation.
27	pub task: String,
28	/// Directory containing the task workspaces.
29	pub tasks_root: Option<NativePath>,
30	/// Directory holding authoritative Workyard state.
31	pub state_dir: Option<NativePath>,
32	/// Location of the task record when available.
33	pub record: Option<NativePath>,
34	/// Members associated with this task or operation.
35	pub participants: Vec<ParticipantOutcome>,
36	/// Diagnostic describing a failure or an unresolved condition.
37	#[serde(skip_serializing_if = "Option::is_none")]
38	pub problem: Option<ProblemOutcome>,
39}
40
41impl RemoveOutcome {
42	/// Present a removal or restoration result with its participant evidence.
43	pub fn from_removal(result: workyard_core::RemovalResult) -> Self {
44		let condition = removal::condition(&result);
45		let mut outcome = Self::from_task(result.task, result.record);
46		outcome.operation = result.operation.into();
47		outcome.problem = result.problem.map(Into::into);
48		outcome.condition = Some(condition);
49		outcome.mutation_started = Some(result.mutation_started);
50
51		if !result.mutation_started && result.inspected {
52			removal::mark_present_after_inspection(&mut outcome.participants);
53		}
54
55		outcome
56	}
57
58	/// Build an invocation outcome for a request rejected before effects.
59	pub fn rejected(task: String, problem: Problem) -> Self {
60		Self {
61			condition: None,
62			mutation_started: None,
63			removal: Default::default(),
64			operation: OperationReport::rejected(OperationKind::Remove, &problem),
65			explicit_workspace: None,
66			lifecycle: None,
67			task,
68			tasks_root: None,
69			state_dir: None,
70			record: None,
71			participants: Vec::new(),
72			problem: Some(problem.into()),
73		}
74	}
75
76	/// Present the recorded task and its participants.
77	pub fn from_task(task: Task, record: std::path::PathBuf) -> Self {
78		let participants = removal::participants(task.participants, task.removal.as_ref());
79
80		Self {
81			condition: None,
82			mutation_started: None,
83			removal: Default::default(),
84			operation: task.operation.into(),
85			explicit_workspace: None,
86			lifecycle: Some(task.lifecycle.as_str()),
87			task: task.name,
88			tasks_root: Some(NativePath::new(task.root).expect("validated root path")),
89			state_dir: Some(NativePath::new(task.state_dir).expect("validated state path")),
90			record: Some(NativePath::new(record).expect("validated filesystem path")),
91			participants,
92			problem: task.problem.map(Into::into),
93		}
94	}
95}