Only compile the regex once

This commit is contained in:
Daan Boerlage 2022-08-29 03:00:05 +02:00
parent ca87d5391f
commit 8d9a2329be
Signed by: daan
GPG key ID: FCE070E1E4956606

View file

@ -5,6 +5,7 @@ mod utils;
use std::fmt::{Display, Formatter};
use eyre::Result;
use clap::Parser;
use regex::Regex;
use walkdir::{DirEntry, WalkDir};
use args::Args;
@ -47,9 +48,11 @@ fn main() -> Result<()> {
error: 0,
};
let regex = Regex::new(SERIES_REGEX).unwrap();
for entry in WalkDir::new(params.path) {
if let Ok(file) = entry {
check_file(file, &mut stats, params.no_emoji, params.show_success);
check_file(file, &mut stats, &regex, params.no_emoji, params.show_success);
}
}
@ -58,11 +61,11 @@ fn main() -> Result<()> {
Ok(())
}
fn check_file(file: DirEntry, stats: &mut Stats, no_emoji: bool, show_success: bool) -> () {
fn check_file(file: DirEntry, stats: &mut Stats, regex: &Regex, no_emoji: bool, show_success: bool) -> () {
let file_type = file.file_type();
if file_type.is_file() {
let filename = file.file_name().to_str().unwrap();
match lint_file_name(&file, filename) {
match lint_file_name(&file, filename, regex) {
ComplianceStatus::NotMatched => {
stats.files += 1;
stats.error += 1;
@ -87,7 +90,7 @@ fn check_file(file: DirEntry, stats: &mut Stats, no_emoji: bool, show_success: b
}
}
fn lint_file_name(file: &DirEntry, filename: &str) -> ComplianceStatus {
fn lint_file_name(file: &DirEntry, filename: &str, regex: &Regex) -> ComplianceStatus {
if let Some(ext) = file.path().extension() {
if !VIDEO_EXTENSIONS.contains(&ext.to_str().unwrap()) {
return ComplianceStatus::Ignored;
@ -96,7 +99,7 @@ fn lint_file_name(file: &DirEntry, filename: &str) -> ComplianceStatus {
return ComplianceStatus::Ignored;
}
let captures = regex::Regex::new(SERIES_REGEX).unwrap().captures(filename);
let captures = regex.captures(filename);
if captures.is_none() {
return ComplianceStatus::NotMatched;