From ba541076aa24818ceba8d7abdc1796a7e72872d4 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 1 Apr 2021 17:59:33 +1000 Subject: [PATCH] convert card template error to tuple, and report notetype name in error Older translations will note have the $notetype variable, but that is not an error in Fluent - it would only cause problems if we tried to use the new string on older Anki versions. --- ftl/core/card-templates.ftl | 2 +- rslib/src/error/mod.rs | 12 +++++++++--- rslib/src/notetype/mod.rs | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ftl/core/card-templates.ftl b/ftl/core/card-templates.ftl index 74d235791..6ee294f2c 100644 --- a/ftl/core/card-templates.ftl +++ b/ftl/core/card-templates.ftl @@ -18,7 +18,7 @@ card-templates-night-mode = Night Mode # on a mobile device. card-templates-add-mobile-class = Add Mobile Class card-templates-preview-settings = Options -card-templates-invalid-template-number = Card template { $number } has a problem. +card-templates-invalid-template-number = Card template { $number } in notetype '{ $notetype }' has a problem. card-templates-changes-saved = Changes saved. card-templates-discard-changes = Discard changes? card-templates-add-card-type = Add Card Type... diff --git a/rslib/src/error/mod.rs b/rslib/src/error/mod.rs index a02893990..3c4100d87 100644 --- a/rslib/src/error/mod.rs +++ b/rslib/src/error/mod.rs @@ -21,7 +21,7 @@ pub type Result = std::result::Result; pub enum AnkiError { InvalidInput(String), TemplateError(String), - TemplateSaveError { ordinal: usize }, + TemplateSaveError(TemplateSaveError), IoError(String), DbError(DbError), NetworkError(NetworkError), @@ -60,8 +60,8 @@ impl AnkiError { // already localized info.into() } - AnkiError::TemplateSaveError { ordinal } => tr - .card_templates_invalid_template_number(ordinal + 1) + AnkiError::TemplateSaveError(err) => tr + .card_templates_invalid_template_number(err.ordinal + 1, &err.notetype) .into(), AnkiError::DbError(err) => err.localized_description(tr), AnkiError::SearchError(kind) => kind.localized_description(&tr), @@ -129,3 +129,9 @@ impl From for AnkiError { AnkiError::InvalidRegex(err.to_string()) } } + +#[derive(Debug, PartialEq)] +pub struct TemplateSaveError { + pub notetype: String, + pub ordinal: usize, +} diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 5504d1eef..a0f3c2dcd 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -26,7 +26,7 @@ use crate::{ collection::Collection, decks::DeckId, define_newtype, - error::{AnkiError, Result}, + error::{AnkiError, Result, TemplateSaveError}, notes::Note, prelude::*, template::{FieldRequirements, ParsedTemplate}, @@ -250,7 +250,10 @@ impl Notetype { } }); if let Some(idx) = invalid_card_idx { - return Err(AnkiError::TemplateSaveError { ordinal: idx }); + return Err(AnkiError::TemplateSaveError(TemplateSaveError { + notetype: self.name.clone(), + ordinal: idx, + })); } let reqs = self.updated_requirements(&parsed_templates);