From c61587b1dec10c4943f80132bd43ceef4a397521 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Thu, 27 May 2021 12:45:17 +0200 Subject: [PATCH] Use HashMap in identical template check --- ftl/core/card-templates.ftl | 2 +- rslib/src/error/mod.rs | 4 +++- rslib/src/notetype/mod.rs | 25 ++++++++++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/ftl/core/card-templates.ftl b/ftl/core/card-templates.ftl index 50e4e61b5..193be1b9c 100644 --- a/ftl/core/card-templates.ftl +++ b/ftl/core/card-templates.ftl @@ -21,7 +21,7 @@ card-templates-night-mode = Night Mode card-templates-add-mobile-class = Add Mobile Class card-templates-preview-settings = Options card-templates-invalid-template-number = Card template { $number } in notetype '{ $notetype }' has a problem. -card-templates-identical-front = Its front side is identical with the one of card template { $number }. +card-templates-identical-front = Its front side is identical to the one of card template { $number }. card-templates-missing-cloze = The 'cloze' filter must be used on both sides of a cloze template. card-templates-extraneous-cloze = The 'cloze' filter can only be used on cloze templates. card-templates-see-preview = See the render preview for more information. diff --git a/rslib/src/error/mod.rs b/rslib/src/error/mod.rs index da6c48375..3bb79c776 100644 --- a/rslib/src/error/mod.rs +++ b/rslib/src/error/mod.rs @@ -66,7 +66,9 @@ impl AnkiError { tr.card_templates_invalid_template_number(err.ordinal + 1, &err.notetype); let details = match err.details { TemplateSaveErrorDetails::TemplateError => tr.card_templates_see_preview(), - TemplateSaveErrorDetails::Duplicate(i) => tr.card_templates_identical_front(i), + TemplateSaveErrorDetails::Duplicate(i) => { + tr.card_templates_identical_front(i + 1) + } TemplateSaveErrorDetails::MissingCloze => tr.card_templates_missing_cloze(), TemplateSaveErrorDetails::ExtraneousCloze => { tr.card_templates_extraneous_cloze() diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index f472f27a7..9d1898468 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -273,18 +273,21 @@ impl Notetype { } fn ensure_template_fronts_unique(&self) -> Result<()> { - for i in 1..self.templates.len() { - for j in 0..i { - if self.templates[i].config.q_format == self.templates[j].config.q_format { - return Err(AnkiError::TemplateSaveError(TemplateSaveError { - notetype: self.name.clone(), - ordinal: i, - details: TemplateSaveErrorDetails::Duplicate(j + 1), - })); - } - } + let mut map = HashMap::new(); + if let Some((index_1, index_2)) = + self.templates.iter().enumerate().find_map(|(index, card)| { + map.insert(&card.config.q_format, index) + .map(|old_index| (old_index, index)) + }) + { + Err(AnkiError::TemplateSaveError(TemplateSaveError { + notetype: self.name.clone(), + ordinal: index_2, + details: TemplateSaveErrorDetails::Duplicate(index_1), + })) + } else { + Ok(()) } - Ok(()) } fn ensure_cloze_if_and_only_if_cloze_notetype(