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
matches:
- glob: "**/*.flac"
# or `regex: "\.flac$"`
# and/or `regex: "\.flac$"`
to:
codec: opus
bitrate: 160

View File

@@ -13,7 +13,7 @@ pub struct Config {
#[derive(Clone, Debug)]
pub struct TranscodeMatch {
pub regex: Regex,
pub regexes: Vec<Regex>,
pub to: Transcode,
}
@@ -160,28 +160,31 @@ pub fn config() -> Result<Config> {
.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<Config> {
.filter(|matches| !matches.is_empty())
.unwrap_or_else(|| {
vec![TranscodeMatch {
regex: default_regex,
regexes: vec![default_regex],
to: Transcode::default(),
}]
});

View File

@@ -82,7 +82,11 @@ fn get_convertion_args(config: &Config) -> impl Iterator<Item = ConvertionArgs>
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 {