Skip to content

Commit

Permalink
Only print one error message re unknown extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kimono-koans committed May 31, 2023
1 parent 3f50618 commit 5c59cfd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
77 changes: 48 additions & 29 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -383,58 +384,76 @@ impl Config {
) -> Vec<PathBuf> {
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);
false
})
.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<Cow<str>> = 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()
}
}
18 changes: 10 additions & 8 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 5c59cfd

Please sign in to comment.