diff --git a/ftl/core/card-templates.ftl b/ftl/core/card-templates.ftl index 6b0d8a1ef..7a507fce8 100644 --- a/ftl/core/card-templates.ftl +++ b/ftl/core/card-templates.ftl @@ -21,6 +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-see-preview = See the render preview for more information. card-templates-changes-saved = Changes saved. card-templates-discard-changes = Discard changes? diff --git a/rslib/src/error/mod.rs b/rslib/src/error/mod.rs index e623d94e2..914074368 100644 --- a/rslib/src/error/mod.rs +++ b/rslib/src/error/mod.rs @@ -66,6 +66,7 @@ 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), }; format!("{}
{}", header, details) } @@ -145,5 +146,6 @@ pub struct TemplateSaveError { #[derive(Debug, PartialEq)] pub enum TemplateSaveErrorDetails { + Duplicate(usize), TemplateError, } diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 924de46e1..9e223d7af 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -272,6 +272,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), + })); + } + } + } + Ok(()) + } + pub(crate) fn normalize_names(&mut self) { ensure_string_in_nfc(&mut self.name); for f in &mut self.fields { @@ -314,6 +329,7 @@ impl Notetype { self.fix_template_names()?; self.ensure_names_unique(); self.reposition_sort_idx(); + self.ensure_template_fronts_unique()?; let parsed_templates = self.parsed_templates(); let invalid_card_idx = parsed_templates