Make TagMatcher::new() infallible

This commit is contained in:
RumovZ 2022-03-29 09:13:04 +02:00
parent 6269a88cd3
commit 7d1686e272
4 changed files with 12 additions and 14 deletions

View file

@ -6,7 +6,6 @@ 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>,
@ -17,7 +16,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) -> Result<Self> { pub fn new(space_separated_tags: &str) -> 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)
@ -40,12 +39,13 @@ impl TagMatcher {
) )
"#, "#,
tags tags
))?; ))
.unwrap();
Ok(Self { 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() -> Result<()> { fn regex() {
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,7 +155,5 @@ 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

@ -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