diff --git a/src/config.rs b/src/config.rs index e7f6065..968a24f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,10 +17,11 @@ use std::{ ffi::OsStr, - path::{Path, PathBuf}, + path::{Path, PathBuf}, collections::HashSet, borrow::Cow, }; use clap::{crate_name, crate_version, Arg, ArgMatches}; +use itertools::Either; use rayon::prelude::*; use serde::{Deserialize, Serialize}; @@ -383,11 +384,11 @@ impl Config { ) -> Vec { let auto_extension_filter = include_str!("../data/ffmpeg_extensions_list.txt"); - raw_paths + let (bad_extensions, valid_paths): (Vec<_>, Vec<_>) = raw_paths .into_par_iter() .filter(|path| { if path.exists() { - return true + return true; } eprintln!("Error: Path does not exist: {:?}", path); @@ -395,46 +396,64 @@ impl Config { }) .filter(|path| { if path.is_file() { - return true + return true; } eprintln!("Error: Path is not a file: {:?}", path); false }) - .filter(|path| match path.to_str() { - Some(_) => true, - None => { - eprintln!("Error: Path cannot be serialized to string: {:?}", path); - false + .filter(|path| { + if path.to_str().is_some() { + return true; } + + eprintln!("Error: Path cannot be serialized to string: {:?}", path); + false }) .filter(|path| path.file_name() != Some(OsStr::new(hash_file))) - .filter(|path| { + .filter_map(|path| { if !opt_disable_filter { + let opt_extension = path.extension(); + if auto_extension_filter .lines() - .any(|extension| { - path.extension() == Some(OsStr::new(extension)) - }) { - return true - } - - if !opt_silent && path.extension().is_some() { - eprintln!("WARNING: {:?} contains an extension which is unknown to dano. If you know this file type is acceptable to ffmpeg, you may use --disable-filter to force dano to accept its use.", path); + .any(|extension| opt_extension == Some(OsStr::new(extension))) + { + return Some(Either::Right(path.as_path())); } - return false - } + if let Some(ext) = opt_extension { + return Some(Either::Left(ext.to_string_lossy())); + } - true - }) - .map(|path| { - if opt_canonical_paths { - path.canonicalize().unwrap_or_else(|_| path.to_owned()) - } else { - path.to_owned() + return None; } - }) - .collect() + + Some(Either::Right(path.as_path())) + }).partition_map(|item|{ + item + }); + + if !opt_silent && !bad_extensions.is_empty() { + let unique: HashSet> = bad_extensions.into_iter().collect(); + + let mut buffer = String::new(); + + unique.iter().for_each(|ext| { + buffer.push_str(ext); + buffer.push_str(" "); + }); + + eprintln!("WARNING: The following are extensions which are unknown to dano: {:?}. dano has excluded all files with these extensions. If you know this file type is acceptable to ffmpeg, you may use --disable-filter to force dano to accept its use.", buffer.trim()); + } + + valid_paths.iter().map(|path| { + if opt_canonical_paths { + path.canonicalize().unwrap_or_else(|_| path.to_path_buf()) + } else { + path.to_path_buf() + } + }) + .collect() } } diff --git a/src/output.rs b/src/output.rs index 519b896..a7dca6c 100644 --- a/src/output.rs +++ b/src/output.rs @@ -80,14 +80,16 @@ impl Deref for WriteOutBundle { impl WriteOutBundle { pub fn write_out(self, config: &Config) -> DanoResult<()> { - self.into_inner().into_iter().try_for_each(|remainder_bundle| { - if !remainder_bundle.files.is_empty() { - remainder_bundle.write_out(config) - } else { - Self::print_bundle_empty(config, &remainder_bundle.remainder_type); - Ok(()) - } - }) + self.into_inner() + .into_iter() + .try_for_each(|remainder_bundle| { + if !remainder_bundle.files.is_empty() { + remainder_bundle.write_out(config) + } else { + Self::print_bundle_empty(config, &remainder_bundle.remainder_type); + Ok(()) + } + }) } fn print_bundle_empty(config: &Config, remainder_type: &RemainderType) { diff --git a/src/utility.rs b/src/utility.rs index 6762476..9864735 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -146,15 +146,12 @@ pub fn print_file_info(config: &Config, file_info: &FileInfo) -> DanoResult<()> ) } None => { - let msg = format!( - "Could not find file metadata for: {:?}\n", - file_info.path - ); + let msg = format!("Could not find file metadata for: {:?}\n", file_info.path); return Err(DanoError::new(&msg).into()); } }; - // why? b/c the writing of the file is the thing in write and dump mode and + // why? b/c the writing of the file is the thing in write and dump mode and // this fn used then is just to print info about the hash. we may wish to send to dev null match config.exec_mode { ExecMode::Print | ExecMode::Duplicates | ExecMode::Test(_) => print_out_buf(&buffer),