From 25d0685bdff3939705766c5ec958db7d134ace78 Mon Sep 17 00:00:00 2001 From: andrews05 Date: Tue, 26 Sep 2023 20:52:51 +1300 Subject: [PATCH] Remove `backup` and `check` options (#546) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tidy up the API by removing a couple of options we don't really need. Backup was discussed in #542 Check was discussed in #439 @shssoichiro Just say if you prefer to keep either of these 🙂 --- src/lib.rs | 33 +-------------------------------- src/main.rs | 17 ++++++----------- 2 files changed, 7 insertions(+), 43 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c0c53e4a..2530c310 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ use log::{debug, info, trace, warn}; use rayon::prelude::*; use std::borrow::Cow; use std::fmt; -use std::fs::{copy, File, Metadata}; +use std::fs::{File, Metadata}; use std::io::{stdin, stdout, BufWriter, Read, Write}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -147,18 +147,10 @@ pub type PngResult = Result; #[derive(Clone, Debug)] /// Options controlling the output of the `optimize` function pub struct Options { - /// Whether the input file should be backed up before writing the output. - /// - /// Default: `false` - pub backup: bool, /// Attempt to fix errors when decoding the input file rather than returning an `Err`. /// /// Default: `false` pub fix_errors: bool, - /// Don't actually run any optimizations, just parse the PNG file. - /// - /// Default: `false` - pub check: bool, /// Write to output even if there was no improvement in compression. /// /// Default: `false` @@ -307,8 +299,6 @@ impl Default for Options { fn default() -> Options { // Default settings based on -o 2 from the CLI interface Options { - backup: false, - check: false, fix_errors: false, force: false, filter: indexset! {RowFilter::None, RowFilter::Sub, RowFilter::Entropy, RowFilter::Bigrams}, @@ -462,11 +452,6 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<( let mut png = PngData::from_slice(&in_data, opts)?; - if opts.check { - info!("Running in check mode, not optimizing"); - return Ok(()); - } - // Run the optimizer on the decoded PNG. let mut optimized_output = optimize_png(&mut png, &in_data, opts, deadline)?; @@ -516,9 +501,6 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<( .as_ref() .map(|p| p.as_path()) .unwrap_or_else(|| input.path().unwrap()); - if opts.backup { - perform_backup(output_path)?; - } let out_file = File::create(output_path).map_err(|err| { PngError::new(&format!( "Unable to write to file {}: {}", @@ -998,19 +980,6 @@ fn is_fully_optimized(original_size: usize, optimized_size: usize, opts: &Option original_size <= optimized_size && !opts.force } -fn perform_backup(input_path: &Path) -> PngResult<()> { - let backup_file = input_path.with_extension(format!( - "bak.{}", - input_path.extension().unwrap().to_str().unwrap() - )); - copy(input_path, &backup_file).map(|_| ()).map_err(|_| { - PngError::new(&format!( - "Unable to write to backup file at {}", - backup_file.display() - )) - }) -} - #[cfg(not(unix))] fn copy_permissions(metadata_input: &Metadata, out_file: &File) -> PngResult<()> { let readonly_input = metadata_input.permissions().readonly(); diff --git a/src/main.rs b/src/main.rs index db1eabe2..94c0cfc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,7 @@ fn main() { .help("Back up modified files") .short('b') .long("backup") + .hide(true) .action(ArgAction::SetTrue), ) .arg( @@ -103,13 +104,6 @@ fn main() { .long("preserve") .action(ArgAction::SetTrue), ) - .arg( - Arg::new("check") - .help("Do not run any optimization passes") - .short('c') - .long("check") - .action(ArgAction::SetTrue), - ) .arg( Arg::new("pretend") .help("Do not write any files, only calculate compression gains") @@ -299,6 +293,11 @@ Heuristic filter selection strategies: ) .get_matches_from(std::env::args()); + if matches.get_flag("backup") { + eprintln!("The --backup flag is no longer supported. Please use --out or --dir to preserve your existing files."); + exit(1) + } + let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) { Ok(x) => x, Err(x) => { @@ -492,14 +491,10 @@ fn parse_opts_into_struct( opts.fast_evaluation = matches.get_flag("fast"); } - opts.backup = matches.get_flag("backup"); - opts.force = matches.get_flag("force"); opts.fix_errors = matches.get_flag("fix"); - opts.check = matches.get_flag("check"); - opts.bit_depth_reduction = !matches.get_flag("no-bit-reduction"); opts.color_type_reduction = !matches.get_flag("no-color-reduction");