mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
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:
parent
dd48b2dff0
commit
9661684ca3
2 changed files with 18 additions and 11 deletions
|
@ -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.
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue