workyard_application/actions/recover/
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 RecoverOutcome {
11 #[serde(skip)]
12 pub(crate) explicit_workspace: Option<NativePath>,
13 #[serde(skip_serializing_if = "Option::is_none")]
15 pub registration_problem: Option<ProblemOutcome>,
16 pub nothing_to_recover: bool,
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub condition: Option<crate::contracts::Condition>,
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub mutation_started: Option<bool>,
24 #[serde(flatten)]
26 pub removal: removal::RemovalReport,
27 pub operation: OperationReport,
29 pub lifecycle: Option<&'static str>,
31 pub task: String,
33 pub tasks_root: Option<NativePath>,
35 pub state_dir: Option<NativePath>,
37 pub record: Option<NativePath>,
39 pub participants: Vec<ParticipantOutcome>,
41 #[serde(skip_serializing_if = "Vec::is_empty")]
43 pub changes: Vec<crate::contracts::ParticipantChange>,
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub problem: Option<ProblemOutcome>,
47}
48
49impl RecoverOutcome {
50 pub fn from_removal(result: workyard_core::RemovalResult) -> Self {
52 let condition = removal::condition(&result);
53 let nothing_to_recover = result.nothing_to_recover;
54 let mut outcome = Self::from_task(result.task, result.record);
55 outcome.nothing_to_recover = nothing_to_recover;
56 outcome.operation = result.operation.into();
57 outcome.problem = result.problem.map(Into::into);
58 outcome.condition = Some(condition);
59 outcome.mutation_started = Some(result.mutation_started);
60
61 if !result.mutation_started && result.inspected {
62 removal::mark_present_after_inspection(&mut outcome.participants);
63 }
64
65 outcome
66 }
67
68 pub fn rejected(task: String, problem: Problem) -> Self {
70 Self {
71 changes: Vec::new(),
72 registration_problem: None,
73 nothing_to_recover: false,
74 condition: None,
75 mutation_started: None,
76 removal: Default::default(),
77 operation: OperationReport::rejected(OperationKind::Recover, &problem),
78 explicit_workspace: None,
79 lifecycle: None,
80 task,
81 tasks_root: None,
82 state_dir: None,
83 record: None,
84 participants: Vec::new(),
85 problem: Some(problem.into()),
86 }
87 }
88
89 pub fn from_task(task: Task, record: std::path::PathBuf) -> Self {
91 let participants = removal::participants(task.participants, task.removal.as_ref());
92
93 Self {
94 changes: Vec::new(),
95 registration_problem: None,
96 nothing_to_recover: false,
97 condition: None,
98 mutation_started: None,
99 removal: Default::default(),
100 operation: task.operation.into(),
101 explicit_workspace: None,
102 lifecycle: Some(task.lifecycle.as_str()),
103 task: task.name,
104 tasks_root: Some(NativePath::new(task.root).expect("validated root path")),
105 state_dir: Some(NativePath::new(task.state_dir).expect("validated state path")),
106 record: Some(NativePath::new(record).expect("validated filesystem path")),
107 participants,
108 problem: task.problem.map(Into::into),
109 }
110 }
111}
112
113#[derive(Serialize)]
115#[serde(untagged)]
116pub enum RecoveryOutcome {
117 Task(Box<RecoverOutcome>),
119 Workspace(super::workspace::WorkspaceRecovery),
121}