From 22e90479d81313f6af1f96c90a837aaa1c860fe8 Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:39:02 +0200 Subject: [PATCH 1/6] Changed wild to glob --- Cargo.lock | 1 + Cargo.toml | 6 +++++- src/main.rs | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa4dc97c..d94955d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -471,6 +471,7 @@ dependencies = [ "clap", "crossbeam-channel", "filetime", + "glob", "image", "indexmap", "libdeflater", diff --git a/Cargo.toml b/Cargo.toml index 20f25ffc..9b64fcec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,10 @@ version = "4.3.8" optional = true version = "2.1.0" +[dependencies.glob] +optional = true +version = "0.3.1" + [dependencies.image] optional = true default-features = false @@ -66,7 +70,7 @@ version = "0.24.6" rustc_version = "0.4.0" [features] -binary = ["clap", "wild", "stderrlog"] +binary = ["clap", "wild", "glob", "stderrlog"] default = ["binary", "filetime", "parallel", "zopfli"] parallel = ["rayon", "indexmap/rayon", "crossbeam-channel"] freestanding = ["libdeflater/freestanding"] diff --git a/src/main.rs b/src/main.rs index 6b536fc0..2ae6ff57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -295,7 +295,7 @@ Heuristic filter selection strategies: 8 => BigEnt Highest Shannon entropy of bigrams 9 => Brute Smallest compressed size (slow)", ) - .get_matches_from(wild::args()); + .get_matches(); let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) { Ok(x) => x, @@ -306,6 +306,14 @@ Heuristic filter selection strategies: }; let files = collect_files( + #[cfg(windows)] + matches + .get_many::("files") + .unwrap() + .cloned() + .flat_map(apply_glob_pattern) + .collect(), + #[cfg(not(windows))] matches .get_many::("files") .unwrap() @@ -383,6 +391,20 @@ fn collect_files( in_out_pairs } +#[cfg(target_os = "windows")] +fn apply_glob_pattern(path: PathBuf) -> Vec { + let mut paths = vec![]; + let input_path = path.to_str().unwrap(); + glob::glob(input_path) + .expect("Failed to read glob pattern") + .for_each(|path| { + let path = path.unwrap(); + paths.push(path) + }); + + paths +} + fn parse_opts_into_struct( matches: &ArgMatches, ) -> Result<(OutFile, Option, Options), String> { From 3ad5d0de2149a15faf08781d607abc123233f5e1 Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:44:43 +0200 Subject: [PATCH 2/6] Make code more readable --- src/main.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2ae6ff57..d8d14e9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -391,18 +391,14 @@ fn collect_files( in_out_pairs } -#[cfg(target_os = "windows")] +#[cfg(windows)] fn apply_glob_pattern(path: PathBuf) -> Vec { - let mut paths = vec![]; let input_path = path.to_str().unwrap(); + glob::glob(input_path) .expect("Failed to read glob pattern") - .for_each(|path| { - let path = path.unwrap(); - paths.push(path) - }); - - paths + .map(|path| path.unwrap()) + .collect() } fn parse_opts_into_struct( From def07f9fa2a9b20296600416f7db8c170710f322 Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:46:49 +0200 Subject: [PATCH 3/6] Removed unused wild crate --- Cargo.lock | 10 ---------- Cargo.toml | 6 +----- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d94955d5..e715cf5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -481,7 +481,6 @@ dependencies = [ "rustc-hash", "rustc_version", "stderrlog", - "wild", "zopfli", ] @@ -646,15 +645,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "wild" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b116685a6be0c52f5a103334cbff26db643826c7b3735fc0a3ba9871310a74" -dependencies = [ - "glob", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 9b64fcec..1fb31ed7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,10 +52,6 @@ version = "1.7.0" optional = true version = "4.3.8" -[dependencies.wild] -optional = true -version = "2.1.0" - [dependencies.glob] optional = true version = "0.3.1" @@ -70,7 +66,7 @@ version = "0.24.6" rustc_version = "0.4.0" [features] -binary = ["clap", "wild", "glob", "stderrlog"] +binary = ["clap", "glob", "stderrlog"] default = ["binary", "filetime", "parallel", "zopfli"] parallel = ["rayon", "indexmap/rayon", "crossbeam-channel"] freestanding = ["libdeflater/freestanding"] From c411ab30d4a73c4bdf4f8ff735b56213c2d57837 Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Wed, 12 Jul 2023 09:51:42 +0200 Subject: [PATCH 4/6] Updated glob dependency to windows only --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1fb31ed7..e86229c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ version = "1.7.0" optional = true version = "4.3.8" -[dependencies.glob] +[target.'cfg(windows)'.dependencies.glob] optional = true version = "0.3.1" From e87920b736be00f1d562a125b6c4a78872a75b09 Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:00:55 +0200 Subject: [PATCH 5/6] Added error handling --- src/main.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index d8d14e9e..0dad90c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -310,8 +310,12 @@ Heuristic filter selection strategies: matches .get_many::("files") .unwrap() - .cloned() - .flat_map(apply_glob_pattern) + .flat_map(|path| { + apply_glob_pattern(path).unwrap_or_else(|e| { + warn!("{path:?}: {e}"); + vec![] + }) + }) .collect(), #[cfg(not(windows))] matches @@ -392,13 +396,13 @@ fn collect_files( } #[cfg(windows)] -fn apply_glob_pattern(path: PathBuf) -> Vec { - let input_path = path.to_str().unwrap(); +fn apply_glob_pattern(path: &std::path::Path) -> Result, String> { + let input_path = path.to_str().ok_or("Failed to read path.".to_string())?; - glob::glob(input_path) - .expect("Failed to read glob pattern") - .map(|path| path.unwrap()) - .collect() + Ok(glob::glob(input_path) + .map_err(|e| e.to_string())? + .flatten() + .collect()) } fn parse_opts_into_struct( From 8124a6d2d721dbf7f810faeb81b993592741578d Mon Sep 17 00:00:00 2001 From: SalOne22 <111443297+SalOne22@users.noreply.github.com> Date: Thu, 13 Jul 2023 11:29:08 +0200 Subject: [PATCH 6/6] Updated apply_glob_pattern function --- src/main.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0dad90c4..72f934c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -295,7 +295,7 @@ Heuristic filter selection strategies: 8 => BigEnt Highest Shannon entropy of bigrams 9 => Brute Smallest compressed size (slow)", ) - .get_matches(); + .get_matches_from(std::env::args()); let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) { Ok(x) => x, @@ -310,12 +310,8 @@ Heuristic filter selection strategies: matches .get_many::("files") .unwrap() - .flat_map(|path| { - apply_glob_pattern(path).unwrap_or_else(|e| { - warn!("{path:?}: {e}"); - vec![] - }) - }) + .cloned() + .flat_map(apply_glob_pattern) .collect(), #[cfg(not(windows))] matches @@ -396,13 +392,16 @@ fn collect_files( } #[cfg(windows)] -fn apply_glob_pattern(path: &std::path::Path) -> Result, String> { - let input_path = path.to_str().ok_or("Failed to read path.".to_string())?; - - Ok(glob::glob(input_path) - .map_err(|e| e.to_string())? - .flatten() - .collect()) +fn apply_glob_pattern(path: PathBuf) -> Vec { + let matches = path + .to_str() + .and_then(|pattern| glob::glob(pattern).ok()) + .map(|paths| paths.flatten().collect::>()); + + match matches { + Some(paths) if !paths.is_empty() => paths, + _ => vec![path], + } } fn parse_opts_into_struct(