mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
Mention missing field's name in CardTypeError (#3225)
This commit is contained in:
parent
e3c6b5bf3d
commit
e0a3768bf3
3 changed files with 19 additions and 13 deletions
|
@ -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-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-extraneous-cloze = 'cloze:' can only be used on cloze notetypes.
|
||||||
card-templates-see-preview = See the preview for more information.
|
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-changes-saved = Changes saved.
|
||||||
card-templates-discard-changes = Discard changes?
|
card-templates-discard-changes = Discard changes?
|
||||||
card-templates-add-card-type = Add Card Type...
|
card-templates-add-card-type = Add Card Type...
|
||||||
|
|
|
@ -138,9 +138,11 @@ impl AnkiError {
|
||||||
AnkiError::CardTypeError { source } => {
|
AnkiError::CardTypeError { source } => {
|
||||||
let header =
|
let header =
|
||||||
tr.card_templates_invalid_template_number(source.ordinal + 1, &source.notetype);
|
tr.card_templates_invalid_template_number(source.ordinal + 1, &source.notetype);
|
||||||
let details = match source.source {
|
let details = match &source.source {
|
||||||
CardTypeErrorDetails::TemplateParseError
|
CardTypeErrorDetails::TemplateParseError => tr.card_templates_see_preview(),
|
||||||
| CardTypeErrorDetails::NoSuchField => tr.card_templates_see_preview(),
|
CardTypeErrorDetails::NoSuchField { field } => {
|
||||||
|
tr.card_templates_field_not_found(field)
|
||||||
|
}
|
||||||
CardTypeErrorDetails::NoFrontField => tr.card_templates_no_front_field(),
|
CardTypeErrorDetails::NoFrontField => tr.card_templates_no_front_field(),
|
||||||
CardTypeErrorDetails::Duplicate { index } => {
|
CardTypeErrorDetails::Duplicate { index } => {
|
||||||
tr.card_templates_identical_front(index + 1)
|
tr.card_templates_identical_front(index + 1)
|
||||||
|
@ -196,9 +198,8 @@ impl AnkiError {
|
||||||
Self::CardTypeError {
|
Self::CardTypeError {
|
||||||
source: CardTypeError { source, .. },
|
source: CardTypeError { source, .. },
|
||||||
} => Some(match source {
|
} => Some(match source {
|
||||||
CardTypeErrorDetails::TemplateParseError | CardTypeErrorDetails::NoSuchField => {
|
CardTypeErrorDetails::TemplateParseError => HelpPage::CardTypeTemplateError,
|
||||||
HelpPage::CardTypeTemplateError
|
CardTypeErrorDetails::NoSuchField { field: _ } => HelpPage::CardTypeTemplateError,
|
||||||
}
|
|
||||||
CardTypeErrorDetails::Duplicate { .. } => HelpPage::CardTypeDuplicate,
|
CardTypeErrorDetails::Duplicate { .. } => HelpPage::CardTypeDuplicate,
|
||||||
CardTypeErrorDetails::NoFrontField => HelpPage::CardTypeNoFrontField,
|
CardTypeErrorDetails::NoFrontField => HelpPage::CardTypeNoFrontField,
|
||||||
CardTypeErrorDetails::MissingCloze => HelpPage::CardTypeMissingCloze,
|
CardTypeErrorDetails::MissingCloze => HelpPage::CardTypeMissingCloze,
|
||||||
|
@ -320,7 +321,7 @@ pub enum CardTypeErrorDetails {
|
||||||
TemplateParseError,
|
TemplateParseError,
|
||||||
Duplicate { index: usize },
|
Duplicate { index: usize },
|
||||||
NoFrontField,
|
NoFrontField,
|
||||||
NoSuchField,
|
NoSuchField { field: String },
|
||||||
MissingCloze,
|
MissingCloze,
|
||||||
ExtraneousCloze,
|
ExtraneousCloze,
|
||||||
}
|
}
|
||||||
|
|
|
@ -410,8 +410,12 @@ impl Notetype {
|
||||||
if q_fields.is_empty() {
|
if q_fields.is_empty() {
|
||||||
return Err(CardTypeErrorDetails::NoFrontField);
|
return Err(CardTypeErrorDetails::NoFrontField);
|
||||||
}
|
}
|
||||||
if self.unknown_field_name(q_fields.union(&a.all_referenced_field_names())) {
|
if let Some(unknown_field) =
|
||||||
return Err(CardTypeErrorDetails::NoSuchField);
|
self.first_unknown_field_name(q_fields.union(&a.all_referenced_field_names()))
|
||||||
|
{
|
||||||
|
return Err(CardTypeErrorDetails::NoSuchField {
|
||||||
|
field: unknown_field.to_string(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
@ -419,14 +423,14 @@ impl Notetype {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// True if any non-empty name in names does not denote a special field or
|
/// Return the first non-empty name in names that does not denote a special
|
||||||
/// a field of this notetype.
|
/// field or a field of this notetype.
|
||||||
fn unknown_field_name<T, I>(&self, names: T) -> bool
|
fn first_unknown_field_name<T, I>(&self, names: T) -> Option<I>
|
||||||
where
|
where
|
||||||
T: IntoIterator<Item = I>,
|
T: IntoIterator<Item = I>,
|
||||||
I: AsRef<str>,
|
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.
|
// The empty field name is allowed as it may be used by add-ons.
|
||||||
!name.as_ref().is_empty()
|
!name.as_ref().is_empty()
|
||||||
&& !SPECIAL_FIELDS.contains(&name.as_ref())
|
&& !SPECIAL_FIELDS.contains(&name.as_ref())
|
||||||
|
|
Loading…
Reference in a new issue