From 4af2e1504c47a8cbafb4acbf9940dfb1dbc54679 Mon Sep 17 00:00:00 2001 From: Thomas Heck Date: Tue, 22 Dec 2020 15:22:28 +0100 Subject: [PATCH] add support for regex & glob pattern for same match entry --- example.audio-conv.yaml | 2 +- src/config.rs | 43 ++++++++++++++++++++++------------------- src/main.rs | 6 +++++- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/example.audio-conv.yaml b/example.audio-conv.yaml index 8d71829..fd3d35f 100644 --- a/example.audio-conv.yaml +++ b/example.audio-conv.yaml @@ -2,7 +2,7 @@ from: ./music to: ./converted_test matches: - glob: "**/*.flac" - # or `regex: "\.flac$"` + # and/or `regex: "\.flac$"` to: codec: opus bitrate: 160 diff --git a/src/config.rs b/src/config.rs index 6a7b17b..96a54b6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,7 @@ pub struct Config { #[derive(Clone, Debug)] pub struct TranscodeMatch { - pub regex: Regex, + pub regexes: Vec, pub to: Transcode, } @@ -160,28 +160,31 @@ pub fn config() -> Result { .matches .iter() .map(|m| { - let regex = match (&m.glob, &m.regex) { - (None, None) => default_regex.clone(), - (Some(_), Some(_)) => { - return Err(Error::msg( - "`glob` and `regex` set for matcher, there can only be one!\nhttps://www.youtube.com/watch?v=5JgAMM3ADCw", - )); - } - (Some(glob), None) => { - let glob = GlobBuilder::new(glob) - .case_insensitive(true) - .build() - .context("failed building glob")?; - Regex::new(glob.regex()).context("failed compiling regex")? - } - (None, Some(regex)) => RegexBuilder::new(regex) + let mut regexes = Vec::with_capacity(1); + + if let Some(glob) = &m.glob { + let glob = GlobBuilder::new(glob) .case_insensitive(true) .build() - .context("failed compiling regex")?, - }; + .context("failed building glob")?; + let regex = Regex::new(glob.regex()).context("failed compiling regex")?; + regexes.push(regex); + } + + if let Some(regex) = &m.regex { + let regex = RegexBuilder::new(regex) + .case_insensitive(true) + .build() + .context("failed compiling regex")?; + regexes.push(regex); + } + + if regexes.is_empty() { + regexes.push(default_regex.clone()); + } Ok(TranscodeMatch { - regex, + regexes, to: m.to.clone(), }) }) @@ -191,7 +194,7 @@ pub fn config() -> Result { .filter(|matches| !matches.is_empty()) .unwrap_or_else(|| { vec![TranscodeMatch { - regex: default_regex, + regexes: vec![default_regex], to: Transcode::default(), }] }); diff --git a/src/main.rs b/src/main.rs index f8d8b7b..6cb85ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,7 +82,11 @@ fn get_convertion_args(config: &Config) -> impl Iterator let transcode = config .matches .iter() - .filter(|m| m.regex.is_match(from_bytes.as_ref())) + .filter(|m| { + m.regexes + .iter() + .any(|regex| regex.is_match(from_bytes.as_ref())) + }) .map(|m| m.to.clone()) .next(); let transcode = if let Some(transcode) = transcode {