Revert changes to rslib/src/tags/

This commit is contained in:
RumovZ 2022-03-30 11:23:30 +02:00
parent aab518d4d9
commit 3c17d37d26
5 changed files with 15 additions and 13 deletions

View file

@ -6,6 +6,7 @@ use std::{borrow::Cow, collections::HashSet};
use regex::{Captures, Regex}; use regex::{Captures, Regex};
use super::{join_tags, split_tags}; use super::{join_tags, split_tags};
use crate::prelude::*;
pub(crate) struct TagMatcher { pub(crate) struct TagMatcher {
regex: Regex, regex: Regex,
new_tags: HashSet<String>, new_tags: HashSet<String>,
@ -16,7 +17,7 @@ pub(crate) struct TagMatcher {
/// ///
/// Tracks seen tags during replacement, so the tag list can be updated as well. /// Tracks seen tags during replacement, so the tag list can be updated as well.
impl TagMatcher { impl TagMatcher {
pub fn new(space_separated_tags: &str) -> Self { pub fn new(space_separated_tags: &str) -> Result<Self> {
// convert "fo*o bar" into "fo\*o|bar" // convert "fo*o bar" into "fo\*o|bar"
let tags: Vec<_> = split_tags(space_separated_tags) let tags: Vec<_> = split_tags(space_separated_tags)
.map(regex::escape) .map(regex::escape)
@ -39,13 +40,12 @@ impl TagMatcher {
) )
"#, "#,
tags tags
)) ))?;
.unwrap();
Self { Ok(Self {
regex, regex,
new_tags: HashSet::new(), new_tags: HashSet::new(),
} })
} }
pub fn is_match(&self, space_separated_tags: &str) -> bool { pub fn is_match(&self, space_separated_tags: &str) -> bool {
@ -129,13 +129,13 @@ mod test {
use super::*; use super::*;
#[test] #[test]
fn regex() { fn regex() -> Result<()> {
let re = TagMatcher::new("one two"); let re = TagMatcher::new("one two")?;
assert!(!re.is_match(" foo ")); assert!(!re.is_match(" foo "));
assert!(re.is_match(" foo one ")); assert!(re.is_match(" foo one "));
assert!(re.is_match(" two foo ")); 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(" foo ")); assert!(re.is_match(" foo "));
assert!(re.is_match(" bar foo baz ")); assert!(re.is_match(" bar foo baz "));
@ -155,5 +155,7 @@ mod test {
&as_xxx(" x foo::bar foo::bar::baz x "), &as_xxx(" x foo::bar foo::bar::baz x "),
" x xxx::bar xxx::bar::baz x " " x xxx::bar xxx::bar::baz x "
); );
Ok(())
} }
} }

View file

@ -4,7 +4,7 @@
mod bulkadd; mod bulkadd;
mod complete; mod complete;
mod findreplace; mod findreplace;
pub(crate) mod matcher; mod matcher;
mod notes; mod notes;
mod register; mod register;
mod remove; mod remove;

View file

@ -32,7 +32,7 @@ impl Collection {
let usn = self.usn()?; let usn = self.usn()?;
// gather tags that need removing // gather tags that need removing
let mut re = TagMatcher::new(tags); let mut re = TagMatcher::new(tags)?;
let matched_notes = self let matched_notes = self
.storage .storage
.get_note_tags_by_predicate(|tags| re.is_match(tags))?; .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<usize> { fn remove_tags_from_notes_inner(&mut self, nids: &[NoteId], tags: &str) -> Result<usize> {
let usn = self.usn()?; let usn = self.usn()?;
let mut re = TagMatcher::new(tags); let mut re = TagMatcher::new(tags)?;
let mut match_count = 0; let mut match_count = 0;
let notes = self.storage.get_note_tags_by_id_list(nids)?; let notes = self.storage.get_note_tags_by_id_list(nids)?;

View file

@ -39,7 +39,7 @@ impl Collection {
.unwrap_or(new_prefix); .unwrap_or(new_prefix);
// gather tags that need replacing // gather tags that need replacing
let mut re = TagMatcher::new(old_prefix); let mut re = TagMatcher::new(old_prefix)?;
let matched_notes = self let matched_notes = self
.storage .storage
.get_note_tags_by_predicate(|tags| re.is_match(tags))?; .get_note_tags_by_predicate(|tags| re.is_match(tags))?;

View file

@ -36,7 +36,7 @@ impl Collection {
new_parent: Option<String>, new_parent: Option<String>,
) -> Result<usize> { ) -> Result<usize> {
let usn = self.usn()?; 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 old_to_new_names = old_to_new_names(tags_to_reparent, new_parent);
let matched_notes = self let matched_notes = self