add support for regex & glob pattern for same match entry

This commit is contained in:
2020-12-22 15:22:28 +01:00
parent 85e6a0cbeb
commit 4af2e1504c
3 changed files with 29 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ from: ./music
to: ./converted_test to: ./converted_test
matches: matches:
- glob: "**/*.flac" - glob: "**/*.flac"
# or `regex: "\.flac$"` # and/or `regex: "\.flac$"`
to: to:
codec: opus codec: opus
bitrate: 160 bitrate: 160

View File

@@ -13,7 +13,7 @@ pub struct Config {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TranscodeMatch { pub struct TranscodeMatch {
pub regex: Regex, pub regexes: Vec<Regex>,
pub to: Transcode, pub to: Transcode,
} }
@@ -160,28 +160,31 @@ pub fn config() -> Result<Config> {
.matches .matches
.iter() .iter()
.map(|m| { .map(|m| {
let regex = match (&m.glob, &m.regex) { let mut regexes = Vec::with_capacity(1);
(None, None) => default_regex.clone(),
(Some(_), Some(_)) => { if let Some(glob) = &m.glob {
return Err(Error::msg( let glob = GlobBuilder::new(glob)
"`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)
.case_insensitive(true) .case_insensitive(true)
.build() .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 { Ok(TranscodeMatch {
regex, regexes,
to: m.to.clone(), to: m.to.clone(),
}) })
}) })
@@ -191,7 +194,7 @@ pub fn config() -> Result<Config> {
.filter(|matches| !matches.is_empty()) .filter(|matches| !matches.is_empty())
.unwrap_or_else(|| { .unwrap_or_else(|| {
vec![TranscodeMatch { vec![TranscodeMatch {
regex: default_regex, regexes: vec![default_regex],
to: Transcode::default(), to: Transcode::default(),
}] }]
}); });

View File

@@ -82,7 +82,11 @@ fn get_convertion_args(config: &Config) -> impl Iterator<Item = ConvertionArgs>
let transcode = config let transcode = config
.matches .matches
.iter() .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()) .map(|m| m.to.clone())
.next(); .next();
let transcode = if let Some(transcode) = transcode { let transcode = if let Some(transcode) = transcode {