workyard_application/actions/remove/
contract.rs1use 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#[derive(Serialize)]
10pub struct RemoveOutcome {
11 #[serde(skip)]
12 pub(crate) explicit_workspace: Option<NativePath>,
13 #[serde(skip_serializing_if = "Option::is_none")]
15 pub condition: Option<crate::contracts::Condition>,
16 #[serde(skip_serializing_if = "Option::is_none")]
18 pub mutation_started: Option<bool>,
19 #[serde(flatten)]
21 pub removal: removal::RemovalReport,
22 pub operation: OperationReport,
24 pub lifecycle: Option<&'static str>,
26 pub task: String,
28 pub tasks_root: Option<NativePath>,
30 pub state_dir: Option<NativePath>,
32 pub record: Option<NativePath>,
34 pub participants: Vec<ParticipantOutcome>,
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub problem: Option<ProblemOutcome>,
39}
40
41impl RemoveOutcome {
42 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 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 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}