From 6a47e80c799e26ace5117e4ccb3af4d3218f8ec1 Mon Sep 17 00:00:00 2001 From: Ken Chou Date: Sat, 31 Aug 2024 22:03:46 +0800 Subject: [PATCH 1/2] 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() } From 418e9f282e2d96cd9d0cee127e1de681f4ee7c3d Mon Sep 17 00:00:00 2001 From: Ken Chou Date: Sat, 31 Aug 2024 22:04:01 +0800 Subject: [PATCH 2/2] code format --- src/cli.rs | 2 +- src/main.rs | 18 +++++++++++++----- src/p2tree.rs | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e5947ac..d9b5c0c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,5 @@ -use std::path::PathBuf; use std::env; +use std::path::PathBuf; use clap::{arg, command, value_parser, ArgAction}; diff --git a/src/main.rs b/src/main.rs index bc44fab..70004b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,13 @@ use std::path::PathBuf; use colored::*; use walkdir::WalkDir; +mod cli; mod data; mod fnmatch_regex; mod p2tree; -mod tprint; -mod pmatcher; mod pconfig; -mod cli; +mod pmatcher; +mod tprint; mod util; fn main() -> std::io::Result<()> { @@ -62,7 +62,11 @@ fn main() -> std::io::Result<()> { if app_options.enable_renaming { let new_filename = pattern_matcher.clean_filename(filename); if new_filename != filename { - operation_list.push((filepath.to_path_buf(), new_filename, data::Operation::Rename)); + operation_list.push(( + filepath.to_path_buf(), + new_filename, + data::Operation::Rename, + )); continue; } } @@ -78,7 +82,11 @@ fn main() -> std::io::Result<()> { )) } - operation_list.push((filepath.to_path_buf(), "".to_string(), data::Operation::None)); + operation_list.push(( + filepath.to_path_buf(), + "".to_string(), + data::Operation::None, + )); } if app_options.is_debug_mode() { diff --git a/src/p2tree.rs b/src/p2tree.rs index 8067f24..452c5ff 100644 --- a/src/p2tree.rs +++ b/src/p2tree.rs @@ -1,9 +1,9 @@ -use std::path::{Path,PathBuf}; use std::collections::HashMap; use std::fs::read_link; +use std::path::{Path, PathBuf}; -use nary_tree::{NodeId,TreeBuilder,Tree}; use colored::*; +use nary_tree::{NodeId, Tree, TreeBuilder}; use crate::data::Operation;