add support for glob matchers

This commit is contained in:
2020-11-28 18:19:43 +01:00
parent 458b7b9aa1
commit 595fbbb3ed
3 changed files with 30 additions and 9 deletions

View File

@@ -18,3 +18,4 @@ clap = "2"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8" serde_yaml = "0.8"
regex = "1" regex = "1"
globset = "0.4"

View File

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

View File

@@ -1,4 +1,5 @@
use anyhow::{Context, Error, Result}; use anyhow::{Context, Error, Result};
use globset::GlobBuilder;
use regex::bytes::{Regex, RegexBuilder}; use regex::bytes::{Regex, RegexBuilder};
use serde::Deserialize; use serde::Deserialize;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -73,7 +74,8 @@ struct ConfigFile {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct TranscodeMatchFile { struct TranscodeMatchFile {
regex: String, glob: Option<String>,
regex: Option<String>,
to: Transcode, to: Transcode,
} }
@@ -132,6 +134,11 @@ pub fn config() -> Result<Config> {
))); )));
} }
let default_regex = RegexBuilder::new("\\.flac$")
.case_insensitive(true)
.build()
.expect("failed compiling default match regex");
let transcode_matches = config_file let transcode_matches = config_file
.as_ref() .as_ref()
.map(|config_file| { .map(|config_file| {
@@ -139,11 +146,28 @@ pub fn config() -> Result<Config> {
.matches .matches
.iter() .iter()
.map(|m| { .map(|m| {
Ok(TranscodeMatch { let regex = match (&m.glob, &m.regex) {
regex: RegexBuilder::new(&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)
.case_insensitive(true) .case_insensitive(true)
.build() .build()
.context("failed compiling regex")?, .context("failed compiling regex")?,
};
Ok(TranscodeMatch {
regex,
to: m.to.clone(), to: m.to.clone(),
}) })
}) })
@@ -152,11 +176,6 @@ pub fn config() -> Result<Config> {
.transpose()? .transpose()?
.filter(|matches| !matches.is_empty()) .filter(|matches| !matches.is_empty())
.unwrap_or_else(|| { .unwrap_or_else(|| {
let default_regex = RegexBuilder::new("\\.flac$")
.case_insensitive(true)
.build()
.expect("failed compiling default match regex");
vec![TranscodeMatch { vec![TranscodeMatch {
regex: default_regex, regex: default_regex,
to: Transcode::default(), to: Transcode::default(),