Return meaningful message if a field is empty after normalizing

This correct the probably rare bug as follow:

I got a note type with a field whose name is "\".

When I made any change to this note type, even unrelated, I get a message stating that there is an empty field. This is
strange because I can see it to be false. Investigating show that "\" is normalized to empty field. This ensure that
it's shown
This commit is contained in:
Arthur Milchior 2021-03-13 07:07:06 +01:00
parent 1ab085dfab
commit dd48b2dff0
2 changed files with 17 additions and 12 deletions

View file

@ -1,7 +1,10 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::backend_proto::{NoteField as NoteFieldProto, NoteFieldConfig, OptionalUInt32}; use crate::{
backend_proto::{NoteField as NoteFieldProto, NoteFieldConfig, OptionalUInt32},
err::{AnkiError, Result},
};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct NoteField { pub struct NoteField {
@ -35,8 +38,11 @@ impl NoteField {
} }
} }
pub(crate) fn fix_name(&mut self) { /// Fix the name of the field if it's valid. Otherwise explain why it's not.
// remove special characters pub(crate) fn fix_name(&mut self) -> Result<()> {
if self.name.is_empty() {
return Err(AnkiError::invalid_input("Empty field name"));
}
let bad_chars = |c| c == ':' || c == '{' || c == '}' || c == '"'; let bad_chars = |c| c == ':' || c == '{' || c == '}' || c == '"';
if self.name.contains(bad_chars) { if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, ""); self.name = self.name.replace(bad_chars, "");
@ -44,9 +50,15 @@ impl NoteField {
// and leading/trailing whitespace and special chars // and leading/trailing whitespace and special chars
let bad_start_chars = |c: char| c == '#' || c == '/' || c == '^' || c.is_whitespace(); let bad_start_chars = |c: char| c == '#' || c == '/' || c == '^' || c.is_whitespace();
let trimmed = self.name.trim().trim_start_matches(bad_start_chars); let trimmed = self.name.trim().trim_start_matches(bad_start_chars);
if trimmed.is_empty() {
return Err(AnkiError::invalid_input(
"Field name: ".to_owned() + &self.name,
));
}
if trimmed.len() != self.name.len() { if trimmed.len() != self.name.len() {
self.name = trimmed.into(); self.name = trimmed.into();
} }
Ok(())
} }
} }
@ -57,7 +69,7 @@ mod test {
#[test] #[test]
fn name() { fn name() {
let mut field = NoteField::new(" # /^ t:e{s\"t} field name #/^ "); let mut field = NoteField::new(" # /^ t:e{s\"t} field name #/^ ");
field.fix_name(); assert_eq!(field.fix_name(), Ok(()));
assert_eq!(&field.name, "test field name #/^"); assert_eq!(&field.name, "test field name #/^");
} }
} }

View file

@ -329,14 +329,7 @@ impl NoteType {
} }
fn fix_field_names(&mut self) -> Result<()> { fn fix_field_names(&mut self) -> Result<()> {
for mut f in &mut self.fields { self.fields.iter_mut().try_for_each(NoteField::fix_name)
NoteField::fix_name(&mut f);
if f.name.is_empty() {
return Err(AnkiError::invalid_input("Empty field name"));
}
}
Ok(())
} }
fn fix_template_names(&mut self) -> Result<()> { fn fix_template_names(&mut self) -> Result<()> {