From 3c17d37d26b6c97fb9cf5b9570d4917871414988 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Wed, 30 Mar 2022 11:23:30 +0200 Subject: [PATCH] Revert changes to rslib/src/tags/ --- rslib/src/tags/matcher.rs | 18 ++++++++++-------- rslib/src/tags/mod.rs | 2 +- rslib/src/tags/remove.rs | 4 ++-- rslib/src/tags/rename.rs | 2 +- rslib/src/tags/reparent.rs | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/rslib/src/tags/matcher.rs b/rslib/src/tags/matcher.rs index 2cc3a0850..3f7ded47e 100644 --- a/rslib/src/tags/matcher.rs +++ b/rslib/src/tags/matcher.rs @@ -6,6 +6,7 @@ use std::{borrow::Cow, collections::HashSet}; use regex::{Captures, Regex}; use super::{join_tags, split_tags}; +use crate::prelude::*; pub(crate) struct TagMatcher { regex: Regex, new_tags: HashSet, @@ -16,7 +17,7 @@ pub(crate) struct TagMatcher { /// /// Tracks seen tags during replacement, so the tag list can be updated as well. impl TagMatcher { - pub fn new(space_separated_tags: &str) -> Self { + pub fn new(space_separated_tags: &str) -> Result { // convert "fo*o bar" into "fo\*o|bar" let tags: Vec<_> = split_tags(space_separated_tags) .map(regex::escape) @@ -39,13 +40,12 @@ impl TagMatcher { ) "#, tags - )) - .unwrap(); + ))?; - Self { + Ok(Self { regex, new_tags: HashSet::new(), - } + }) } pub fn is_match(&self, space_separated_tags: &str) -> bool { @@ -129,13 +129,13 @@ mod test { use super::*; #[test] - fn regex() { - let re = TagMatcher::new("one two"); + fn regex() -> Result<()> { + let re = TagMatcher::new("one two")?; assert!(!re.is_match(" foo ")); assert!(re.is_match(" foo one ")); assert!(re.is_match(" two foo ")); - let mut re = TagMatcher::new("foo"); + let mut re = TagMatcher::new("foo")?; assert!(re.is_match("foo")); assert!(re.is_match(" foo ")); assert!(re.is_match(" bar foo baz ")); @@ -155,5 +155,7 @@ mod test { &as_xxx(" x foo::bar foo::bar::baz x "), " x xxx::bar xxx::bar::baz x " ); + + Ok(()) } } diff --git a/rslib/src/tags/mod.rs b/rslib/src/tags/mod.rs index 0da072942..e9ef57b34 100644 --- a/rslib/src/tags/mod.rs +++ b/rslib/src/tags/mod.rs @@ -4,7 +4,7 @@ mod bulkadd; mod complete; mod findreplace; -pub(crate) mod matcher; +mod matcher; mod notes; mod register; mod remove; diff --git a/rslib/src/tags/remove.rs b/rslib/src/tags/remove.rs index 0577d50d7..d43b401d7 100644 --- a/rslib/src/tags/remove.rs +++ b/rslib/src/tags/remove.rs @@ -32,7 +32,7 @@ impl Collection { let usn = self.usn()?; // gather tags that need removing - let mut re = TagMatcher::new(tags); + let mut re = TagMatcher::new(tags)?; let matched_notes = self .storage .get_note_tags_by_predicate(|tags| re.is_match(tags))?; @@ -57,7 +57,7 @@ impl Collection { fn remove_tags_from_notes_inner(&mut self, nids: &[NoteId], tags: &str) -> Result { let usn = self.usn()?; - let mut re = TagMatcher::new(tags); + let mut re = TagMatcher::new(tags)?; let mut match_count = 0; let notes = self.storage.get_note_tags_by_id_list(nids)?; diff --git a/rslib/src/tags/rename.rs b/rslib/src/tags/rename.rs index 1de297a1a..253a62cd9 100644 --- a/rslib/src/tags/rename.rs +++ b/rslib/src/tags/rename.rs @@ -39,7 +39,7 @@ impl Collection { .unwrap_or(new_prefix); // gather tags that need replacing - let mut re = TagMatcher::new(old_prefix); + let mut re = TagMatcher::new(old_prefix)?; let matched_notes = self .storage .get_note_tags_by_predicate(|tags| re.is_match(tags))?; diff --git a/rslib/src/tags/reparent.rs b/rslib/src/tags/reparent.rs index af45822db..bdedd38b0 100644 --- a/rslib/src/tags/reparent.rs +++ b/rslib/src/tags/reparent.rs @@ -36,7 +36,7 @@ impl Collection { new_parent: Option, ) -> Result { let usn = self.usn()?; - let mut matcher = TagMatcher::new(&join_tags(tags_to_reparent)); + let mut matcher = TagMatcher::new(&join_tags(tags_to_reparent))?; let old_to_new_names = old_to_new_names(tags_to_reparent, new_parent); let matched_notes = self