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<()> {
for mut t in &mut self.templates {
CardTemplate::fix_name(&mut t);
if t.name.is_empty() {
return Err(AnkiError::invalid_input("Empty template name"));
}
}
Ok(())
self.templates
.iter_mut()
.try_for_each(CardTemplate::fix_name)
}
/// Find the field index of the provided field name.

View file

@ -4,6 +4,7 @@
use crate::{
backend_proto::{CardTemplate as CardTemplateProto, CardTemplateConfig, OptionalUInt32},
decks::DeckID,
err::{AnkiError, Result},
template::ParsedTemplate,
timestamp::TimestampSecs,
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 == '"';
if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, "");
}
if self.name.is_empty() {
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(())
}
}