made_core/entities/
ceremony_commit.rs1use crate::entities::{AuditFact, AuditRecord, CeremonyInstance};
10use crate::error::DomainError;
11use crate::value_objects::{CeremonyRevision, ExpectedRevision, OutboxMessage};
12
13#[derive(Debug, Clone, PartialEq)]
15pub struct CeremonyCommit {
16 instance: CeremonyInstance,
17 expected_revision: ExpectedRevision,
18 facts: Vec<AuditFact>,
19 messages: Vec<OutboxMessage>,
20}
21
22impl CeremonyCommit {
23 pub fn new(
30 instance: CeremonyInstance,
31 expected_revision: ExpectedRevision,
32 facts: impl IntoIterator<Item = AuditFact>,
33 messages: impl IntoIterator<Item = OutboxMessage>,
34 ) -> Result<Self, DomainError> {
35 let facts = facts.into_iter().collect::<Vec<_>>();
36 if facts.iter().any(|fact| &fact.ceremony_id != instance.id()) {
37 return Err(DomainError::InvariantViolated {
38 reason: "a commit cannot carry audit facts from another ceremony",
39 });
40 }
41 Ok(Self {
42 instance,
43 expected_revision,
44 facts,
45 messages: messages.into_iter().collect(),
46 })
47 }
48
49 #[must_use]
50 pub fn instance(&self) -> &CeremonyInstance {
51 &self.instance
52 }
53
54 #[must_use]
55 pub fn expected_revision(&self) -> ExpectedRevision {
56 self.expected_revision
57 }
58
59 #[must_use]
60 pub fn facts(&self) -> &[AuditFact] {
61 &self.facts
62 }
63
64 #[must_use]
65 pub fn messages(&self) -> &[OutboxMessage] {
66 &self.messages
67 }
68
69 #[must_use]
71 pub fn into_parts(
72 self,
73 ) -> (
74 CeremonyInstance,
75 ExpectedRevision,
76 Vec<AuditFact>,
77 Vec<OutboxMessage>,
78 ) {
79 (
80 self.instance,
81 self.expected_revision,
82 self.facts,
83 self.messages,
84 )
85 }
86}
87
88#[derive(Debug, Clone, PartialEq)]
95pub enum CommitOutcome {
96 Committed {
97 revision: CeremonyRevision,
98 records: Vec<AuditRecord>,
99 },
100 Conflict {
101 expected: ExpectedRevision,
102 stored: Option<CeremonyRevision>,
103 },
104}
105
106impl CommitOutcome {
107 #[must_use]
108 pub fn committed_revision(&self) -> Option<CeremonyRevision> {
109 match self {
110 Self::Committed { revision, .. } => Some(*revision),
111 Self::Conflict { .. } => None,
112 }
113 }
114
115 #[must_use]
116 pub fn records(&self) -> &[AuditRecord] {
117 match self {
118 Self::Committed { records, .. } => records,
119 Self::Conflict { .. } => &[],
120 }
121 }
122
123 #[must_use]
124 pub fn is_conflict(&self) -> bool {
125 matches!(self, Self::Conflict { .. })
126 }
127}