Skip to content

Commit

Permalink
Remove backup and check options (#546)
Browse files Browse the repository at this point in the history
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 🙂
  • Loading branch information
andrews05 authored Sep 26, 2023
1 parent fa47c82 commit 25d0685
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 43 deletions.
33 changes: 1 addition & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -147,18 +147,10 @@ pub type PngResult<T> = Result<T, PngError>;
#[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`
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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 {}: {}",
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 6 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn main() {
.help("Back up modified files")
.short('b')
.long("backup")
.hide(true)
.action(ArgAction::SetTrue),
)
.arg(
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 25d0685

Please sign in to comment.