open_gpui_canvas/persistence/
memory.rs1use super::{
2 CanvasCheckpoint, CanvasEncodedLogEntry, CanvasLogEntry, CanvasPersistenceByteStore,
3 CanvasPersistenceStore,
4};
5use std::convert::Infallible;
6
7#[derive(Clone, Debug, Default, PartialEq)]
8pub struct MemoryCanvasPersistenceStore {
9 checkpoint: Option<CanvasCheckpoint>,
10 log_entries: Vec<CanvasLogEntry>,
11}
12
13impl MemoryCanvasPersistenceStore {
14 pub fn checkpoint(&self) -> Option<&CanvasCheckpoint> {
15 self.checkpoint.as_ref()
16 }
17
18 pub fn log_entries(&self) -> &[CanvasLogEntry] {
19 &self.log_entries
20 }
21}
22
23impl CanvasPersistenceStore for MemoryCanvasPersistenceStore {
24 type Error = Infallible;
25
26 fn load_checkpoint(&self) -> Result<Option<CanvasCheckpoint>, Self::Error> {
27 Ok(self.checkpoint.clone())
28 }
29
30 fn save_checkpoint(&mut self, checkpoint: CanvasCheckpoint) -> Result<(), Self::Error> {
31 self.checkpoint = Some(checkpoint);
32 Ok(())
33 }
34
35 fn append_log_entry(&mut self, entry: CanvasLogEntry) -> Result<(), Self::Error> {
36 self.log_entries.push(entry);
37 Ok(())
38 }
39
40 fn load_log_entries(&self, after_sequence: u64) -> Result<Vec<CanvasLogEntry>, Self::Error> {
41 Ok(self
42 .log_entries
43 .iter()
44 .filter(|entry| entry.sequence() > after_sequence)
45 .cloned()
46 .collect())
47 }
48
49 fn compact_log_entries(&mut self, through_sequence: u64) -> Result<(), Self::Error> {
50 self.log_entries
51 .retain(|entry| entry.sequence() > through_sequence);
52 Ok(())
53 }
54}
55
56#[derive(Clone, Debug, Default, PartialEq)]
57pub struct MemoryCanvasPersistenceByteStore {
58 checkpoint: Option<Vec<u8>>,
59 log_entries: Vec<CanvasEncodedLogEntry>,
60}
61
62impl MemoryCanvasPersistenceByteStore {
63 pub fn checkpoint_bytes(&self) -> Option<&[u8]> {
64 self.checkpoint.as_deref()
65 }
66
67 pub fn encoded_log_entries(&self) -> &[CanvasEncodedLogEntry] {
68 &self.log_entries
69 }
70}
71
72impl CanvasPersistenceByteStore for MemoryCanvasPersistenceByteStore {
73 type Error = Infallible;
74
75 fn load_checkpoint_bytes(&self) -> Result<Option<Vec<u8>>, Self::Error> {
76 Ok(self.checkpoint.clone())
77 }
78
79 fn save_checkpoint_bytes(&mut self, bytes: Vec<u8>) -> Result<(), Self::Error> {
80 self.checkpoint = Some(bytes);
81 Ok(())
82 }
83
84 fn append_log_entry_bytes(&mut self, sequence: u64, bytes: Vec<u8>) -> Result<(), Self::Error> {
85 self.log_entries
86 .push(CanvasEncodedLogEntry::new(sequence, bytes));
87 Ok(())
88 }
89
90 fn load_log_entry_bytes(
91 &self,
92 after_sequence: u64,
93 ) -> Result<Vec<CanvasEncodedLogEntry>, Self::Error> {
94 Ok(self
95 .log_entries
96 .iter()
97 .filter(|entry| entry.sequence > after_sequence)
98 .cloned()
99 .collect())
100 }
101
102 fn compact_log_entry_bytes(&mut self, through_sequence: u64) -> Result<(), Self::Error> {
103 self.log_entries
104 .retain(|entry| entry.sequence > through_sequence);
105 Ok(())
106 }
107}