fix deck and tag normalization

The issue existed in the deck code as well; I've added a test for it.
This commit is contained in:
Damien Elmes 2021-01-19 12:36:01 +10:00
parent 09bea954a2
commit 9edb002679
2 changed files with 26 additions and 7 deletions

View file

@ -114,10 +114,11 @@ fn normalize_native_name(name: &str) -> Cow<str> {
.split('\x1f') .split('\x1f')
.any(|comp| matches!(normalized_deck_name_component(comp), Cow::Owned(_))) .any(|comp| matches!(normalized_deck_name_component(comp), Cow::Owned(_)))
{ {
name.split('\x1f') let comps: Vec<_> = name
.split('\x1f')
.map(normalized_deck_name_component) .map(normalized_deck_name_component)
.collect::<String>() .collect::<Vec<_>>();
.into() comps.join("\x1f").into()
} else { } else {
// no changes required // no changes required
name.into() name.into()
@ -525,7 +526,7 @@ impl Collection {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{human_deck_name_to_native, immediate_parent_name}; use super::{human_deck_name_to_native, immediate_parent_name, normalize_native_name};
use crate::{ use crate::{
collection::{open_test_collection, Collection}, collection::{open_test_collection, Collection},
err::Result, err::Result,
@ -562,6 +563,12 @@ mod test {
); );
} }
#[test]
fn normalize() {
assert_eq!(&normalize_native_name("foo\x1fbar"), "foo\x1fbar");
assert_eq!(&normalize_native_name("fo\u{a}o\x1fbar"), "foo\x1fbar");
}
#[test] #[test]
fn adding_updating() -> Result<()> { fn adding_updating() -> Result<()> {
let mut col = open_test_collection(); let mut col = open_test_collection();

View file

@ -91,10 +91,11 @@ fn normalize_tag_name(name: &str) -> Cow<str> {
.split("::") .split("::")
.any(|comp| matches!(normalized_tag_name_component(comp), Cow::Owned(_))) .any(|comp| matches!(normalized_tag_name_component(comp), Cow::Owned(_)))
{ {
name.split("::") let comps: Vec<_> = name
.split("::")
.map(normalized_tag_name_component) .map(normalized_tag_name_component)
.collect::<String>() .collect::<Vec<_>>();
.into() comps.join("::").into()
} else { } else {
// no changes required // no changes required
name.into() name.into()
@ -419,6 +420,17 @@ mod test {
col.update_note(&mut note)?; col.update_note(&mut note)?;
assert_eq!(&note.tags, &["one", "two"]); assert_eq!(&note.tags, &["one", "two"]);
// blanks should be handled
note.tags = vec![
"".into(),
"foo".into(),
" ".into(),
"::".into(),
"foo::".into(),
];
col.update_note(&mut note)?;
assert_eq!(&note.tags, &["blank::blank", "foo", "foo::blank"]);
Ok(()) Ok(())
} }