From be89ec889273ccca9688eba50321e3a226893919 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Thu, 28 Dec 2023 09:01:23 +0100 Subject: [PATCH] Cloze notetype merges (#2912) * Abort when trying to match notetypes of different kinds * Skip merging templates if target is cloze --- ftl/core/importing.ftl | 3 +++ rslib/src/import_export/mod.rs | 10 +++++++++- rslib/src/import_export/package/apkg/import/notes.rs | 9 +++++++++ rslib/src/notetype/merge.rs | 4 +++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ftl/core/importing.ftl b/ftl/core/importing.ftl index 690e3ca57..72158a862 100644 --- a/ftl/core/importing.ftl +++ b/ftl/core/importing.ftl @@ -7,6 +7,9 @@ importing-anki-files-are-from-a-very = .anki files are from a very old version o importing-anki2-files-are-not-directly-importable = .anki2 files are not directly importable - please import the .apkg or .zip file you have received instead. importing-appeared-twice-in-file = Appeared twice in file: { $val } importing-by-default-anki-will-detect-the = By default, Anki will detect the character between fields, such as a tab, comma, and so on. If Anki is detecting the character incorrectly, you can enter it here. Use \t to represent tab. +importing-cannot-merge-notetypes-of-different-kinds = + Cloze notetypes cannot be merged with regular notetypes. + You may still import the file with '{ importing-merge-notetypes }' disabled. importing-change = Change importing-colon = Colon importing-comma = Comma diff --git a/rslib/src/import_export/mod.rs b/rslib/src/import_export/mod.rs index 889c5b878..6d4108e0d 100644 --- a/rslib/src/import_export/mod.rs +++ b/rslib/src/import_export/mod.rs @@ -62,9 +62,14 @@ impl Note { pub enum ImportError { Corrupt, TooNew, - MediaImportFailed { info: String }, + MediaImportFailed { + info: String, + }, NoFieldColumn, EmptyFile, + /// Two notetypes could not be merged because one was a regular one and the + /// other one a cloze notetype. + NotetypeKindMergeConflict, } impl ImportError { @@ -77,6 +82,9 @@ impl ImportError { } ImportError::NoFieldColumn => tr.importing_file_must_contain_field_column(), ImportError::EmptyFile => tr.importing_file_empty(), + ImportError::NotetypeKindMergeConflict => { + tr.importing_cannot_merge_notetypes_of_different_kinds() + } } .into() } diff --git a/rslib/src/import_export/package/apkg/import/notes.rs b/rslib/src/import_export/package/apkg/import/notes.rs index b41237989..942e62547 100644 --- a/rslib/src/import_export/package/apkg/import/notes.rs +++ b/rslib/src/import_export/package/apkg/import/notes.rs @@ -12,6 +12,7 @@ use super::Context; use super::TemplateMap; use crate::import_export::package::media::safe_normalized_file_name; use crate::import_export::package::UpdateCondition; +use crate::import_export::ImportError; use crate::import_export::ImportProgress; use crate::import_export::NoteLog; use crate::notetype::ChangeNotetypeInput; @@ -229,6 +230,10 @@ impl<'n> NoteContext<'n> { incoming: &mut Notetype, mut existing: Notetype, ) -> Result<()> { + if existing.is_cloze() != incoming.is_cloze() { + return Err(ImportError::NotetypeKindMergeConflict.into()); + } + let original_existing = existing.clone(); // get and merge duplicated notetypes from previous no-merge imports let mut siblings = self.get_sibling_notetypes(existing.id); @@ -367,6 +372,10 @@ impl<'n> NoteContext<'n> { .get_notetype(incoming_ntid)? .or_not_found(incoming_ntid)?; + if existing.is_cloze() != incoming.is_cloze() { + return Err(ImportError::NotetypeKindMergeConflict.into()); + } + existing.merge(&incoming); incoming.merge(&existing); self.record_remapped_ords(&incoming); diff --git a/rslib/src/notetype/merge.rs b/rslib/src/notetype/merge.rs index f88be64ab..83c5871c8 100644 --- a/rslib/src/notetype/merge.rs +++ b/rslib/src/notetype/merge.rs @@ -9,7 +9,9 @@ impl Notetype { /// Inserts not yet existing fields ands templates from `other`. pub(crate) fn merge(&mut self, other: &Self) { self.merge_fields(other); - self.merge_templates(other); + if !self.is_cloze() { + self.merge_templates(other); + } } pub(crate) fn merge_all<'a>(&mut self, others: impl IntoIterator) {