Use HashMap in identical template check

This commit is contained in:
RumovZ 2021-05-27 12:45:17 +02:00
parent 3dfa1de68b
commit c61587b1de
3 changed files with 18 additions and 13 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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(