mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Make TagMatcher::new() infallible
This commit is contained in:
parent
6269a88cd3
commit
7d1686e272
4 changed files with 12 additions and 14 deletions
|
@ -6,7 +6,6 @@ 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<String>,
|
||||
|
@ -17,7 +16,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) -> Result<Self> {
|
||||
pub fn new(space_separated_tags: &str) -> Self {
|
||||
// convert "fo*o bar" into "fo\*o|bar"
|
||||
let tags: Vec<_> = split_tags(space_separated_tags)
|
||||
.map(regex::escape)
|
||||
|
@ -40,12 +39,13 @@ impl TagMatcher {
|
|||
)
|
||||
"#,
|
||||
tags
|
||||
))?;
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
Ok(Self {
|
||||
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() -> Result<()> {
|
||||
let re = TagMatcher::new("one two")?;
|
||||
fn regex() {
|
||||
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,7 +155,5 @@ mod test {
|
|||
&as_xxx(" x foo::bar foo::bar::baz x "),
|
||||
" x xxx::bar xxx::bar::baz x "
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<usize> {
|
||||
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)?;
|
||||
|
||||
|
|
|
@ -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))?;
|
||||
|
|
|
@ -36,7 +36,7 @@ impl Collection {
|
|||
new_parent: Option<String>,
|
||||
) -> Result<usize> {
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue