From 3c42339291a3398c2c61a51b35f3488e4f5ff35c Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 4 May 2025 14:20:15 +0200 Subject: [PATCH] Move note store from gdn-app to gdn --- gdn-app/src-tauri/src/api.rs | 11 ++-- gdn-app/src-tauri/src/lib.rs | 4 +- gdn-app/src-tauri/src/types.rs | 13 ++++- gdn/src/lib.rs | 1 + {gdn-app/src-tauri => gdn}/src/store.rs | 74 +++++++++++++++++++++---- 5 files changed, 81 insertions(+), 22 deletions(-) rename {gdn-app/src-tauri => gdn}/src/store.rs (81%) diff --git a/gdn-app/src-tauri/src/api.rs b/gdn-app/src-tauri/src/api.rs index d29bd43..5417fff 100644 --- a/gdn-app/src-tauri/src/api.rs +++ b/gdn-app/src-tauri/src/api.rs @@ -1,12 +1,9 @@ use std::sync::{Arc, Mutex}; -use gdn::ids::NoteId; +use gdn::{ids::NoteId, store::Store}; use tauri::{AppHandle, Emitter, State}; -use crate::{ - store::Store, - types::{EventNotesStoreUpdate, Note}, -}; +use crate::types::{EventNotesStoreUpdate, Note}; // API methods are sorted alphabetically. @@ -76,7 +73,7 @@ pub fn note_create(text: String, app: AppHandle, store: State<'_, Arc #[tauri::command] pub fn note_get(id: NoteId, store: State<'_, Arc>>) -> Option { let guard = store.lock().unwrap(); - guard.get(id) + guard.get(id).map(|it| it.into()) } #[tauri::command] diff --git a/gdn-app/src-tauri/src/lib.rs b/gdn-app/src-tauri/src/lib.rs index 3d8503a..b72fc08 100644 --- a/gdn-app/src-tauri/src/lib.rs +++ b/gdn-app/src-tauri/src/lib.rs @@ -1,9 +1,9 @@ +use serde_json as _; // Silence unused dependency warning use std::sync::{Arc, Mutex}; -use store::Store; +use gdn::store::Store; mod api; -pub mod store; mod types; #[cfg_attr(mobile, tauri::mobile_entry_point)] diff --git a/gdn-app/src-tauri/src/types.rs b/gdn-app/src-tauri/src/types.rs index 67e44e0..1eed82a 100644 --- a/gdn-app/src-tauri/src/types.rs +++ b/gdn-app/src-tauri/src/types.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -use gdn::ids::NoteId; +use gdn::{ids::NoteId, store::RichNote}; use serde::Serialize; #[derive(Serialize)] @@ -12,6 +12,17 @@ pub struct Note { pub parents: HashSet, } +impl From for Note { + fn from(value: RichNote) -> Self { + Self { + id: value.id, + text: value.text, + children: value.children, + parents: value.parents, + } + } +} + //////////// // Events // //////////// diff --git a/gdn/src/lib.rs b/gdn/src/lib.rs index fdab218..5906acc 100644 --- a/gdn/src/lib.rs +++ b/gdn/src/lib.rs @@ -1,6 +1,7 @@ pub mod data; pub mod ids; pub mod repo; +pub mod store; pub const PROPER_NAME: &str = "GedächtNAS"; pub const TECHNICAL_NAME: &str = "gedaechtnas"; diff --git a/gdn-app/src-tauri/src/store.rs b/gdn/src/store.rs similarity index 81% rename from gdn-app/src-tauri/src/store.rs rename to gdn/src/store.rs index cc21753..2882c92 100644 --- a/gdn-app/src-tauri/src/store.rs +++ b/gdn/src/store.rs @@ -1,21 +1,46 @@ use std::collections::{HashMap, HashSet}; -use gdn::ids::NoteId; +use crate::{ + ids::NoteId, + repo::{Note, Repo}, +}; -use crate::types::Note; - -#[derive(Debug)] -pub struct NoteInfo { +#[derive(Clone)] +pub struct RawNote { pub text: String, pub children: Vec, } -/// A note store for testing. +impl RawNote { + pub fn load(note: Note) -> Self { + Self { + text: note.text, + children: note.children, + } + } + + pub fn save(self, id: NoteId) -> Note { + Note { + id, + text: self.text, + children: self.children, + } + } +} + +#[derive(Clone)] +pub struct RichNote { + pub id: NoteId, + pub text: String, + pub children: Vec, + pub parents: HashSet, +} + #[derive(Default)] pub struct Store { last_id: u64, curr_id: u64, - notes: HashMap, + notes: HashMap, parents: HashMap>, } @@ -24,6 +49,31 @@ impl Store { Self::default() } + pub fn load(repo: Repo) -> Self { + let notes = repo + .notes + .into_iter() + .map(|note| (note.id, RawNote::load(note))) + .collect::>(); + + let mut result = Self { + notes, + ..Self::default() + }; + result.make_consistent_and_tick(); + result + } + + pub fn save(&self) -> Repo { + let notes = self + .notes + .iter() + .map(|(id, note)| note.clone().save(*id)) + .collect::>(); + + Repo { notes } + } + pub fn needs_update(&self) -> Option { if self.last_id != self.curr_id { Some(self.curr_id) @@ -36,7 +86,7 @@ impl Store { self.last_id = self.curr_id; } - pub fn get(&self, id: NoteId) -> Option { + pub fn get(&self, id: NoteId) -> Option { let info = self.notes.get(&id)?; let parents = self @@ -45,7 +95,7 @@ impl Store { .map(|ps| ps.keys().copied().collect::>()) .unwrap_or_default(); - Some(Note { + Some(RichNote { id, text: info.text.clone(), children: info.children.clone(), @@ -82,18 +132,18 @@ impl Store { pub fn create(&mut self, text: String) -> NoteId { let id = NoteId::new(); - let info = NoteInfo { + let note = RawNote { text, children: vec![], }; - self.notes.insert(id, info); + self.notes.insert(id, note); self.make_consistent_and_tick(); id } - pub fn delete(&mut self, id: NoteId) -> Option { + pub fn delete(&mut self, id: NoteId) -> Option { let info = self.notes.remove(&id)?; self.make_consistent_and_tick(); Some(info)