coven_sync/
operation_outcome.rs1use crate::{OperationKind, OperationOutcome, SyncError};
5use coven_database::types::{FromSql, FromSqlError, FromSqlResult, ValueRef};
6use coven_merge::Slot;
7
8pub(crate) fn into_slot(outcome: OperationOutcome) -> Result<Option<Slot>, SyncError> {
11 match outcome {
12 OperationOutcome::Landed(slot) => Ok(Some(slot)),
13 OperationOutcome::Satisfied => Ok(None),
14 OperationOutcome::Rejected(reason) => Err(SyncError::Rejected(reason)),
15 OperationOutcome::Stopped(_) => Err(coven_database::DbError::Invariant {
18 what: "a call waits only for operations a stop doesn't end",
19 }
20 .into()),
21 OperationOutcome::Cancelled | OperationOutcome::Undone(_) => {
24 Err(coven_database::DbError::Invariant {
25 what: "only storage setup is cancelled or undone",
26 }
27 .into())
28 }
29 OperationOutcome::SharingRefused { .. } => Err(coven_database::DbError::Invariant {
31 what: "only an invite ends with a sharing refusal",
32 }
33 .into()),
34 }
35}
36
37impl OperationKind {
38 pub fn listed(self) -> String {
40 match serde_json::to_value(self) {
41 Ok(serde_json::Value::String(name)) => name,
42 other => unreachable!("an operation kind is named by its variant: {other:?}"),
43 }
44 }
45}
46
47impl FromSql for OperationKind {
49 fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
50 serde_json::from_value(serde_json::Value::String(value.as_str()?.to_owned()))
51 .map_err(|error| FromSqlError::Other(Box::new(error)))
52 }
53}