Mention missing field's name in CardTypeError (#3225)

This commit is contained in:
Abdo 2024-06-06 13:20:19 +03:00 committed by GitHub
parent e3c6b5bf3d
commit e0a3768bf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 13 deletions

View File

@ -26,6 +26,7 @@ card-templates-no-front-field = Expected to find a field replacement on the fron
card-templates-missing-cloze = Expected to find '{ "{{" }cloze:Text{ "}}" }' or similar on the front and back of the card template.
card-templates-extraneous-cloze = 'cloze:' can only be used on cloze notetypes.
card-templates-see-preview = See the preview for more information.
card-templates-field-not-found = Field '{ $field }' not found.
card-templates-changes-saved = Changes saved.
card-templates-discard-changes = Discard changes?
card-templates-add-card-type = Add Card Type...

View File

@ -138,9 +138,11 @@ impl AnkiError {
AnkiError::CardTypeError { source } => {
let header =
tr.card_templates_invalid_template_number(source.ordinal + 1, &source.notetype);
let details = match source.source {
CardTypeErrorDetails::TemplateParseError
| CardTypeErrorDetails::NoSuchField => tr.card_templates_see_preview(),
let details = match &source.source {
CardTypeErrorDetails::TemplateParseError => tr.card_templates_see_preview(),
CardTypeErrorDetails::NoSuchField { field } => {
tr.card_templates_field_not_found(field)
}
CardTypeErrorDetails::NoFrontField => tr.card_templates_no_front_field(),
CardTypeErrorDetails::Duplicate { index } => {
tr.card_templates_identical_front(index + 1)
@ -196,9 +198,8 @@ impl AnkiError {
Self::CardTypeError {
source: CardTypeError { source, .. },
} => Some(match source {
CardTypeErrorDetails::TemplateParseError | CardTypeErrorDetails::NoSuchField => {
HelpPage::CardTypeTemplateError
}
CardTypeErrorDetails::TemplateParseError => HelpPage::CardTypeTemplateError,
CardTypeErrorDetails::NoSuchField { field: _ } => HelpPage::CardTypeTemplateError,
CardTypeErrorDetails::Duplicate { .. } => HelpPage::CardTypeDuplicate,
CardTypeErrorDetails::NoFrontField => HelpPage::CardTypeNoFrontField,
CardTypeErrorDetails::MissingCloze => HelpPage::CardTypeMissingCloze,
@ -320,7 +321,7 @@ pub enum CardTypeErrorDetails {
TemplateParseError,
Duplicate { index: usize },
NoFrontField,
NoSuchField,
NoSuchField { field: String },
MissingCloze,
ExtraneousCloze,
}

View File

@ -410,8 +410,12 @@ impl Notetype {
if q_fields.is_empty() {
return Err(CardTypeErrorDetails::NoFrontField);
}
if self.unknown_field_name(q_fields.union(&a.all_referenced_field_names())) {
return Err(CardTypeErrorDetails::NoSuchField);
if let Some(unknown_field) =
self.first_unknown_field_name(q_fields.union(&a.all_referenced_field_names()))
{
return Err(CardTypeErrorDetails::NoSuchField {
field: unknown_field.to_string(),
});
}
Ok(())
} else {
@ -419,14 +423,14 @@ impl Notetype {
}
}
/// True if any non-empty name in names does not denote a special field or
/// a field of this notetype.
fn unknown_field_name<T, I>(&self, names: T) -> bool
/// Return the first non-empty name in names that does not denote a special
/// field or a field of this notetype.
fn first_unknown_field_name<T, I>(&self, names: T) -> Option<I>
where
T: IntoIterator<Item = I>,
I: AsRef<str>,
{
names.into_iter().any(|name| {
names.into_iter().find(|name| {
// The empty field name is allowed as it may be used by add-ons.
!name.as_ref().is_empty()
&& !SPECIAL_FIELDS.contains(&name.as_ref())