Skip to main content

open_gpui_canvas/
persistence.rs

1pub const CANVAS_REDB_STORE_FEATURE: &str = "redb-store";
2pub const CANVAS_LORO_CRDT_FEATURE: &str = "loro-crdt";
3pub const CANVAS_RKYV_SNAPSHOT_FEATURE: &str = "rkyv-snapshot";
4
5mod byte_store;
6mod codec;
7mod memory;
8mod store;
9#[cfg(test)]
10mod tests;
11
12pub use byte_store::{
13    CanvasEncodedLogEntry, CanvasPersistenceByteStore, CanvasPersistenceByteStoreAdapter,
14    CanvasPersistenceByteStoreError,
15};
16pub use codec::{
17    CANVAS_PERSISTENCE_CODEC_VERSION, CanvasJsonPersistenceCodec, CanvasPersistenceCodec,
18    CanvasPersistenceCodecError, CanvasPersistenceEnvelope, CanvasPersistenceRecord,
19    CanvasPersistenceRecordKind,
20};
21pub use memory::{MemoryCanvasPersistenceByteStore, MemoryCanvasPersistenceStore};
22pub use store::{
23    CanvasCheckpoint, CanvasLogEntry, CanvasLogEntryKind, CanvasPersistenceCursor,
24    CanvasPersistenceError, CanvasPersistenceStore, CanvasPersistentToolRegistryError,
25    CanvasReplayError, apply_persistent_store_transaction, apply_persistent_tool_intent,
26    apply_persistent_tool_intents, apply_persistent_transaction, handle_persistent_event,
27    handle_persistent_event_with_custom_tool, handle_persistent_event_with_tool_registry,
28    load_canvas_document, load_canvas_persistence_cursor, redo_persistent_store_transaction,
29    redo_persistent_transaction, replay_canvas_log, save_canvas_checkpoint,
30    save_canvas_store_checkpoint, undo_persistent_store_transaction, undo_persistent_transaction,
31};
32
33#[derive(Clone, Copy, Debug, Eq, PartialEq)]
34pub enum CanvasPersistenceAdapter {
35    MemoryStore,
36    RedbStore,
37    LoroCrdt,
38    RkyvSnapshot,
39}
40
41impl CanvasPersistenceAdapter {
42    pub fn feature_name(self) -> Option<&'static str> {
43        match self {
44            Self::MemoryStore => None,
45            Self::RedbStore => Some(CANVAS_REDB_STORE_FEATURE),
46            Self::LoroCrdt => Some(CANVAS_LORO_CRDT_FEATURE),
47            Self::RkyvSnapshot => Some(CANVAS_RKYV_SNAPSHOT_FEATURE),
48        }
49    }
50
51    pub fn feature_enabled(self) -> bool {
52        match self {
53            Self::MemoryStore => true,
54            Self::RedbStore => cfg!(feature = "redb-store"),
55            Self::LoroCrdt => cfg!(feature = "loro-crdt"),
56            Self::RkyvSnapshot => cfg!(feature = "rkyv-snapshot"),
57        }
58    }
59
60    pub fn implemented(self) -> bool {
61        matches!(self, Self::MemoryStore)
62    }
63
64    pub fn status(self) -> CanvasPersistenceAdapterStatus {
65        CanvasPersistenceAdapterStatus {
66            adapter: self,
67            feature_name: self.feature_name(),
68            feature_enabled: self.feature_enabled(),
69            implemented: self.implemented(),
70        }
71    }
72}
73
74#[derive(Clone, Copy, Debug, Eq, PartialEq)]
75pub struct CanvasPersistenceAdapterStatus {
76    pub adapter: CanvasPersistenceAdapter,
77    pub feature_name: Option<&'static str>,
78    pub feature_enabled: bool,
79    pub implemented: bool,
80}
81
82pub const CANVAS_PERSISTENCE_ADAPTERS: &[CanvasPersistenceAdapter] = &[
83    CanvasPersistenceAdapter::MemoryStore,
84    CanvasPersistenceAdapter::RedbStore,
85    CanvasPersistenceAdapter::LoroCrdt,
86    CanvasPersistenceAdapter::RkyvSnapshot,
87];
88
89pub fn canvas_persistence_adapter_statuses() -> [CanvasPersistenceAdapterStatus; 4] {
90    [
91        CanvasPersistenceAdapter::MemoryStore.status(),
92        CanvasPersistenceAdapter::RedbStore.status(),
93        CanvasPersistenceAdapter::LoroCrdt.status(),
94        CanvasPersistenceAdapter::RkyvSnapshot.status(),
95    ]
96}