coven_sync/
operation_outcome.rs1use crate::{OperationKind, OperationRejection, SyncError};
6use coven_database::types::{FromSql, FromSqlError, FromSqlResult, ValueRef};
7use coven_database::{ChangeOutcome, FreshChangeOutcome, ListedOutcome};
8use coven_merge::Slot;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
13pub(crate) enum EntryOutcome {
14 Landed(Slot),
16 Satisfied,
19 Rejected(OperationRejection),
21}
22
23impl EntryOutcome {
24 pub(crate) fn into_slot(self) -> Result<Option<Slot>, SyncError> {
27 match self {
28 Self::Landed(slot) => Ok(Some(slot)),
29 Self::Satisfied => Ok(None),
30 Self::Rejected(reason) => Err(SyncError::Rejected(reason)),
31 }
32 }
33}
34
35impl From<EntryOutcome> for ChangeOutcome {
38 fn from(outcome: EntryOutcome) -> Self {
39 match outcome {
40 EntryOutcome::Landed(slot) => Self::Landed(slot),
41 EntryOutcome::Satisfied => Self::Satisfied,
42 EntryOutcome::Rejected(reason) => Self::Rejected(reason),
43 }
44 }
45}
46
47impl TryFrom<EntryOutcome> for FreshChangeOutcome {
48 type Error = SyncError;
49
50 fn try_from(outcome: EntryOutcome) -> Result<Self, SyncError> {
53 match outcome {
54 EntryOutcome::Landed(slot) => Ok(Self::Landed(slot)),
55 EntryOutcome::Rejected(reason) => Ok(Self::Rejected(reason)),
56 EntryOutcome::Satisfied => Err(coven_database::DbError::Invariant {
57 what: "a change naming a new circle or pair is never satisfied",
58 }
59 .into()),
60 }
61 }
62}
63
64#[cfg(test)]
68pub(crate) trait SettledByPass: std::fmt::Debug + Sized {
69 fn settled(self) -> Result<(), SyncError>;
72}
73
74#[cfg(test)]
75impl SettledByPass for FreshChangeOutcome {
76 fn settled(self) -> Result<(), SyncError> {
77 match self {
78 Self::Landed(_) => Ok(()),
79 Self::Rejected(reason) => Err(SyncError::Rejected(reason)),
80 other => panic!("the pass ended the operation {other:?}"),
81 }
82 }
83}
84
85#[cfg(test)]
86impl SettledByPass for ChangeOutcome {
87 fn settled(self) -> Result<(), SyncError> {
88 match self {
89 Self::Landed(_) | Self::Satisfied => Ok(()),
90 Self::Rejected(reason) => Err(SyncError::Rejected(reason)),
91 other => panic!("the pass ended the operation {other:?}"),
92 }
93 }
94}
95
96#[derive(PartialEq)]
99enum OutcomeType {
100 Change,
101 FreshChange,
102 Invite,
103 StorageSetup,
104}
105
106impl OperationKind {
107 fn outcome_type(self) -> Option<OutcomeType> {
110 use OperationKind as K;
111 match self {
112 K::RenameStore
113 | K::SetRole
114 | K::RemoveMember
115 | K::RemoveDevice
116 | K::RenameCircle
117 | K::DeleteCircle
118 | K::AddCircleMember
119 | K::RemoveCircleMember
120 | K::ApproveJoin => Some(OutcomeType::Change),
121 K::CreateCircle | K::ReplaceMemberKeys => Some(OutcomeType::FreshChange),
122 K::Invite => Some(OutcomeType::Invite),
123 K::StorageSetup => Some(OutcomeType::StorageSetup),
124 K::CreateStore
125 | K::SetUpStorage
126 | K::AddDevice
127 | K::SetAccess
128 | K::RetainSequence
129 | K::RetainFiles
130 | K::WriteSnapshot
131 | K::SchemaChange
132 | K::Reset
133 | K::ReloadFromSnapshot
134 | K::RequiredReload
135 | K::RotateKey
136 | K::SendKeyCopy
137 | K::Pairing
138 | K::RevokeAccess => None,
139 }
140 }
141
142 #[cfg(any(test, feature = "test-utils"))]
145 pub fn lists(self, outcome: &ListedOutcome) -> bool {
146 let of_outcome = match outcome {
147 ListedOutcome::Change(_) => OutcomeType::Change,
148 ListedOutcome::FreshChange(_) => OutcomeType::FreshChange,
149 ListedOutcome::Invite(_) => OutcomeType::Invite,
150 ListedOutcome::StorageSetup(_) => OutcomeType::StorageSetup,
151 };
152 self.outcome_type() == Some(of_outcome)
153 }
154
155 pub(crate) fn listed_outcome(
159 self,
160 outcome: EntryOutcome,
161 ) -> Result<Option<ListedOutcome>, SyncError> {
162 Ok(match self.outcome_type() {
163 Some(OutcomeType::Change) => Some(ChangeOutcome::from(outcome).into()),
164 Some(OutcomeType::FreshChange) => Some(FreshChangeOutcome::try_from(outcome)?.into()),
165 Some(OutcomeType::Invite | OutcomeType::StorageSetup) => {
166 return Err(coven_database::DbError::Invariant {
167 what:
168 "an invite or a storage setup ends with its own outcome, never an entry's",
169 }
170 .into())
171 }
172 None => None,
173 })
174 }
175
176 pub fn listed(self) -> String {
178 match serde_json::to_value(self) {
179 Ok(serde_json::Value::String(name)) => name,
180 other => unreachable!("an operation kind is named by its variant: {other:?}"),
181 }
182 }
183}
184
185impl FromSql for OperationKind {
187 fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
188 serde_json::from_value(serde_json::Value::String(value.as_str()?.to_owned()))
189 .map_err(|error| FromSqlError::Other(Box::new(error)))
190 }
191}