WIP: rule collections instead of hardcoded rules
This commit is contained in:
parent
978bf187e0
commit
206070ed67
1 changed files with 24 additions and 20 deletions
40
src/main.rs
40
src/main.rs
|
@ -5,7 +5,7 @@ mod utils;
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use regex::Regex;
|
use regex::{Captures, Regex};
|
||||||
use walkdir::{DirEntry, WalkDir};
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
use args::Args;
|
use args::Args;
|
||||||
|
@ -109,6 +109,20 @@ fn check_file(file: DirEntry, stats: &mut Stats, ctx: &mut Context, regex: &Rege
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SERIES_RULES_WITHOUT_NAME: [fn(&str, &Captures) -> Option<NonCompliantReason>; 3] = [
|
||||||
|
DashInTitle::check,
|
||||||
|
EpisodeMarker::check,
|
||||||
|
HasFluff::check,
|
||||||
|
];
|
||||||
|
|
||||||
|
const SERIES_RULES_WITH_NAME: [fn(&str, &Captures) -> Option<NonCompliantReason>; 5] = [
|
||||||
|
DashInTitle::check,
|
||||||
|
EpisodeMarker::check,
|
||||||
|
HasFluff::check,
|
||||||
|
HasEpisodeName::check,
|
||||||
|
MissingNameSeparator::check
|
||||||
|
];
|
||||||
|
|
||||||
fn lint_file_name(file: &DirEntry, filename: &str, regex: &Regex, episode_name: bool) -> ComplianceStatus {
|
fn lint_file_name(file: &DirEntry, filename: &str, regex: &Regex, episode_name: bool) -> ComplianceStatus {
|
||||||
if let Some(ext) = file.path().extension() {
|
if let Some(ext) = file.path().extension() {
|
||||||
if !VIDEO_EXTENSIONS.contains(&ext.to_str().unwrap()) {
|
if !VIDEO_EXTENSIONS.contains(&ext.to_str().unwrap()) {
|
||||||
|
@ -126,29 +140,19 @@ fn lint_file_name(file: &DirEntry, filename: &str, regex: &Regex, episode_name:
|
||||||
|
|
||||||
let captures = captures.unwrap();
|
let captures = captures.unwrap();
|
||||||
|
|
||||||
if let Some(reason) = DashInTitle::check(filename, &captures) {
|
if episode_name {
|
||||||
return ComplianceStatus::NonCompliant(reason);
|
check_rules(SERIES_RULES_WITH_NAME, filename, &captures)
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(reason) = EpisodeMarker::check(filename, &captures) {
|
|
||||||
return ComplianceStatus::NonCompliant(reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(reason) = HasFluff::check(filename, &captures) {
|
|
||||||
return ComplianceStatus::NonCompliant(reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(reason) = HasEpisodeName::check(filename, &captures) {
|
|
||||||
return if episode_name {
|
|
||||||
ComplianceStatus::NonCompliant(reason)
|
|
||||||
} else {
|
} else {
|
||||||
ComplianceStatus::Compliant
|
check_rules(SERIES_RULES_WITHOUT_NAME, filename, &captures)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(reason) = MissingNameSeparator::check(filename, &captures) {
|
fn check_rules<const N: usize>(validation_rules: [fn(&str, &Captures) -> Option<NonCompliantReason>; N], filename: &str, captures: &Captures) -> ComplianceStatus {
|
||||||
|
for rule in validation_rules {
|
||||||
|
if let Some(reason) = rule(filename, &captures) {
|
||||||
return ComplianceStatus::NonCompliant(reason);
|
return ComplianceStatus::NonCompliant(reason);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ComplianceStatus::Compliant
|
ComplianceStatus::Compliant
|
||||||
}
|
}
|
Loading…
Reference in a new issue