diff --git a/Cargo.toml b/Cargo.toml index ae45cb3..6317d1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ clap = "2" serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.8" regex = "1" +globset = "0.4" diff --git a/example.audio-conv.yaml b/example.audio-conv.yaml index 3a5541e..e855202 100644 --- a/example.audio-conv.yaml +++ b/example.audio-conv.yaml @@ -1,7 +1,8 @@ from: ./music to: ./converted_test matches: - - regex: \.flac$ + - glob: "**/*.flac" + # or `regex: "\.flac$"` to: codec: opus bitrate: 160 diff --git a/src/config.rs b/src/config.rs index 4af6aa3..bc3b9c0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use anyhow::{Context, Error, Result}; +use globset::GlobBuilder; use regex::bytes::{Regex, RegexBuilder}; use serde::Deserialize; use std::path::{Path, PathBuf}; @@ -73,7 +74,8 @@ struct ConfigFile { #[derive(Debug, Deserialize)] struct TranscodeMatchFile { - regex: String, + glob: Option, + regex: Option, to: Transcode, } @@ -132,6 +134,11 @@ pub fn config() -> Result { ))); } + let default_regex = RegexBuilder::new("\\.flac$") + .case_insensitive(true) + .build() + .expect("failed compiling default match regex"); + let transcode_matches = config_file .as_ref() .map(|config_file| { @@ -139,11 +146,28 @@ pub fn config() -> Result { .matches .iter() .map(|m| { - Ok(TranscodeMatch { - regex: RegexBuilder::new(&m.regex) + 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) .case_insensitive(true) .build() .context("failed compiling regex")?, + }; + + Ok(TranscodeMatch { + regex, to: m.to.clone(), }) }) @@ -152,11 +176,6 @@ pub fn config() -> Result { .transpose()? .filter(|matches| !matches.is_empty()) .unwrap_or_else(|| { - let default_regex = RegexBuilder::new("\\.flac$") - .case_insensitive(true) - .build() - .expect("failed compiling default match regex"); - vec![TranscodeMatch { regex: default_regex, to: Transcode::default(),