Determine stdin outside of collect_files

This commit is contained in:
Andrew 2025-12-29 11:27:37 +13:00
parent d48b147b3d
commit 5b5717e552

View file

@ -35,7 +35,7 @@ fn main() -> ExitCode {
.after_long_help("") .after_long_help("")
.get_matches_from(std::env::args()); .get_matches_from(std::env::args());
let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) { let (mut out_file, out_dir, opts) = match parse_opts_into_struct(&matches) {
Ok(x) => x, Ok(x) => x,
Err(x) => { Err(x) => {
error!("{x}"); error!("{x}");
@ -43,25 +43,31 @@ fn main() -> ExitCode {
} }
}; };
let files = collect_files( // Determine input and output
#[cfg(windows)] let file_args = matches.get_many::<PathBuf>("files").unwrap().cloned();
matches #[cfg(windows)]
.get_many::<PathBuf>("files") let inputs: Vec<_> = file_args.flat_map(apply_glob_pattern).collect();
.unwrap() #[cfg(not(windows))]
.cloned() let inputs: Vec<_> = file_args.collect();
.flat_map(apply_glob_pattern) let using_stdin = inputs.len() == 1 && inputs[0].to_str() == Some("-");
.collect(), let files = if using_stdin {
#[cfg(not(windows))] if out_dir.is_some() {
matches error!("Cannot use --dir when reading from stdin.");
.get_many::<PathBuf>("files") return ExitCode::FAILURE;
.unwrap() }
.cloned() if matches!(out_file, OutFile::Path { path: None, .. }) {
.collect(), out_file = OutFile::StdOut;
&out_dir, }
&out_file, vec![(InFile::StdIn, out_file)]
matches.get_flag("recursive"), } else {
true, collect_files(
); inputs,
&out_dir,
&out_file,
matches.get_flag("recursive"),
true,
)
};
let parallel_files = matches.get_flag("parallel-files"); let parallel_files = matches.get_flag("parallel-files");
let summary = if parallel_files { let summary = if parallel_files {
@ -98,10 +104,8 @@ fn collect_files(
top_level: bool, //explicitly specify files top_level: bool, //explicitly specify files
) -> Vec<(InFile, OutFile)> { ) -> Vec<(InFile, OutFile)> {
let mut in_out_pairs = Vec::new(); let mut in_out_pairs = Vec::new();
let allow_stdin = top_level && files.len() == 1;
for input in files { for input in files {
let using_stdin = allow_stdin && input.to_str() == Some("-"); if input.is_dir() {
if !using_stdin && input.is_dir() {
if recursive { if recursive {
match input.read_dir() { match input.read_dir() {
Ok(dir) => { Ok(dir) => {
@ -118,6 +122,15 @@ fn collect_files(
} }
continue; continue;
} }
// Skip non png files if not given on top level
if !top_level && {
let extension = input.extension().map(OsStr::to_ascii_lowercase);
extension != Some(OsString::from("png")) && extension != Some(OsString::from("apng"))
} {
continue;
}
let out_file = let out_file =
if let (Some(out_dir), &OutFile::Path { preserve_attrs, .. }) = (out_dir, out_file) { if let (Some(out_dir), &OutFile::Path { preserve_attrs, .. }) = (out_dir, out_file) {
let path = Some(out_dir.join(input.file_name().unwrap())); let path = Some(out_dir.join(input.file_name().unwrap()));
@ -128,19 +141,7 @@ fn collect_files(
} else { } else {
(*out_file).clone() (*out_file).clone()
}; };
let in_file = if using_stdin { let in_file = InFile::Path(input);
InFile::StdIn
} else {
// Skip non png files if not given on top level
if !top_level && {
let extension = input.extension().map(OsStr::to_ascii_lowercase);
extension != Some(OsString::from("png"))
&& extension != Some(OsString::from("apng"))
} {
continue;
}
InFile::Path(input)
};
in_out_pairs.push((in_file, out_file)); in_out_pairs.push((in_file, out_file));
} }
in_out_pairs in_out_pairs