Skip to main content

made_core/value_objects/ceremony/
ceremony_state.rs

1use serde::{Deserialize, Serialize};
2
3use super::{CeremonyStateKind, StateExecution, StateId, StateRepeatPolicy};
4use crate::value_objects::Attributes;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct CeremonyState {
8    id: StateId,
9    kind: CeremonyStateKind,
10    #[serde(default, skip_serializing_if = "StateExecution::is_sequential")]
11    execution: StateExecution,
12    #[serde(default, skip_serializing_if = "Option::is_none")]
13    repeat: Option<StateRepeatPolicy>,
14    #[serde(default, skip_serializing_if = "Attributes::is_empty")]
15    annotations: Attributes,
16}
17
18impl CeremonyState {
19    #[must_use]
20    pub fn new(id: StateId, kind: CeremonyStateKind) -> Self {
21        Self {
22            id,
23            kind,
24            execution: StateExecution::Sequential,
25            repeat: None,
26            annotations: Attributes::empty(),
27        }
28    }
29
30    #[must_use]
31    pub fn initial(id: StateId) -> Self {
32        Self::new(id, CeremonyStateKind::Initial)
33    }
34
35    #[must_use]
36    pub fn intermediate(id: StateId) -> Self {
37        Self::new(id, CeremonyStateKind::Intermediate)
38    }
39
40    #[must_use]
41    pub fn terminal(id: StateId) -> Self {
42        Self::new(id, CeremonyStateKind::Terminal)
43    }
44
45    #[must_use]
46    pub fn id(&self) -> &StateId {
47        &self.id
48    }
49
50    #[must_use]
51    pub fn kind(&self) -> CeremonyStateKind {
52        self.kind
53    }
54
55    #[must_use]
56    pub fn with_execution(mut self, execution: StateExecution) -> Self {
57        self.execution = execution;
58        self
59    }
60
61    #[must_use]
62    pub fn execution(&self) -> StateExecution {
63        self.execution
64    }
65
66    #[must_use]
67    pub fn with_repeat_policy(mut self, repeat: StateRepeatPolicy) -> Self {
68        self.repeat = Some(repeat);
69        self
70    }
71
72    #[must_use]
73    pub fn repeat_policy(&self) -> Option<&StateRepeatPolicy> {
74        self.repeat.as_ref()
75    }
76
77    /// Opaque authoring and rendering metadata. The state machine never
78    /// interprets these values when deciding or folding commands.
79    #[must_use]
80    pub fn with_annotations(mut self, annotations: Attributes) -> Self {
81        self.annotations = annotations;
82        self
83    }
84
85    #[must_use]
86    pub fn annotations(&self) -> &Attributes {
87        &self.annotations
88    }
89
90    #[must_use]
91    pub fn is_initial(&self) -> bool {
92        self.kind.is_initial()
93    }
94
95    #[must_use]
96    pub fn is_terminal(&self) -> bool {
97        self.kind.is_terminal()
98    }
99}