Skip to main content

made_core/value_objects/ceremony/
ceremony_state.rs

1use serde::{Deserialize, Serialize};
2
3use super::{CeremonyStateKind, StateExecution, StateId, StateRepeatPolicy};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct CeremonyState {
7    id: StateId,
8    kind: CeremonyStateKind,
9    #[serde(default, skip_serializing_if = "StateExecution::is_sequential")]
10    execution: StateExecution,
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    repeat: Option<StateRepeatPolicy>,
13}
14
15impl CeremonyState {
16    #[must_use]
17    pub fn new(id: StateId, kind: CeremonyStateKind) -> Self {
18        Self {
19            id,
20            kind,
21            execution: StateExecution::Sequential,
22            repeat: None,
23        }
24    }
25
26    #[must_use]
27    pub fn initial(id: StateId) -> Self {
28        Self::new(id, CeremonyStateKind::Initial)
29    }
30
31    #[must_use]
32    pub fn intermediate(id: StateId) -> Self {
33        Self::new(id, CeremonyStateKind::Intermediate)
34    }
35
36    #[must_use]
37    pub fn terminal(id: StateId) -> Self {
38        Self::new(id, CeremonyStateKind::Terminal)
39    }
40
41    #[must_use]
42    pub fn id(&self) -> &StateId {
43        &self.id
44    }
45
46    #[must_use]
47    pub fn kind(&self) -> CeremonyStateKind {
48        self.kind
49    }
50
51    #[must_use]
52    pub fn with_execution(mut self, execution: StateExecution) -> Self {
53        self.execution = execution;
54        self
55    }
56
57    #[must_use]
58    pub fn execution(&self) -> StateExecution {
59        self.execution
60    }
61
62    #[must_use]
63    pub fn with_repeat_policy(mut self, repeat: StateRepeatPolicy) -> Self {
64        self.repeat = Some(repeat);
65        self
66    }
67
68    #[must_use]
69    pub fn repeat_policy(&self) -> Option<&StateRepeatPolicy> {
70        self.repeat.as_ref()
71    }
72
73    #[must_use]
74    pub fn is_initial(&self) -> bool {
75        self.kind.is_initial()
76    }
77
78    #[must_use]
79    pub fn is_terminal(&self) -> bool {
80        self.kind.is_terminal()
81    }
82}