From ad74e26c849ccef45615734ebbf3245507a55e47 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 6 Mar 2021 15:48:59 +1000 Subject: [PATCH] move note undo into separate file --- rslib/src/card/mod.rs | 2 +- rslib/src/card/undo.rs | 10 +-- rslib/src/{notes.rs => notes/mod.rs} | 105 ++------------------------- rslib/src/notes/undo.rs | 104 ++++++++++++++++++++++++++ rslib/src/sync/mod.rs | 2 +- 5 files changed, 117 insertions(+), 106 deletions(-) rename rslib/src/{notes.rs => notes/mod.rs} (87%) create mode 100644 rslib/src/notes/undo.rs diff --git a/rslib/src/card/mod.rs b/rslib/src/card/mod.rs index f4eefdcbc..bb1781f7a 100644 --- a/rslib/src/card/mod.rs +++ b/rslib/src/card/mod.rs @@ -183,7 +183,7 @@ impl Collection { } for nid in nids { if self.storage.note_is_orphaned(nid)? { - self.remove_note_only(nid, usn)?; + self.remove_note_only_undoable(nid, usn)?; } } diff --git a/rslib/src/card/undo.rs b/rslib/src/card/undo.rs index 685ca776c..5852c079a 100644 --- a/rslib/src/card/undo.rs +++ b/rslib/src/card/undo.rs @@ -8,7 +8,7 @@ use crate::prelude::*; pub(crate) struct CardAdded(Card); impl Undo for CardAdded { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { + fn undo(self: Box, col: &mut Collection) -> Result<()> { col.remove_card_only(self.0) } } @@ -17,7 +17,7 @@ impl Undo for CardAdded { pub(crate) struct CardRemoved(Card); impl Undo for CardRemoved { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { + fn undo(self: Box, col: &mut Collection) -> Result<()> { col.readd_deleted_card(self.0) } } @@ -26,7 +26,7 @@ impl Undo for CardRemoved { pub(crate) struct CardGraveAdded(CardID, Usn); impl Undo for CardGraveAdded { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { + fn undo(self: Box, col: &mut Collection) -> Result<()> { col.remove_card_grave(self.0, self.1) } } @@ -35,7 +35,7 @@ impl Undo for CardGraveAdded { pub(crate) struct CardGraveRemoved(CardID, Usn); impl Undo for CardGraveRemoved { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { + fn undo(self: Box, col: &mut Collection) -> Result<()> { col.add_card_grave_undoable(self.0, self.1) } } @@ -44,7 +44,7 @@ impl Undo for CardGraveRemoved { pub(crate) struct CardUpdated(Card); impl Undo for CardUpdated { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { + fn undo(self: Box, col: &mut Collection) -> Result<()> { let current = col .storage .get_card(self.0.id)? diff --git a/rslib/src/notes.rs b/rslib/src/notes/mod.rs similarity index 87% rename from rslib/src/notes.rs rename to rslib/src/notes/mod.rs index e5a8de5ca..bfb268d15 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes/mod.rs @@ -1,6 +1,9 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +mod undo; + +use crate::backend_proto::note_is_duplicate_or_empty_out::State as DuplicateState; use crate::{ backend_proto as pb, decks::DeckID, @@ -13,7 +16,6 @@ use crate::{ timestamp::TimestampSecs, types::Usn, }; -use crate::{backend_proto::note_is_duplicate_or_empty_out::State as DuplicateState, undo::Undo}; use itertools::Itertools; use num_integer::Integer; use regex::{Regex, Replacer}; @@ -318,17 +320,10 @@ impl Collection { self.canonify_note_tags(note, ctx.usn)?; note.prepare_for_update(&ctx.notetype, normalize_text)?; note.set_modified(ctx.usn); - self.storage.add_note(note)?; - self.save_undo(Box::new(NoteAdded(note.clone()))); + self.add_note_only_undoable(note)?; self.generate_cards_for_new_note(ctx, note, did) } - fn add_note_for_undo(&mut self, note: Note) -> Result<()> { - self.storage.add_or_update_note(¬e)?; - self.save_undo(Box::new(NoteAdded(note))); - Ok(()) - } - pub fn update_note(&mut self, note: &mut Note) -> Result<()> { self.update_note_with_op(note, Some(UndoableOp::UpdateNote)) } @@ -387,46 +382,7 @@ impl Collection { if mark_note_modified { note.set_modified(usn); } - self.update_note_inner_undo_only(note, original) - } - - /// Saves in the undo queue, and commits to DB. - /// No validation, card generation or normalization is done. - pub(crate) fn update_note_inner_undo_only( - &mut self, - note: &mut Note, - original: &Note, - ) -> Result<()> { - self.save_undo(Box::new(NoteUpdated(original.clone()))); - self.storage.update_note(note)?; - - Ok(()) - } - - /// Remove a note. Cards must already have been deleted. - pub(crate) fn remove_note_only(&mut self, nid: NoteID, usn: Usn) -> Result<()> { - if let Some(note) = self.storage.get_note(nid)? { - self.save_undo(Box::new(NoteRemoved(note))); - self.storage.remove_note(nid)?; - self.add_note_grave(nid, usn)?; - } - Ok(()) - } - - fn add_note_grave(&mut self, nid: NoteID, usn: Usn) -> Result<()> { - self.save_undo(Box::new(NoteGraveAdded(nid, usn))); - self.storage.add_note_grave(nid, usn) - } - - fn remove_note_grave_for_undo(&mut self, nid: NoteID, usn: Usn) -> Result<()> { - self.save_undo(Box::new(NoteGraveRemoved(nid, usn))); - self.storage.remove_note_grave(nid) - } - - fn remove_note_for_undo(&mut self, note: Note) -> Result<()> { - self.storage.remove_note(note.id)?; - self.save_undo(Box::new(NoteRemoved(note))); - Ok(()) + self.update_note_undoable(note, original) } /// Remove provided notes, and any cards that use them. @@ -439,7 +395,7 @@ impl Collection { for card in col.storage.all_cards_of_note(nid)? { col.remove_card_and_add_grave_undoable(card, usn)?; } - col.remove_note_only(nid, usn)?; + col.remove_note_only_undoable(nid, usn)?; } } @@ -590,55 +546,6 @@ fn note_modified(existing_note: &mut Note, note: &Note) -> bool { notes_differ } -#[derive(Debug)] -pub(crate) struct NoteAdded(Note); - -impl Undo for NoteAdded { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { - col.remove_note_for_undo(self.0) - } -} - -#[derive(Debug)] -pub(crate) struct NoteRemoved(Note); - -impl Undo for NoteRemoved { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { - col.add_note_for_undo(self.0) - } -} - -#[derive(Debug)] -pub(crate) struct NoteGraveAdded(NoteID, Usn); - -impl Undo for NoteGraveAdded { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { - col.remove_note_grave_for_undo(self.0, self.1) - } -} - -#[derive(Debug)] -pub(crate) struct NoteGraveRemoved(NoteID, Usn); - -impl Undo for NoteGraveRemoved { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { - col.add_note_grave(self.0, self.1) - } -} - -#[derive(Debug)] -pub(crate) struct NoteUpdated(Note); - -impl Undo for NoteUpdated { - fn undo(self: Box, col: &mut crate::collection::Collection) -> Result<()> { - let current = col - .storage - .get_note(self.0.id)? - .ok_or_else(|| AnkiError::invalid_input("note disappeared"))?; - col.update_note_inner_undo_only(&mut self.0.clone(), ¤t) - } -} - #[cfg(test)] mod test { use super::{anki_base91, field_checksum}; diff --git a/rslib/src/notes/undo.rs b/rslib/src/notes/undo.rs new file mode 100644 index 000000000..ee33ee2f5 --- /dev/null +++ b/rslib/src/notes/undo.rs @@ -0,0 +1,104 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +use crate::prelude::*; +use crate::undo::Undo; + +#[derive(Debug)] +pub(crate) struct NoteAdded(Note); + +impl Undo for NoteAdded { + fn undo(self: Box, col: &mut Collection) -> Result<()> { + col.remove_note_for_undo(self.0) + } +} + +#[derive(Debug)] +pub(crate) struct NoteRemoved(Note); + +impl Undo for NoteRemoved { + fn undo(self: Box, col: &mut Collection) -> Result<()> { + col.add_note_for_undo(self.0) + } +} + +#[derive(Debug)] +pub(crate) struct NoteGraveAdded(NoteID, Usn); + +impl Undo for NoteGraveAdded { + fn undo(self: Box, col: &mut Collection) -> Result<()> { + col.remove_note_grave_for_undo(self.0, self.1) + } +} + +#[derive(Debug)] +pub(crate) struct NoteGraveRemoved(NoteID, Usn); + +impl Undo for NoteGraveRemoved { + fn undo(self: Box, col: &mut Collection) -> Result<()> { + col.add_note_grave(self.0, self.1) + } +} + +#[derive(Debug)] +pub(crate) struct NoteUpdated(Note); + +impl Undo for NoteUpdated { + fn undo(self: Box, col: &mut Collection) -> Result<()> { + let current = col + .storage + .get_note(self.0.id)? + .ok_or_else(|| AnkiError::invalid_input("note disappeared"))?; + col.update_note_undoable(&mut self.0.clone(), ¤t) + } +} + +impl Collection { + /// Saves in the undo queue, and commits to DB. + /// No validation, card generation or normalization is done. + pub(super) fn update_note_undoable(&mut self, note: &mut Note, original: &Note) -> Result<()> { + self.save_undo(Box::new(NoteUpdated(original.clone()))); + self.storage.update_note(note)?; + + Ok(()) + } + + /// Remove a note. Cards must already have been deleted. + pub(crate) fn remove_note_only_undoable(&mut self, nid: NoteID, usn: Usn) -> Result<()> { + if let Some(note) = self.storage.get_note(nid)? { + self.save_undo(Box::new(NoteRemoved(note))); + self.storage.remove_note(nid)?; + self.add_note_grave(nid, usn)?; + } + Ok(()) + } + + /// Add a note, not adding any cards. + pub(super) fn add_note_only_undoable(&mut self, note: &mut Note) -> Result<(), AnkiError> { + self.storage.add_note(note)?; + self.save_undo(Box::new(NoteAdded(note.clone()))); + Ok(()) + } + + fn add_note_grave(&mut self, nid: NoteID, usn: Usn) -> Result<()> { + self.save_undo(Box::new(NoteGraveAdded(nid, usn))); + self.storage.add_note_grave(nid, usn) + } + + fn remove_note_grave_for_undo(&mut self, nid: NoteID, usn: Usn) -> Result<()> { + self.save_undo(Box::new(NoteGraveRemoved(nid, usn))); + self.storage.remove_note_grave(nid) + } + + fn remove_note_for_undo(&mut self, note: Note) -> Result<()> { + self.storage.remove_note(note.id)?; + self.save_undo(Box::new(NoteRemoved(note))); + Ok(()) + } + + fn add_note_for_undo(&mut self, note: Note) -> Result<()> { + self.storage.add_or_update_note(¬e)?; + self.save_undo(Box::new(NoteAdded(note))); + Ok(()) + } +} diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index 09fb768fc..7f43a9c8f 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -1531,7 +1531,7 @@ mod test { // fixme: inconsistent usn arg col1.remove_cards_and_orphaned_notes(&[cardid])?; let usn = col1.usn()?; - col1.remove_note_only(noteid, usn)?; + col1.remove_note_only_undoable(noteid, usn)?; col1.remove_deck_and_child_decks(deckid)?; let out = ctx.normal_sync(&mut col1).await;