Skip to content

Commit

Permalink
factor with rust clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kenchou committed Dec 7, 2023
1 parent 64d253b commit d286231
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ struct PatternMatcher {
}

impl PatternMatcher {
fn from_config_file(config_file: &Path) -> Result<PatternMatcher, serde_yaml::Error> {
fn from_config_file(config_file: &Path) -> PatternMatcher {
let config = PatternsConfig::from_config_file(config_file);
let patterns_to_remove =
create_mixed_regex_list(config.remove.iter().map(AsRef::as_ref).collect()).unwrap();
let patterns_to_rename =
create_regex_list(config.cleanup.iter().map(AsRef::as_ref).collect()).unwrap();
let patterns_to_remove_with_hash = create_patterns_with_hash(config.remove_hash).unwrap();
Ok(PatternMatcher {
create_regex_list(config.cleanup.iter().map(AsRef::as_ref).collect());
let patterns_to_remove_with_hash = create_patterns_with_hash(config.remove_hash);
PatternMatcher {
patterns_to_remove,
patterns_to_remove_with_hash,
patterns_to_rename,
})
}
}

fn match_remove_pattern(&self, test_file: &str) -> (bool, Option<String>) {
Expand Down Expand Up @@ -196,8 +196,8 @@ fn create_mixed_regex_list(patterns: Vec<&str>) -> Result<Vec<Regex>, Box<dyn st
.map(|pattern| {
let pattern = pattern.trim();
// println!(">>> {:#?}", pattern);
if pattern.starts_with('/') {
Regex::new(&pattern[1..]).unwrap()
if let Some(stripped) = pattern.strip_prefix('/') {
Regex::new(stripped).unwrap()
} else {
Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap()
}
Expand All @@ -209,21 +209,19 @@ fn create_mixed_regex_list(patterns: Vec<&str>) -> Result<Vec<Regex>, Box<dyn st
/**
* 创建正则表达式列表
*/
fn create_regex_list(patterns: Vec<&str>) -> Result<Vec<Regex>, Box<dyn std::error::Error>> {
fn create_regex_list(patterns: Vec<&str>) -> Vec<Regex> {
let regex_list: Vec<Regex> = patterns
.iter()
.map(|pattern| {
// println!("---> {:#?}", pattern);
Regex::new(pattern.trim()).unwrap()
})
.collect();
Ok(regex_list)
regex_list
}

fn create_patterns_with_hash(
patterns: HashMap<String, Vec<String>>,
) -> Result<Vec<(Regex, Vec<String>)>, Box<dyn std::error::Error>> {
let patterns_to_remove_with_hash = patterns
fn create_patterns_with_hash(patterns: HashMap<String, Vec<String>>) -> Vec<(Regex, Vec<String>)> {
patterns
.into_iter()
.map(|(key, value)| {
// println!("hash --> {}", key);
Expand All @@ -232,8 +230,7 @@ fn create_patterns_with_hash(
value,
)
})
.collect();
Ok(patterns_to_remove_with_hash)
.collect()
}

fn get_guess_paths(target_path: &Path) -> Vec<PathBuf> {
Expand Down Expand Up @@ -267,7 +264,7 @@ fn guess_path(test_file: &str, mut guess_paths: Vec<PathBuf>) -> Option<PathBuf>
return Some(file_path);
}
}
None
None // return None; if found nothing in paths
}

fn dedup_vec(v: &Vec<PathBuf>) -> Vec<PathBuf> {
Expand Down Expand Up @@ -389,7 +386,7 @@ fn main() -> std::io::Result<()> {
println!("{:#?}", app_options);
}

let pattern_matcher = PatternMatcher::from_config_file(&app_options.config_file).unwrap();
let pattern_matcher = PatternMatcher::from_config_file(&app_options.config_file);
if app_options.is_debug_mode() {
println!("{:#?}", pattern_matcher);
}
Expand Down

0 comments on commit d286231

Please sign in to comment.