From 3ce88ebbcedce0fbdd6e13dc441fe77763da54e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Mon, 18 Mar 2024 12:02:39 +0100 Subject: [PATCH] Bring back generation of help strings from `DISPLAY` chunks constant This is done by extracting such a constant to a separate module file, outside of `StripChunks`. --- src/cli.rs | 16 ++++++++++++---- src/display_chunks.rs | 4 ++++ src/headers.rs | 9 ++------- src/lib.rs | 1 + src/main.rs | 4 +++- 5 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 src/display_chunks.rs diff --git a/src/cli.rs b/src/cli.rs index 5b965f3a..5b0bfee6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,9 @@ -use clap::{value_parser, Arg, ArgAction, Command}; use std::path::PathBuf; +use clap::{value_parser, Arg, ArgAction, Command}; + +include!("display_chunks.rs"); + pub fn build_command() -> Command { // Note: clap 'wrap_help' is enabled to automatically wrap lines according to terminal width. // To keep things tidy though, short help descriptions should be no more than 54 characters, @@ -118,18 +121,23 @@ Note that this will not preserve the directory structure of the input files when .arg( Arg::new("strip") .help("Strip metadata (safe, all, or comma-separated list)\nCAUTION: 'all' will convert APNGs to standard PNGs") - .long_help("\ + .long_help(format!("\ Strip metadata chunks, where is one of: safe => Strip all non-critical chunks, except for the following: - cICP, iCCP, sRGB, pHYs, acTL, fcTL, fdAT + {} all => Strip all non-critical chunks => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM' CAUTION: 'all' will convert APNGs to standard PNGs. Note that 'bKGD', 'sBIT' and 'hIST' will be forcibly stripped if the color type or bit \ -depth is changed, regardless of any options set.") +depth is changed, regardless of any options set.", + DISPLAY_CHUNKS + .iter() + .map(|c| String::from_utf8_lossy(c)) + .collect::>() + .join(", "))) .long("strip") .value_name("mode") .conflicts_with("strip-safe"), diff --git a/src/display_chunks.rs b/src/display_chunks.rs new file mode 100644 index 00000000..7a5e5187 --- /dev/null +++ b/src/display_chunks.rs @@ -0,0 +1,4 @@ +/// List of chunks that affect image display and will be kept when using the `Safe` chunk strip option +pub const DISPLAY_CHUNKS: [[u8; 4]; 7] = [ + *b"cICP", *b"iCCP", *b"sRGB", *b"pHYs", *b"acTL", *b"fcTL", *b"fdAT", +]; diff --git a/src/headers.rs b/src/headers.rs index 1035045b..c85257ae 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -5,6 +5,7 @@ use rgb::{RGB16, RGBA8}; use crate::{ colors::{BitDepth, ColorType}, deflate::{crc32, inflate}, + display_chunks::DISPLAY_CHUNKS, error::PngError, interlace::Interlacing, AtomicMin, Deflaters, PngResult, @@ -86,18 +87,12 @@ pub enum StripChunks { } impl StripChunks { - /// List of chunks that affect image display and will be kept when using the `Safe` option - // NOTE: If this list is updated, the documentation in `cli` must also be updated - pub const DISPLAY: [[u8; 4]; 7] = [ - *b"cICP", *b"iCCP", *b"sRGB", *b"pHYs", *b"acTL", *b"fcTL", *b"fdAT", - ]; - pub(crate) fn keep(&self, name: &[u8; 4]) -> bool { match &self { StripChunks::None => true, StripChunks::Keep(names) => names.contains(name), StripChunks::Strip(names) => !names.contains(name), - StripChunks::Safe => Self::DISPLAY.contains(name), + StripChunks::Safe => DISPLAY_CHUNKS.contains(name), StripChunks::All => false, } } diff --git a/src/lib.rs b/src/lib.rs index 8f9122b0..ae3d522b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,7 @@ pub use crate::{ mod atomicmin; mod colors; mod deflate; +mod display_chunks; mod error; mod evaluate; mod filters; diff --git a/src/main.rs b/src/main.rs index 42c707e9..e1a24435 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,8 @@ use log::{error, warn, Level, LevelFilter}; use oxipng::{Deflaters, InFile, Options, OutFile, RowFilter, StripChunks}; use rayon::prelude::*; +use crate::cli::DISPLAY_CHUNKS; + fn main() { let matches = cli::build_command() // Set the value parser for filters which isn't appropriate to do in the build_command function @@ -281,7 +283,7 @@ fn parse_opts_into_struct( }) .collect::, _>>()?; if keep_display { - names.extend(StripChunks::DISPLAY.iter().cloned()); + names.extend(DISPLAY_CHUNKS.iter().cloned()); } opts.strip = StripChunks::Keep(names) }