2020-02-10 05:19:39 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-05-24 11:48:56 +02:00
|
|
|
use crate::backend_proto::note_is_duplicate_or_empty_out::State as DuplicateState;
|
2020-04-17 06:36:45 +02:00
|
|
|
use crate::{
|
2020-04-23 02:10:28 +02:00
|
|
|
backend_proto as pb,
|
2020-04-17 06:36:45 +02:00
|
|
|
collection::Collection,
|
2020-04-23 02:10:28 +02:00
|
|
|
decks::DeckID,
|
2020-04-17 06:36:45 +02:00
|
|
|
define_newtype,
|
|
|
|
err::{AnkiError, Result},
|
|
|
|
notetype::{CardGenContext, NoteField, NoteType, NoteTypeID},
|
2020-05-06 12:06:42 +02:00
|
|
|
text::{ensure_string_in_nfc, strip_html_preserving_image_filenames},
|
2020-04-17 06:36:45 +02:00
|
|
|
timestamp::TimestampSecs,
|
|
|
|
types::Usn,
|
|
|
|
};
|
2020-05-06 05:14:57 +02:00
|
|
|
use itertools::Itertools;
|
2020-04-16 09:02:59 +02:00
|
|
|
use num_integer::Integer;
|
2020-05-07 09:54:23 +02:00
|
|
|
use regex::{Regex, Replacer};
|
2020-05-13 02:35:01 +02:00
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
collections::{HashMap, HashSet},
|
|
|
|
convert::TryInto,
|
|
|
|
};
|
2020-02-10 05:19:39 +01:00
|
|
|
|
2020-03-26 05:42:43 +01:00
|
|
|
define_newtype!(NoteID, i64);
|
|
|
|
|
2020-04-14 07:41:01 +02:00
|
|
|
// fixme: ensure nulls and x1f not in field contents
|
|
|
|
|
2020-05-07 09:54:23 +02:00
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct TransformNoteOutput {
|
|
|
|
pub changed: bool,
|
|
|
|
pub generate_cards: bool,
|
|
|
|
pub mark_modified: bool,
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:00:59 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-04-14 07:41:01 +02:00
|
|
|
pub struct Note {
|
2020-03-26 05:42:43 +01:00
|
|
|
pub id: NoteID,
|
2020-04-14 07:41:01 +02:00
|
|
|
pub guid: String,
|
2020-03-26 06:00:24 +01:00
|
|
|
pub ntid: NoteTypeID,
|
2020-03-26 03:59:51 +01:00
|
|
|
pub mtime: TimestampSecs,
|
2020-02-10 05:19:39 +01:00
|
|
|
pub usn: Usn,
|
2020-04-14 07:41:01 +02:00
|
|
|
pub tags: Vec<String>,
|
2020-04-16 09:02:59 +02:00
|
|
|
pub(crate) fields: Vec<String>,
|
2020-04-14 07:41:01 +02:00
|
|
|
pub(crate) sort_field: Option<String>,
|
|
|
|
pub(crate) checksum: Option<u32>,
|
2020-02-10 05:19:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Note {
|
2020-04-17 06:36:45 +02:00
|
|
|
pub(crate) fn new(notetype: &NoteType) -> Self {
|
2020-04-16 09:02:59 +02:00
|
|
|
Note {
|
|
|
|
id: NoteID(0),
|
|
|
|
guid: guid(),
|
2020-04-17 06:36:45 +02:00
|
|
|
ntid: notetype.id,
|
2020-04-16 09:02:59 +02:00
|
|
|
mtime: TimestampSecs(0),
|
|
|
|
usn: Usn(0),
|
|
|
|
tags: vec![],
|
2020-04-17 06:36:45 +02:00
|
|
|
fields: vec!["".to_string(); notetype.fields.len()],
|
2020-04-16 09:02:59 +02:00
|
|
|
sort_field: None,
|
|
|
|
checksum: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 05:19:39 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-14 07:41:01 +02:00
|
|
|
|
2020-05-06 05:14:57 +02:00
|
|
|
/// Prepare note for saving to the database. Does not mark it as modified.
|
2020-05-06 12:06:42 +02:00
|
|
|
pub fn prepare_for_update(&mut self, nt: &NoteType, normalize_text: bool) -> Result<()> {
|
2020-04-17 06:36:45 +02:00
|
|
|
assert!(nt.id == self.ntid);
|
|
|
|
if nt.fields.len() != self.fields.len() {
|
|
|
|
return Err(AnkiError::invalid_input(format!(
|
|
|
|
"note has {} fields, expected {}",
|
|
|
|
self.fields.len(),
|
|
|
|
nt.fields.len()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:06:42 +02:00
|
|
|
if normalize_text {
|
|
|
|
for field in &mut self.fields {
|
|
|
|
ensure_string_in_nfc(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:41:01 +02:00
|
|
|
let field1_nohtml = strip_html_preserving_image_filenames(&self.fields()[0]);
|
|
|
|
let checksum = field_checksum(field1_nohtml.as_ref());
|
2020-04-17 06:36:45 +02:00
|
|
|
let sort_field = if nt.config.sort_field_idx == 0 {
|
2020-04-14 07:41:01 +02:00
|
|
|
field1_nohtml
|
|
|
|
} else {
|
|
|
|
strip_html_preserving_image_filenames(
|
|
|
|
self.fields
|
2020-04-17 06:36:45 +02:00
|
|
|
.get(nt.config.sort_field_idx as usize)
|
2020-04-14 07:41:01 +02:00
|
|
|
.map(AsRef::as_ref)
|
|
|
|
.unwrap_or(""),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
self.sort_field = Some(sort_field.into());
|
|
|
|
self.checksum = Some(checksum);
|
2020-04-17 06:36:45 +02:00
|
|
|
Ok(())
|
2020-04-14 07:41:01 +02:00
|
|
|
}
|
2020-04-16 09:02:59 +02:00
|
|
|
|
2020-05-06 05:14:57 +02:00
|
|
|
pub(crate) fn set_modified(&mut self, usn: Usn) {
|
|
|
|
self.mtime = TimestampSecs::now();
|
|
|
|
self.usn = usn;
|
|
|
|
}
|
|
|
|
|
2020-04-17 06:36:45 +02:00
|
|
|
pub(crate) fn nonempty_fields<'a>(&self, fields: &'a [NoteField]) -> HashSet<&'a str> {
|
2020-04-16 09:02:59 +02:00
|
|
|
self.fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(ord, s)| {
|
|
|
|
if s.trim().is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-04-17 06:36:45 +02:00
|
|
|
fields.get(ord).map(|f| f.name.as_str())
|
2020-04-16 09:02:59 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2020-05-07 09:54:23 +02:00
|
|
|
|
2020-05-13 02:35:01 +02:00
|
|
|
pub(crate) fn fields_map<'a>(
|
|
|
|
&'a self,
|
|
|
|
fields: &'a [NoteField],
|
|
|
|
) -> HashMap<&'a str, Cow<'a, str>> {
|
|
|
|
self.fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(ord, field_content)| {
|
|
|
|
(
|
|
|
|
fields.get(ord).map(|f| f.name.as_str()).unwrap_or(""),
|
|
|
|
field_content.as_str().into(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:54:23 +02:00
|
|
|
pub(crate) fn replace_tags<T: Replacer>(&mut self, re: &Regex, mut repl: T) -> bool {
|
|
|
|
let mut changed = false;
|
|
|
|
for tag in &mut self.tags {
|
|
|
|
if let Cow::Owned(rep) = re.replace_all(tag, repl.by_ref()) {
|
|
|
|
*tag = rep;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed
|
|
|
|
}
|
2020-05-10 05:50:04 +02:00
|
|
|
|
|
|
|
/// Pad or merge fields to match note type.
|
|
|
|
pub(crate) fn fix_field_count(&mut self, nt: &NoteType) {
|
|
|
|
while self.fields.len() < nt.fields.len() {
|
|
|
|
self.fields.push("".into())
|
|
|
|
}
|
|
|
|
while self.fields.len() > nt.fields.len() && self.fields.len() > 1 {
|
|
|
|
let last = self.fields.pop().unwrap();
|
|
|
|
self.fields
|
|
|
|
.last_mut()
|
|
|
|
.unwrap()
|
|
|
|
.push_str(&format!("; {}", last));
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 05:19:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 02:10:28 +02:00
|
|
|
impl From<Note> for pb::Note {
|
|
|
|
fn from(n: Note) -> Self {
|
|
|
|
pb::Note {
|
|
|
|
id: n.id.0,
|
|
|
|
guid: n.guid,
|
|
|
|
ntid: n.ntid.0,
|
|
|
|
mtime_secs: n.mtime.0 as u32,
|
|
|
|
usn: n.usn.0,
|
|
|
|
tags: n.tags,
|
|
|
|
fields: n.fields,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<pb::Note> for Note {
|
|
|
|
fn from(n: pb::Note) -> Self {
|
|
|
|
Note {
|
|
|
|
id: NoteID(n.id),
|
|
|
|
guid: n.guid,
|
|
|
|
ntid: NoteTypeID(n.ntid),
|
|
|
|
mtime: TimestampSecs(n.mtime_secs as i64),
|
|
|
|
usn: Usn(n.usn),
|
|
|
|
tags: n.tags,
|
|
|
|
fields: n.fields,
|
|
|
|
sort_field: None,
|
|
|
|
checksum: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 05:52:55 +01:00
|
|
|
/// Text must be passed to strip_html_preserving_image_filenames() by
|
|
|
|
/// caller prior to passing in here.
|
2020-03-17 03:31:54 +01:00
|
|
|
pub(crate) fn field_checksum(text: &str) -> u32 {
|
2020-03-17 05:52:55 +01:00
|
|
|
let digest = sha1::Sha1::from(text).digest().bytes();
|
2020-02-10 05:19:39 +01:00
|
|
|
u32::from_be_bytes(digest[..4].try_into().unwrap())
|
|
|
|
}
|
2020-04-16 09:02:59 +02:00
|
|
|
|
2020-05-26 09:18:50 +02:00
|
|
|
pub(crate) fn guid() -> String {
|
2020-04-16 09:02:59 +02:00
|
|
|
anki_base91(rand::random())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn anki_base91(mut n: u64) -> String {
|
|
|
|
let table = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
2020-04-17 06:36:45 +02:00
|
|
|
0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~";
|
2020-04-16 09:02:59 +02:00
|
|
|
let mut buf = String::new();
|
|
|
|
while n > 0 {
|
|
|
|
let (q, r) = n.div_rem(&(table.len() as u64));
|
|
|
|
buf.push(table[r as usize] as char);
|
|
|
|
n = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.chars().rev().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Collection {
|
2020-04-23 02:10:28 +02:00
|
|
|
fn canonify_note_tags(&self, note: &mut Note, usn: Usn) -> Result<()> {
|
2020-05-07 09:54:23 +02:00
|
|
|
if !note.tags.is_empty() {
|
|
|
|
let tags = std::mem::replace(&mut note.tags, vec![]);
|
|
|
|
note.tags = self.canonify_tags(tags, usn)?.0;
|
|
|
|
}
|
2020-04-23 02:10:28 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_note(&mut self, note: &mut Note, did: DeckID) -> Result<()> {
|
2020-04-16 09:02:59 +02:00
|
|
|
self.transact(None, |col| {
|
2020-04-17 06:36:45 +02:00
|
|
|
let nt = col
|
2020-04-18 00:48:10 +02:00
|
|
|
.get_notetype(note.ntid)?
|
2020-04-17 06:36:45 +02:00
|
|
|
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
2020-04-20 13:32:55 +02:00
|
|
|
let ctx = CardGenContext::new(&nt, col.usn()?);
|
2020-05-06 12:06:42 +02:00
|
|
|
let norm = col.normalize_note_text();
|
|
|
|
col.add_note_inner(&ctx, note, did, norm)
|
2020-04-16 09:02:59 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-17 06:36:45 +02:00
|
|
|
|
2020-04-23 02:10:28 +02:00
|
|
|
pub(crate) fn add_note_inner(
|
|
|
|
&mut self,
|
|
|
|
ctx: &CardGenContext,
|
|
|
|
note: &mut Note,
|
|
|
|
did: DeckID,
|
2020-05-06 12:06:42 +02:00
|
|
|
normalize_text: bool,
|
2020-04-23 02:10:28 +02:00
|
|
|
) -> Result<()> {
|
|
|
|
self.canonify_note_tags(note, ctx.usn)?;
|
2020-05-06 12:06:42 +02:00
|
|
|
note.prepare_for_update(&ctx.notetype, normalize_text)?;
|
2020-05-06 05:14:57 +02:00
|
|
|
note.set_modified(ctx.usn);
|
2020-04-17 06:36:45 +02:00
|
|
|
self.storage.add_note(note)?;
|
2020-04-23 02:10:28 +02:00
|
|
|
self.generate_cards_for_new_note(ctx, note, did)
|
2020-04-20 13:32:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_note(&mut self, note: &mut Note) -> Result<()> {
|
2020-06-02 05:32:36 +02:00
|
|
|
if let Some(existing_note) = self.storage.get_note(note.id)? {
|
|
|
|
if &existing_note == note {
|
|
|
|
// nothing to do
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:32:55 +02:00
|
|
|
self.transact(None, |col| {
|
|
|
|
let nt = col
|
|
|
|
.get_notetype(note.ntid)?
|
|
|
|
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
|
|
|
let ctx = CardGenContext::new(&nt, col.usn()?);
|
2020-05-06 12:06:42 +02:00
|
|
|
let norm = col.normalize_note_text();
|
|
|
|
col.update_note_inner_generating_cards(&ctx, note, true, norm)
|
2020-04-20 13:32:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:14:57 +02:00
|
|
|
pub(crate) fn update_note_inner_generating_cards(
|
2020-04-20 13:32:55 +02:00
|
|
|
&mut self,
|
|
|
|
ctx: &CardGenContext,
|
|
|
|
note: &mut Note,
|
2020-05-06 05:14:57 +02:00
|
|
|
mark_note_modified: bool,
|
2020-05-06 12:06:42 +02:00
|
|
|
normalize_text: bool,
|
2020-04-20 13:32:55 +02:00
|
|
|
) -> Result<()> {
|
2020-05-06 12:06:42 +02:00
|
|
|
self.update_note_inner_without_cards(
|
|
|
|
note,
|
|
|
|
ctx.notetype,
|
|
|
|
ctx.usn,
|
|
|
|
mark_note_modified,
|
|
|
|
normalize_text,
|
|
|
|
)?;
|
2020-05-06 05:14:57 +02:00
|
|
|
self.generate_cards_for_existing_note(ctx, note)
|
|
|
|
}
|
2020-04-17 06:36:45 +02:00
|
|
|
|
2020-05-06 05:14:57 +02:00
|
|
|
pub(crate) fn update_note_inner_without_cards(
|
|
|
|
&mut self,
|
|
|
|
note: &mut Note,
|
|
|
|
nt: &NoteType,
|
|
|
|
usn: Usn,
|
|
|
|
mark_note_modified: bool,
|
2020-05-06 12:06:42 +02:00
|
|
|
normalize_text: bool,
|
2020-05-06 05:14:57 +02:00
|
|
|
) -> Result<()> {
|
|
|
|
self.canonify_note_tags(note, usn)?;
|
2020-05-06 12:06:42 +02:00
|
|
|
note.prepare_for_update(nt, normalize_text)?;
|
2020-05-06 05:14:57 +02:00
|
|
|
if mark_note_modified {
|
|
|
|
note.set_modified(usn);
|
|
|
|
}
|
|
|
|
self.storage.update_note(note)
|
2020-04-17 06:36:45 +02:00
|
|
|
}
|
2020-05-02 04:41:53 +02:00
|
|
|
|
|
|
|
/// 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)? {
|
|
|
|
// fixme: undo
|
|
|
|
self.storage.remove_note(nid)?;
|
|
|
|
self.storage.add_note_grave(nid, usn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 05:14:57 +02:00
|
|
|
|
|
|
|
/// Update cards and field cache after notes modified externally.
|
|
|
|
/// If gencards is false, skip card generation.
|
|
|
|
pub(crate) fn after_note_updates(
|
|
|
|
&mut self,
|
|
|
|
nids: &[NoteID],
|
|
|
|
generate_cards: bool,
|
|
|
|
mark_notes_modified: bool,
|
|
|
|
) -> Result<()> {
|
2020-05-07 09:54:23 +02:00
|
|
|
self.transform_notes(nids, |_note, _nt| {
|
|
|
|
Ok(TransformNoteOutput {
|
|
|
|
changed: true,
|
|
|
|
generate_cards,
|
|
|
|
mark_modified: mark_notes_modified,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn transform_notes<F>(
|
|
|
|
&mut self,
|
|
|
|
nids: &[NoteID],
|
|
|
|
mut transformer: F,
|
|
|
|
) -> Result<usize>
|
|
|
|
where
|
|
|
|
F: FnMut(&mut Note, &NoteType) -> Result<TransformNoteOutput>,
|
|
|
|
{
|
2020-05-06 05:14:57 +02:00
|
|
|
let nids_by_notetype = self.storage.note_ids_by_notetype(nids)?;
|
2020-05-06 12:06:42 +02:00
|
|
|
let norm = self.normalize_note_text();
|
2020-05-07 09:54:23 +02:00
|
|
|
let mut changed_notes = 0;
|
|
|
|
let usn = self.usn()?;
|
|
|
|
|
2020-05-06 05:14:57 +02:00
|
|
|
for (ntid, group) in &nids_by_notetype.into_iter().group_by(|tup| tup.0) {
|
|
|
|
let nt = self
|
|
|
|
.get_notetype(ntid)?
|
|
|
|
.ok_or_else(|| AnkiError::invalid_input("missing note type"))?;
|
2020-05-07 09:54:23 +02:00
|
|
|
|
|
|
|
let mut genctx = None;
|
2020-05-06 05:14:57 +02:00
|
|
|
for (_, nid) in group {
|
2020-05-07 09:54:23 +02:00
|
|
|
// grab the note and transform it
|
2020-05-06 05:14:57 +02:00
|
|
|
let mut note = self.storage.get_note(nid)?.unwrap();
|
2020-05-07 09:54:23 +02:00
|
|
|
let out = transformer(&mut note, &nt)?;
|
|
|
|
if !out.changed {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.generate_cards {
|
|
|
|
let ctx = genctx.get_or_insert_with(|| CardGenContext::new(&nt, usn));
|
2020-05-06 05:14:57 +02:00
|
|
|
self.update_note_inner_generating_cards(
|
2020-05-07 09:54:23 +02:00
|
|
|
&ctx,
|
2020-05-06 05:14:57 +02:00
|
|
|
&mut note,
|
2020-05-07 09:54:23 +02:00
|
|
|
out.mark_modified,
|
2020-05-06 12:06:42 +02:00
|
|
|
norm,
|
2020-05-06 05:14:57 +02:00
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
self.update_note_inner_without_cards(
|
|
|
|
&mut note,
|
2020-05-07 09:54:23 +02:00
|
|
|
&nt,
|
2020-05-06 05:14:57 +02:00
|
|
|
usn,
|
2020-05-07 09:54:23 +02:00
|
|
|
out.mark_modified,
|
2020-05-06 12:06:42 +02:00
|
|
|
norm,
|
2020-05-06 05:14:57 +02:00
|
|
|
)?;
|
|
|
|
}
|
2020-05-07 09:54:23 +02:00
|
|
|
|
|
|
|
changed_notes += 1;
|
2020-05-06 05:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 09:54:23 +02:00
|
|
|
|
|
|
|
Ok(changed_notes)
|
2020-05-06 05:14:57 +02:00
|
|
|
}
|
2020-05-24 11:48:56 +02:00
|
|
|
|
|
|
|
pub(crate) fn note_is_duplicate_or_empty(&self, note: &Note) -> Result<DuplicateState> {
|
|
|
|
if let Some(field1) = note.fields.get(0) {
|
|
|
|
let stripped = strip_html_preserving_image_filenames(field1);
|
|
|
|
if stripped.trim().is_empty() {
|
|
|
|
Ok(DuplicateState::Empty)
|
|
|
|
} else {
|
|
|
|
let csum = field_checksum(&stripped);
|
|
|
|
for field in self
|
|
|
|
.storage
|
|
|
|
.note_fields_by_checksum(note.id, note.ntid, csum)?
|
|
|
|
{
|
|
|
|
if strip_html_preserving_image_filenames(&field) == stripped {
|
|
|
|
return Ok(DuplicateState::Duplicate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(DuplicateState::Normal)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(DuplicateState::Empty)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 09:02:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::{anki_base91, field_checksum};
|
2020-05-18 11:02:54 +02:00
|
|
|
use crate::{
|
|
|
|
collection::open_test_collection, config::ConfigKey, decks::DeckID, err::Result,
|
|
|
|
search::SortMode,
|
|
|
|
};
|
2020-04-16 09:02:59 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_base91() {
|
|
|
|
// match the python implementation for now
|
|
|
|
assert_eq!(anki_base91(0), "");
|
|
|
|
assert_eq!(anki_base91(1), "b");
|
|
|
|
assert_eq!(anki_base91(u64::max_value()), "Rj&Z5m[>Zp");
|
|
|
|
assert_eq!(anki_base91(1234567890), "saAKk");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_field_checksum() {
|
|
|
|
assert_eq!(field_checksum("test"), 2840236005);
|
|
|
|
assert_eq!(field_checksum("今日"), 1464653051);
|
|
|
|
}
|
2020-04-17 06:36:45 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 08:50:34 +02:00
|
|
|
fn adding_cards() -> Result<()> {
|
2020-04-17 06:36:45 +02:00
|
|
|
let mut col = open_test_collection();
|
2020-04-21 08:50:34 +02:00
|
|
|
let nt = col
|
|
|
|
.get_notetype_by_name("basic (and reversed card)")?
|
|
|
|
.unwrap();
|
2020-04-17 06:36:45 +02:00
|
|
|
|
|
|
|
let mut note = nt.new_note();
|
2020-04-21 08:50:34 +02:00
|
|
|
// if no cards are generated, 1 card is added
|
2020-04-23 02:10:28 +02:00
|
|
|
col.add_note(&mut note, DeckID(1)).unwrap();
|
2020-04-21 08:50:34 +02:00
|
|
|
let existing = col.storage.existing_cards_for_note(note.id)?;
|
|
|
|
assert_eq!(existing.len(), 1);
|
|
|
|
assert_eq!(existing[0].ord, 0);
|
2020-04-17 06:36:45 +02:00
|
|
|
|
2020-04-21 08:50:34 +02:00
|
|
|
// nothing changes if the first field is filled
|
|
|
|
note.fields[0] = "test".into();
|
|
|
|
col.update_note(&mut note).unwrap();
|
|
|
|
let existing = col.storage.existing_cards_for_note(note.id)?;
|
|
|
|
assert_eq!(existing.len(), 1);
|
|
|
|
assert_eq!(existing[0].ord, 0);
|
|
|
|
|
|
|
|
// second field causes another card to be generated
|
|
|
|
note.fields[1] = "test".into();
|
|
|
|
col.update_note(&mut note).unwrap();
|
|
|
|
let existing = col.storage.existing_cards_for_note(note.id)?;
|
|
|
|
assert_eq!(existing.len(), 2);
|
|
|
|
assert_eq!(existing[1].ord, 1);
|
2020-04-17 06:36:45 +02:00
|
|
|
|
2020-04-21 08:50:34 +02:00
|
|
|
// cloze cards also generate card 0 if no clozes are found
|
2020-04-21 05:49:40 +02:00
|
|
|
let nt = col.get_notetype_by_name("cloze")?.unwrap();
|
|
|
|
let mut note = nt.new_note();
|
2020-04-23 02:10:28 +02:00
|
|
|
col.add_note(&mut note, DeckID(1)).unwrap();
|
2020-04-21 05:49:40 +02:00
|
|
|
let existing = col.storage.existing_cards_for_note(note.id)?;
|
|
|
|
assert_eq!(existing.len(), 1);
|
|
|
|
assert_eq!(existing[0].ord, 0);
|
2020-04-21 06:24:19 +02:00
|
|
|
assert_eq!(existing[0].original_deck_id, DeckID(1));
|
2020-04-21 05:49:40 +02:00
|
|
|
|
2020-04-21 08:50:34 +02:00
|
|
|
// and generate cards for any cloze deletions
|
2020-04-21 05:49:40 +02:00
|
|
|
note.fields[0] = "{{c1::foo}} {{c2::bar}} {{c3::baz}} {{c0::quux}} {{c501::over}}".into();
|
|
|
|
col.update_note(&mut note)?;
|
|
|
|
let existing = col.storage.existing_cards_for_note(note.id)?;
|
|
|
|
let mut ords = existing.iter().map(|a| a.ord).collect::<Vec<_>>();
|
|
|
|
ords.sort();
|
|
|
|
assert_eq!(ords, vec![0, 1, 2, 499]);
|
|
|
|
|
2020-04-17 06:36:45 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 12:06:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalization() -> Result<()> {
|
|
|
|
let mut col = open_test_collection();
|
|
|
|
|
|
|
|
let nt = col.get_notetype_by_name("Basic")?.unwrap();
|
|
|
|
let mut note = nt.new_note();
|
|
|
|
note.fields[0] = "\u{fa47}".into();
|
|
|
|
col.add_note(&mut note, DeckID(1))?;
|
|
|
|
assert_eq!(note.fields[0], "\u{6f22}");
|
|
|
|
// non-normalized searches should be converted
|
2020-05-18 11:02:54 +02:00
|
|
|
assert_eq!(col.search_cards("\u{fa47}", SortMode::NoOrder)?.len(), 1);
|
2020-05-06 12:06:42 +02:00
|
|
|
assert_eq!(
|
2020-05-18 11:02:54 +02:00
|
|
|
col.search_cards("front:\u{fa47}", SortMode::NoOrder)?.len(),
|
2020-05-06 12:06:42 +02:00
|
|
|
1
|
|
|
|
);
|
2020-05-18 11:02:54 +02:00
|
|
|
let cids = col.search_cards("", SortMode::NoOrder)?;
|
|
|
|
col.remove_cards_inner(&cids)?;
|
2020-05-06 12:06:42 +02:00
|
|
|
|
|
|
|
// if normalization turned off, note text is entered as-is
|
|
|
|
let mut note = nt.new_note();
|
|
|
|
note.fields[0] = "\u{fa47}".into();
|
|
|
|
col.set_config(ConfigKey::NormalizeNoteText, &false)
|
|
|
|
.unwrap();
|
|
|
|
col.add_note(&mut note, DeckID(1))?;
|
|
|
|
assert_eq!(note.fields[0], "\u{fa47}");
|
|
|
|
// normalized searches won't match
|
2020-05-18 11:02:54 +02:00
|
|
|
assert_eq!(col.search_cards("\u{6f22}", SortMode::NoOrder)?.len(), 0);
|
2020-05-06 12:06:42 +02:00
|
|
|
// but original characters will
|
2020-05-18 11:02:54 +02:00
|
|
|
assert_eq!(col.search_cards("\u{fa47}", SortMode::NoOrder)?.len(), 1);
|
2020-05-06 12:06:42 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-16 09:02:59 +02:00
|
|
|
}
|