|
| 1 | +use graphite_desktop_wrapper::messages::{Document, DocumentId}; |
| 2 | + |
| 3 | +#[derive(Default, serde::Serialize, serde::Deserialize)] |
| 4 | +pub(crate) struct PersistentData { |
| 5 | + documents: DocumentStore, |
| 6 | + current_document: Option<DocumentId>, |
| 7 | + #[serde(skip)] |
| 8 | + document_order: Option<Vec<DocumentId>>, |
| 9 | +} |
| 10 | + |
| 11 | +impl PersistentData { |
| 12 | + pub(crate) fn write_document(&mut self, id: DocumentId, document: Document) { |
| 13 | + self.documents.write(id, document); |
| 14 | + if let Some(order) = &self.document_order { |
| 15 | + self.documents.force_order(order.clone()); |
| 16 | + } |
| 17 | + self.flush(); |
| 18 | + } |
| 19 | + |
| 20 | + pub(crate) fn delete_document(&mut self, id: &DocumentId) { |
| 21 | + if Some(*id) == self.current_document { |
| 22 | + self.current_document = None; |
| 23 | + } |
| 24 | + self.documents.delete(id); |
| 25 | + self.flush(); |
| 26 | + } |
| 27 | + |
| 28 | + pub(crate) fn current_document_id(&self) -> Option<DocumentId> { |
| 29 | + match self.current_document { |
| 30 | + Some(id) => Some(id), |
| 31 | + None => Some(*self.documents.document_ids().first()?), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub(crate) fn current_document(&self) -> Option<(DocumentId, Document)> { |
| 36 | + let current_id = self.current_document_id()?; |
| 37 | + Some((current_id, self.documents.read(¤t_id)?)) |
| 38 | + } |
| 39 | + |
| 40 | + pub(crate) fn documents_before_current(&self) -> Vec<(DocumentId, Document)> { |
| 41 | + let Some(current_id) = self.current_document_id() else { |
| 42 | + return Vec::new(); |
| 43 | + }; |
| 44 | + self.documents |
| 45 | + .document_ids() |
| 46 | + .into_iter() |
| 47 | + .take_while(|id| *id != current_id) |
| 48 | + .filter_map(|id| Some((id, self.documents.read(&id)?))) |
| 49 | + .collect() |
| 50 | + } |
| 51 | + |
| 52 | + pub(crate) fn documents_after_current(&self) -> Vec<(DocumentId, Document)> { |
| 53 | + let Some(current_id) = self.current_document_id() else { |
| 54 | + return Vec::new(); |
| 55 | + }; |
| 56 | + self.documents |
| 57 | + .document_ids() |
| 58 | + .into_iter() |
| 59 | + .skip_while(|id| *id != current_id) |
| 60 | + .skip(1) |
| 61 | + .filter_map(|id| Some((id, self.documents.read(&id)?))) |
| 62 | + .collect() |
| 63 | + } |
| 64 | + |
| 65 | + pub(crate) fn set_current_document(&mut self, id: DocumentId) { |
| 66 | + self.current_document = Some(id); |
| 67 | + self.flush(); |
| 68 | + } |
| 69 | + |
| 70 | + pub(crate) fn set_document_order(&mut self, order: Vec<DocumentId>) { |
| 71 | + self.document_order = Some(order); |
| 72 | + self.flush(); |
| 73 | + } |
| 74 | + |
| 75 | + fn flush(&self) { |
| 76 | + let data = match ron::to_string(self) { |
| 77 | + Ok(d) => d, |
| 78 | + Err(e) => { |
| 79 | + tracing::error!("Failed to serialize persistent data: {e}"); |
| 80 | + return; |
| 81 | + } |
| 82 | + }; |
| 83 | + if let Err(e) = std::fs::write(Self::persistence_file_path(), data) { |
| 84 | + tracing::error!("Failed to write persistent data to disk: {e}"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pub(crate) fn load_from_disk(&mut self) { |
| 89 | + let path = Self::persistence_file_path(); |
| 90 | + let data = match std::fs::read_to_string(&path) { |
| 91 | + Ok(d) => d, |
| 92 | + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { |
| 93 | + tracing::info!("No persistent data file found at {path:?}, starting fresh"); |
| 94 | + return; |
| 95 | + } |
| 96 | + Err(e) => { |
| 97 | + tracing::error!("Failed to read persistent data from disk: {e}"); |
| 98 | + return; |
| 99 | + } |
| 100 | + }; |
| 101 | + let loaded = match ron::from_str(&data) { |
| 102 | + Ok(d) => d, |
| 103 | + Err(e) => { |
| 104 | + tracing::error!("Failed to deserialize persistent data: {e}"); |
| 105 | + return; |
| 106 | + } |
| 107 | + }; |
| 108 | + *self = loaded; |
| 109 | + } |
| 110 | + |
| 111 | + fn persistence_file_path() -> std::path::PathBuf { |
| 112 | + let mut path = crate::dirs::graphite_data_dir(); |
| 113 | + path.push(format!("{}.ron", crate::consts::APP_AUTOSAVE_DIRECTORY_NAME)); |
| 114 | + path |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[derive(Default, serde::Serialize, serde::Deserialize)] |
| 119 | +struct DocumentStore(Vec<DocumentInfo>); |
| 120 | +impl DocumentStore { |
| 121 | + fn write(&mut self, id: DocumentId, document: Document) { |
| 122 | + let meta = DocumentInfo::new(id, &document); |
| 123 | + if let Some(existing) = self.0.iter_mut().find(|meta| meta.id == id) { |
| 124 | + *existing = meta; |
| 125 | + } else { |
| 126 | + self.0.push(meta); |
| 127 | + } |
| 128 | + if let Err(e) = std::fs::write(Self::document_path(&id), document.content) { |
| 129 | + tracing::error!("Failed to write document {id:?} to disk: {e}"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fn delete(&mut self, id: &DocumentId) { |
| 134 | + self.0.retain(|meta| meta.id != *id); |
| 135 | + if let Err(e) = std::fs::remove_file(Self::document_path(id)) { |
| 136 | + tracing::error!("Failed to delete document {id:?} from disk: {e}"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fn read(&self, id: &DocumentId) -> Option<Document> { |
| 141 | + let meta = self.0.iter().find(|meta| meta.id == *id)?; |
| 142 | + let content = std::fs::read_to_string(Self::document_path(id)).ok()?; |
| 143 | + Some(Document { |
| 144 | + content, |
| 145 | + name: meta.name.clone(), |
| 146 | + path: meta.path.clone(), |
| 147 | + is_saved: meta.is_saved, |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + fn force_order(&mut self, desired_order: Vec<DocumentId>) { |
| 152 | + let mut ordered_prefix_len = 0; |
| 153 | + for id in desired_order { |
| 154 | + if let Some(offset) = self.0[ordered_prefix_len..].iter().position(|meta| meta.id == id) { |
| 155 | + let found_index = ordered_prefix_len + offset; |
| 156 | + if found_index != ordered_prefix_len { |
| 157 | + self.0[ordered_prefix_len..=found_index].rotate_right(1); |
| 158 | + } |
| 159 | + ordered_prefix_len += 1; |
| 160 | + } |
| 161 | + } |
| 162 | + self.0.truncate(ordered_prefix_len); |
| 163 | + } |
| 164 | + |
| 165 | + fn document_ids(&self) -> Vec<DocumentId> { |
| 166 | + self.0.iter().map(|meta| meta.id).collect() |
| 167 | + } |
| 168 | + |
| 169 | + fn document_path(id: &DocumentId) -> std::path::PathBuf { |
| 170 | + let mut path = crate::dirs::graphite_autosave_documents_dir(); |
| 171 | + path.push(format!("{:x}.graphite", id.0)); |
| 172 | + path |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +#[derive(serde::Serialize, serde::Deserialize)] |
| 177 | +struct DocumentInfo { |
| 178 | + id: DocumentId, |
| 179 | + name: String, |
| 180 | + path: Option<std::path::PathBuf>, |
| 181 | + is_saved: bool, |
| 182 | +} |
| 183 | +impl DocumentInfo { |
| 184 | + fn new(id: DocumentId, Document { name, path, is_saved, .. }: &Document) -> Self { |
| 185 | + Self { |
| 186 | + id, |
| 187 | + name: name.clone(), |
| 188 | + path: path.clone(), |
| 189 | + is_saved: *is_saved, |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments