If a template name contains only quote, show relevant error message

This is for the sake of the consistency with the last commit
This commit is contained in:
Arthur Milchior 2021-03-13 07:16:11 +01:00
parent dd48b2dff0
commit 9661684ca3
2 changed files with 18 additions and 11 deletions

View file

@ -333,14 +333,9 @@ impl NoteType {
} }
fn fix_template_names(&mut self) -> Result<()> { fn fix_template_names(&mut self) -> Result<()> {
for mut t in &mut self.templates { self.templates
CardTemplate::fix_name(&mut t); .iter_mut()
if t.name.is_empty() { .try_for_each(CardTemplate::fix_name)
return Err(AnkiError::invalid_input("Empty template name"));
}
}
Ok(())
} }
/// Find the field index of the provided field name. /// Find the field index of the provided field name.

View file

@ -4,6 +4,7 @@
use crate::{ use crate::{
backend_proto::{CardTemplate as CardTemplateProto, CardTemplateConfig, OptionalUInt32}, backend_proto::{CardTemplate as CardTemplateProto, CardTemplateConfig, OptionalUInt32},
decks::DeckID, decks::DeckID,
err::{AnkiError, Result},
template::ParsedTemplate, template::ParsedTemplate,
timestamp::TimestampSecs, timestamp::TimestampSecs,
types::Usn, types::Usn,
@ -89,10 +90,21 @@ impl CardTemplate {
} }
} }
pub(crate) fn fix_name(&mut self) { /// Return whether the name is valid. Remove quote characters if it leads to a valid name.
pub(crate) fn fix_name(&mut self) -> Result<()> {
let bad_chars = |c| c == '"'; let bad_chars = |c| c == '"';
if self.name.contains(bad_chars) { if self.name.is_empty() {
self.name = self.name.replace(bad_chars, ""); return Err(AnkiError::invalid_input("Empty template name"));
} }
let trimmed = self.name.replace(bad_chars, "");
if trimmed.is_empty() {
return Err(AnkiError::invalid_input(
"Template name contain only quotes",
));
}
if self.name.len() != trimmed.len() {
self.name = trimmed;
}
Ok(())
} }
} }