move pub functions to top of notes/mod.rs

This commit is contained in:
Damien Elmes 2021-04-29 14:07:16 +10:00
parent 07b5c35e90
commit 86cf8949ea
3 changed files with 72 additions and 58 deletions

View File

@ -167,7 +167,7 @@ service NotesService {
rpc GetNote(NoteId) returns (Note); rpc GetNote(NoteId) returns (Note);
rpc RemoveNotes(RemoveNotesIn) returns (OpChangesWithCount); rpc RemoveNotes(RemoveNotesIn) returns (OpChangesWithCount);
rpc ClozeNumbersInNote(Note) returns (ClozeNumbersInNoteOut); rpc ClozeNumbersInNote(Note) returns (ClozeNumbersInNoteOut);
rpc AfterNoteUpdates(AfterNoteUpdatesIn) returns (OpChanges); rpc AfterNoteUpdates(AfterNoteUpdatesIn) returns (OpChangesWithCount);
rpc FieldNamesForNotes(FieldNamesForNotesIn) returns (FieldNamesForNotesOut); rpc FieldNamesForNotes(FieldNamesForNotesIn) returns (FieldNamesForNotesOut);
rpc NoteIsDuplicateOrEmpty(Note) returns (NoteIsDuplicateOrEmptyOut); rpc NoteIsDuplicateOrEmpty(Note) returns (NoteIsDuplicateOrEmptyOut);
rpc CardsOfNote(NoteId) returns (CardIds); rpc CardsOfNote(NoteId) returns (CardIds);

View File

@ -97,7 +97,7 @@ impl NotesService for Backend {
}) })
} }
fn after_note_updates(&self, input: pb::AfterNoteUpdatesIn) -> Result<pb::OpChanges> { fn after_note_updates(&self, input: pb::AfterNoteUpdatesIn) -> Result<pb::OpChangesWithCount> {
self.with_col(|col| { self.with_col(|col| {
col.after_note_updates( col.after_note_updates(
&to_note_ids(input.nids), &to_note_ids(input.nids),

View File

@ -49,6 +49,57 @@ pub struct Note {
pub(crate) checksum: Option<u32>, pub(crate) checksum: Option<u32>,
} }
impl Note {
pub fn fields(&self) -> &Vec<String> {
&self.fields
}
pub fn set_field(&mut self, idx: usize, text: impl Into<String>) -> Result<()> {
if idx >= self.fields.len() {
return Err(AnkiError::invalid_input(
"field idx out of range".to_string(),
));
}
self.fields[idx] = text.into();
self.mark_dirty();
Ok(())
}
}
impl Collection {
pub fn add_note(&mut self, note: &mut Note, did: DeckId) -> Result<OpOutput<()>> {
self.transact(Op::AddNote, |col| {
let nt = col
.get_notetype(note.notetype_id)?
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
let ctx = CardGenContext::new(&nt, col.usn()?);
let norm = col.get_bool(BoolKey::NormalizeNoteText);
col.add_note_inner(&ctx, note, did, norm)
})
}
/// Remove provided notes, and any cards that use them.
pub fn remove_notes(&mut self, nids: &[NoteId]) -> Result<OpOutput<usize>> {
let usn = self.usn()?;
self.transact(Op::RemoveNote, |col| col.remove_notes_inner(nids, usn))
}
/// Update cards and field cache after notes modified externally.
/// If gencards is false, skip card generation.
pub fn after_note_updates(
&mut self,
nids: &[NoteId],
generate_cards: bool,
mark_notes_modified: bool,
) -> Result<OpOutput<usize>> {
self.transact(Op::UpdateNote, |col| {
col.after_note_updates_inner(nids, generate_cards, mark_notes_modified)
})
}
}
/// Information required for updating tags while leaving note content alone. /// Information required for updating tags while leaving note content alone.
/// Tags are stored in their DB form, separated by spaces. /// Tags are stored in their DB form, separated by spaces.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@ -106,10 +157,6 @@ impl Note {
} }
} }
pub fn fields(&self) -> &Vec<String> {
&self.fields
}
pub(crate) fn fields_mut(&mut self) -> &mut Vec<String> { pub(crate) fn fields_mut(&mut self) -> &mut Vec<String> {
self.mark_dirty(); self.mark_dirty();
&mut self.fields &mut self.fields
@ -121,21 +168,8 @@ impl Note {
self.checksum = None; self.checksum = None;
} }
pub fn set_field(&mut self, idx: usize, text: impl Into<String>) -> Result<()> {
if idx >= self.fields.len() {
return Err(AnkiError::invalid_input(
"field idx out of range".to_string(),
));
}
self.fields[idx] = text.into();
self.mark_dirty();
Ok(())
}
/// Prepare note for saving to the database. Does not mark it as modified. /// Prepare note for saving to the database. Does not mark it as modified.
pub fn prepare_for_update(&mut self, nt: &Notetype, normalize_text: bool) -> Result<()> { pub(crate) fn prepare_for_update(&mut self, nt: &Notetype, normalize_text: bool) -> Result<()> {
assert!(nt.id == self.notetype_id); assert!(nt.id == self.notetype_id);
let notetype_field_count = nt.fields.len().max(1); let notetype_field_count = nt.fields.len().max(1);
if notetype_field_count != self.fields.len() { if notetype_field_count != self.fields.len() {
@ -292,17 +326,6 @@ impl Collection {
Ok(()) Ok(())
} }
pub fn add_note(&mut self, note: &mut Note, did: DeckId) -> Result<OpOutput<()>> {
self.transact(Op::AddNote, |col| {
let nt = col
.get_notetype(note.notetype_id)?
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
let ctx = CardGenContext::new(&nt, col.usn()?);
let norm = col.get_bool(BoolKey::NormalizeNoteText);
col.add_note_inner(&ctx, note, did, norm)
})
}
pub(crate) fn add_note_inner( pub(crate) fn add_note_inner(
&mut self, &mut self,
ctx: &CardGenContext, ctx: &CardGenContext,
@ -402,43 +425,34 @@ impl Collection {
self.update_note_undoable(note, original) self.update_note_undoable(note, original)
} }
/// Remove provided notes, and any cards that use them. pub(crate) fn remove_notes_inner(&mut self, nids: &[NoteId], usn: Usn) -> Result<usize> {
pub(crate) fn remove_notes(&mut self, nids: &[NoteId]) -> Result<OpOutput<usize>> {
let usn = self.usn()?;
self.transact(Op::RemoveNote, |col| {
let mut card_count = 0; let mut card_count = 0;
for nid in nids { for nid in nids {
let nid = *nid; let nid = *nid;
if let Some(_existing_note) = col.storage.get_note(nid)? { if let Some(_existing_note) = self.storage.get_note(nid)? {
for card in col.storage.all_cards_of_note(nid)? { for card in self.storage.all_cards_of_note(nid)? {
card_count += 1; card_count += 1;
col.remove_card_and_add_grave_undoable(card, usn)?; self.remove_card_and_add_grave_undoable(card, usn)?;
} }
col.remove_note_only_undoable(nid, usn)?; self.remove_note_only_undoable(nid, usn)?;
} }
} }
Ok(card_count) Ok(card_count)
})
} }
/// Update cards and field cache after notes modified externally. fn after_note_updates_inner(
/// If gencards is false, skip card generation.
pub fn after_note_updates(
&mut self, &mut self,
nids: &[NoteId], nids: &[NoteId],
generate_cards: bool, generate_cards: bool,
mark_notes_modified: bool, mark_notes_modified: bool,
) -> Result<OpOutput<()>> { ) -> Result<usize> {
self.transact(Op::UpdateNote, |col| { self.transform_notes(nids, |_note, _nt| {
col.transform_notes(nids, |_note, _nt| {
Ok(TransformNoteOutput { Ok(TransformNoteOutput {
changed: true, changed: true,
generate_cards, generate_cards,
mark_modified: mark_notes_modified, mark_modified: mark_notes_modified,
}) })
}) })
.map(|_| ())
})
} }
pub(crate) fn transform_notes<F>( pub(crate) fn transform_notes<F>(