workyard_application/actions/amend/
contract.rs1use crate::contracts::{OperationReport, ParticipantChange, ParticipantOutcome, ProblemOutcome};
2use serde::Serialize;
3use trine::NativePath;
4use workyard_core::{AmendResult, OperationKind, Problem};
5
6#[derive(Serialize)]
8pub struct AmendOutcome {
9 #[serde(flatten)]
11 pub removal: crate::contracts::removal::RemovalReport,
12 #[serde(skip_serializing_if = "Option::is_none")]
14 pub registration_problem: Option<ProblemOutcome>,
15 pub operation: OperationReport,
17 pub task: String,
19 pub lifecycle: Option<&'static str>,
21 pub state_dir: Option<NativePath>,
23 pub participants: Vec<ParticipantOutcome>,
25 pub changes: Vec<ParticipantChange>,
27 #[serde(skip_serializing_if = "Option::is_none")]
29 pub problem: Option<ProblemOutcome>,
30}
31
32impl AmendOutcome {
33 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 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}