From 6a47e80c799e26ace5117e4ccb3af4d3218f8ec1 Mon Sep 17 00:00:00 2001 From: Ken Chou Date: Sat, 31 Aug 2024 22:03:46 +0800 Subject: [PATCH] allow mixing regex and glob patterns in hash list filenames. --- src/pmatcher.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/pmatcher.rs b/src/pmatcher.rs index b21d18d..f706ceb 100644 --- a/src/pmatcher.rs +++ b/src/pmatcher.rs @@ -1,13 +1,13 @@ -use std::path::{Path,PathBuf}; use std::collections::HashMap; use std::fs::File; use std::io::Read; +use std::path::{Path, PathBuf}; use fancy_regex::Regex; use md5::{Digest, Md5}; -use crate::pconfig; use crate::fnmatch_regex; +use crate::pconfig; #[derive(Debug)] pub struct PatternMatcher { @@ -79,31 +79,27 @@ impl PatternMatcher { fn create_patterns_with_hash(patterns: HashMap>) -> Vec<(Regex, Vec)> { patterns .into_iter() - .map(|(key, value)| { - // println!("hash --> {}", key); - ( - Regex::new(fnmatch_regex::glob_to_regex_string(&key).as_str()).unwrap(), - value, - ) - }) + .map(|(key, value)| (parse_mixed_regex(&key), value)) .collect() } +fn parse_mixed_regex(pattern: &str) -> Regex { + let pattern = pattern.trim(); + // println!(">>> {:#?}", pattern); + if let Some(stripped) = pattern.strip_prefix('/') { + Regex::new(stripped).unwrap() + } else { + Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap() + } +} + /** * 创建正则表达式列表,通配符形式转为正则表达式 */ fn create_mixed_regex_list(patterns: Vec<&str>) -> Vec { patterns .iter() - .map(|pattern| { - let pattern = pattern.trim(); - // println!(">>> {:#?}", pattern); - if let Some(stripped) = pattern.strip_prefix('/') { - Regex::new(stripped).unwrap() - } else { - Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap() - } - }) + .map(|pattern| parse_mixed_regex(pattern)) .collect() }