From c018b6000db2a85c97960d0de9470cac0b07b7e7 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 14 Aug 2023 18:56:31 +0530 Subject: [PATCH] feat(config): allow specifying a custom file filter --- .gitignore | 1 + Cargo.lock | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/config.rs | 15 +++++++++++++ src/main.rs | 2 +- 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..fabfb87 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/config.toml diff --git a/Cargo.lock b/Cargo.lock index 5b2f441..7b767c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8f9420f797f2d9e935edf629310eb938a0d839f984e25327f3c7eed22300c" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.72" @@ -88,6 +97,16 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +[[package]] +name = "bstr" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -191,6 +210,7 @@ dependencies = [ "basic-toml", "dirs", "futures 0.3.28", + "globset", "serde", "serde_derive", "tokio", @@ -319,6 +339,19 @@ version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" +[[package]] +name = "globset" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", +] + [[package]] name = "hermit-abi" version = "0.3.2" @@ -545,6 +578,35 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" + [[package]] name = "rustc-demangle" version = "0.1.23" diff --git a/Cargo.toml b/Cargo.toml index 6f0c546..937bc15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ anyhow = "1.0.72" basic-toml = "0.1.4" dirs = "5.0.1" futures = "0.3.28" +globset = "0.4.13" serde = "1.0.183" serde_derive = "1.0.183" tokio = { version = "1.31.0", features = ["tokio-macros"] } diff --git a/src/config.rs b/src/config.rs index 4dbdac0..4000ccc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Result}; +use globset::Glob; use serde_derive::Deserialize; use std::path::PathBuf; use tracing::trace; @@ -14,6 +15,18 @@ pub struct Bucket<'bucket> { pub name: Option<&'bucket str>, pub sources: Vec, pub target: PathBuf, + pub file_filter: Option<&'bucket str>, +} + +impl<'a> Root<'a> { + pub fn is_match(&self, file_name: &str) -> bool { + if let Some(pattern) = self.bucket.file_filter { + if let Ok(glob) = Glob::new(pattern) { + return glob.compile_matcher().is_match(file_name); + } + }; + true + } } pub fn get_path() -> Result { @@ -48,6 +61,7 @@ mod test { "/mnt/data/Game 1/screenshots" ] target = "/home/test/screenshots" + file_filter = "*.mp4" "#; let config: Root<'_> = from_str(config)?; let bucket = &config.bucket; @@ -57,5 +71,6 @@ mod test { bucket.sources ); assert_eq!(PathBuf::from("/home/test/screenshots"), bucket.target); + assert_eq!(Some("*.mp4"), config.bucket.file_filter); } } diff --git a/src/main.rs b/src/main.rs index 6375e4a..1c9f415 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ async fn run() -> Result<()> { let name = file.name.as_path(); let exists = *file.exists; let empty = *file.size == 0; - if exists && !empty { + if exists && !empty && config.is_match(name.to_str().unwrap()) { let source = config.bucket.sources[index].join(name); let source = source.as_path(); let target = config.bucket.target.join(source.file_name().unwrap());