Skip to main content

syncular_runtime/core/
command_history.rs

1use crate::error::{Result, SyncularError};
2use crate::protocol::MutationReceipt;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::fmt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub enum CommandHistoryState {
10    Done,
11    Undone,
12}
13
14impl CommandHistoryState {
15    pub fn as_str(self) -> &'static str {
16        match self {
17            Self::Done => "done",
18            Self::Undone => "undone",
19        }
20    }
21}
22
23impl TryFrom<&str> for CommandHistoryState {
24    type Error = SyncularError;
25
26    fn try_from(value: &str) -> Result<Self> {
27        match value {
28            "done" => Ok(Self::Done),
29            "undone" => Ok(Self::Undone),
30            _ => Err(SyncularError::storage(anyhow::anyhow!(
31                "invalid sync_command_history.state: {value}"
32            ))),
33        }
34    }
35}
36
37impl fmt::Display for CommandHistoryState {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(self.as_str())
40    }
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44#[serde(rename_all = "camelCase")]
45pub struct CommandHistoryEntry {
46    pub table: String,
47    pub row_id: String,
48    pub before: Option<Value>,
49    pub after: Option<Value>,
50}
51
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53#[serde(rename_all = "camelCase")]
54pub struct CommandHistoryRecord {
55    pub id: String,
56    pub mutation_scope: String,
57    pub state: CommandHistoryState,
58    pub entries: Vec<CommandHistoryEntry>,
59    pub client_commit_id: String,
60    pub undo_client_commit_id: Option<String>,
61    pub redo_client_commit_id: Option<String>,
62    pub created_at: i64,
63    pub updated_at: i64,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68pub struct CommandHistoryReceipt {
69    pub command_id: String,
70    pub commit: MutationReceipt,
71}