workyard_core/model/
operation.rs1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum OperationKind {
4 Create,
6 Amend,
8 Recover,
10 Remove,
12}
13
14impl OperationKind {
15 pub fn as_str(self) -> &'static str {
17 match self {
18 Self::Amend => "amend",
19 Self::Create => "create",
20 Self::Recover => "recover",
21 Self::Remove => "remove",
22 }
23 }
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum OperationOutcome {
29 Pending,
31 Succeeded,
33 Cancelled,
35 Failed,
37}
38
39impl OperationOutcome {
40 pub fn as_str(self) -> &'static str {
42 match self {
43 Self::Pending => "pending",
44 Self::Succeeded => "succeeded",
45 Self::Cancelled => "cancelled",
46 Self::Failed => "failed",
47 }
48 }
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub struct Operation {
54 pub kind: Option<OperationKind>,
56 pub continues: Option<OperationKind>,
58 pub outcome: OperationOutcome,
60}
61
62impl Operation {
63 pub fn create() -> Self {
65 Self {
66 kind: Some(OperationKind::Create),
67 continues: None,
68 outcome: OperationOutcome::Pending,
69 }
70 }
71
72 pub fn is_continuing(self, kind: OperationKind) -> bool {
74 match (self.kind, self.continues) {
75 (Some(OperationKind::Recover), Some(previous)) => previous == kind,
76 (Some(current), None) => current == kind,
77 _ => false,
78 }
79 }
80
81 pub fn recovery(self) -> Self {
83 Self {
84 kind: Some(OperationKind::Recover),
85 continues: self
86 .continues
87 .or(self.kind.filter(|kind| *kind != OperationKind::Recover)),
88 outcome: OperationOutcome::Pending,
89 }
90 }
91}