1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! [`CeremonyCommit`] — everything one step of a ceremony changes.
//!
//! State, audit and publication are three claims about the same moment.
//! Saving them separately lets a process die between two of them and
//! leave a journal that disagrees with the state, or a message that
//! reports something that was never stored. They travel together so
//! they can land together.
use crate::entities::{AuditFact, CeremonyInstance};
use crate::error::DomainError;
use crate::value_objects::{ExpectedRevision, OutboxMessage};
/// The unit that is committed, all of it or none of it.
#[derive(Debug, Clone, PartialEq)]
pub struct CeremonyCommit {
instance: CeremonyInstance,
expected_revision: ExpectedRevision,
facts: Vec<AuditFact>,
messages: Vec<OutboxMessage>,
}
impl CeremonyCommit {
/// Every fact must belong to the instance being committed.
///
/// Rejected here rather than in the adapter: a commit that mixes
/// ceremonies has no correct interpretation, and every
/// implementation would otherwise have to discover that
/// independently.
pub fn new(
instance: CeremonyInstance,
expected_revision: ExpectedRevision,
facts: impl IntoIterator<Item = AuditFact>,
messages: impl IntoIterator<Item = OutboxMessage>,
) -> Result<Self, DomainError> {
let facts = facts.into_iter().collect::<Vec<_>>();
if facts.iter().any(|fact| &fact.ceremony_id != instance.id()) {
return Err(DomainError::InvariantViolated {
reason: "a commit cannot carry audit facts from another ceremony",
});
}
Ok(Self {
instance,
expected_revision,
facts,
messages: messages.into_iter().collect(),
})
}
#[must_use]
pub fn instance(&self) -> &CeremonyInstance {
&self.instance
}
#[must_use]
pub fn expected_revision(&self) -> ExpectedRevision {
self.expected_revision
}
#[must_use]
pub fn facts(&self) -> &[AuditFact] {
&self.facts
}
#[must_use]
pub fn messages(&self) -> &[OutboxMessage] {
&self.messages
}
/// Consume the commit into the parts an adapter writes.
#[must_use]
pub fn into_parts(
self,
) -> (
CeremonyInstance,
ExpectedRevision,
Vec<AuditFact>,
Vec<OutboxMessage>,
) {
(
self.instance,
self.expected_revision,
self.facts,
self.messages,
)
}
}