Skip to main content

made_core/value_objects/ceremony/
ceremony_lifecycle.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4use super::{CeremonyEndReason, CeremonyLifecyclePhase, LifecycleReason};
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct CeremonyLifecycle {
8    phase: CeremonyLifecyclePhase,
9    #[serde(default, skip_serializing_if = "Option::is_none")]
10    end_reason: Option<CeremonyEndReason>,
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    reason: Option<LifecycleReason>,
13    #[serde(
14        default,
15        with = "time::serde::rfc3339::option",
16        skip_serializing_if = "Option::is_none"
17    )]
18    changed_at: Option<OffsetDateTime>,
19}
20
21impl Default for CeremonyLifecycle {
22    fn default() -> Self {
23        Self {
24            phase: CeremonyLifecyclePhase::Running,
25            end_reason: None,
26            reason: None,
27            changed_at: None,
28        }
29    }
30}
31
32impl CeremonyLifecycle {
33    #[must_use]
34    pub fn completed(at: OffsetDateTime) -> Self {
35        Self {
36            phase: CeremonyLifecyclePhase::Ended,
37            end_reason: Some(CeremonyEndReason::Completed),
38            reason: None,
39            changed_at: Some(at),
40        }
41    }
42
43    #[must_use]
44    pub fn phase(&self) -> CeremonyLifecyclePhase {
45        self.phase
46    }
47    #[must_use]
48    pub fn end_reason(&self) -> Option<CeremonyEndReason> {
49        self.end_reason
50    }
51    #[must_use]
52    pub fn reason(&self) -> Option<&LifecycleReason> {
53        self.reason.as_ref()
54    }
55    #[must_use]
56    pub fn changed_at(&self) -> Option<OffsetDateTime> {
57        self.changed_at
58    }
59    #[must_use]
60    pub fn admits_new_work(&self) -> bool {
61        self.phase == CeremonyLifecyclePhase::Running
62    }
63    #[must_use]
64    pub fn is_paused(&self) -> bool {
65        self.phase == CeremonyLifecyclePhase::Paused
66    }
67    #[must_use]
68    pub fn is_ended(&self) -> bool {
69        self.phase == CeremonyLifecyclePhase::Ended
70    }
71    #[must_use]
72    pub fn is_explicit(&self) -> bool {
73        self.changed_at.is_some()
74    }
75    #[must_use]
76    pub fn is_default(&self) -> bool {
77        self == &Self::default()
78    }
79    pub(crate) fn pause(&mut self, reason: LifecycleReason, at: OffsetDateTime) {
80        self.phase = CeremonyLifecyclePhase::Paused;
81        self.reason = Some(reason);
82        self.changed_at = Some(at);
83    }
84    pub(crate) fn resume(&mut self, at: OffsetDateTime) {
85        self.phase = CeremonyLifecyclePhase::Running;
86        self.reason = None;
87        self.changed_at = Some(at);
88    }
89    pub(crate) fn end(
90        &mut self,
91        end_reason: CeremonyEndReason,
92        reason: Option<LifecycleReason>,
93        at: OffsetDateTime,
94    ) {
95        self.phase = CeremonyLifecyclePhase::Ended;
96        self.end_reason = Some(end_reason);
97        self.reason = reason;
98        self.changed_at = Some(at);
99    }
100}