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`.
This commit is contained in:
Alejandro González 2024-03-18 12:02:39 +01:00
parent d7f6757a78
commit 3ce88ebbce
No known key found for this signature in database
5 changed files with 22 additions and 12 deletions

View file

@ -1,6 +1,9 @@
use clap::{value_parser, Arg, ArgAction, Command};
use std::path::PathBuf; use std::path::PathBuf;
use clap::{value_parser, Arg, ArgAction, Command};
include!("display_chunks.rs");
pub fn build_command() -> Command { pub fn build_command() -> Command {
// Note: clap 'wrap_help' is enabled to automatically wrap lines according to terminal width. // 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, // 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(
Arg::new("strip") Arg::new("strip")
.help("Strip metadata (safe, all, or comma-separated list)\nCAUTION: 'all' will convert APNGs to standard PNGs") .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 <mode> is one of: Strip metadata chunks, where <mode> is one of:
safe => Strip all non-critical chunks, except for the following: safe => Strip all non-critical chunks, except for the following:
cICP, iCCP, sRGB, pHYs, acTL, fcTL, fdAT {}
all => Strip all non-critical chunks all => Strip all non-critical chunks
<list> => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM' <list> => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM'
CAUTION: 'all' will convert APNGs to standard PNGs. CAUTION: 'all' will convert APNGs to standard PNGs.
Note that 'bKGD', 'sBIT' and 'hIST' will be forcibly stripped if the color type or bit \ 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::<Vec<_>>()
.join(", ")))
.long("strip") .long("strip")
.value_name("mode") .value_name("mode")
.conflicts_with("strip-safe"), .conflicts_with("strip-safe"),

4
src/display_chunks.rs Normal file
View file

@ -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",
];

View file

@ -5,6 +5,7 @@ use rgb::{RGB16, RGBA8};
use crate::{ use crate::{
colors::{BitDepth, ColorType}, colors::{BitDepth, ColorType},
deflate::{crc32, inflate}, deflate::{crc32, inflate},
display_chunks::DISPLAY_CHUNKS,
error::PngError, error::PngError,
interlace::Interlacing, interlace::Interlacing,
AtomicMin, Deflaters, PngResult, AtomicMin, Deflaters, PngResult,
@ -86,18 +87,12 @@ pub enum StripChunks {
} }
impl 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 { pub(crate) fn keep(&self, name: &[u8; 4]) -> bool {
match &self { match &self {
StripChunks::None => true, StripChunks::None => true,
StripChunks::Keep(names) => names.contains(name), StripChunks::Keep(names) => names.contains(name),
StripChunks::Strip(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, StripChunks::All => false,
} }
} }

View file

@ -62,6 +62,7 @@ pub use crate::{
mod atomicmin; mod atomicmin;
mod colors; mod colors;
mod deflate; mod deflate;
mod display_chunks;
mod error; mod error;
mod evaluate; mod evaluate;
mod filters; mod filters;

View file

@ -27,6 +27,8 @@ use log::{error, warn, Level, LevelFilter};
use oxipng::{Deflaters, InFile, Options, OutFile, RowFilter, StripChunks}; use oxipng::{Deflaters, InFile, Options, OutFile, RowFilter, StripChunks};
use rayon::prelude::*; use rayon::prelude::*;
use crate::cli::DISPLAY_CHUNKS;
fn main() { fn main() {
let matches = cli::build_command() let matches = cli::build_command()
// Set the value parser for filters which isn't appropriate to do in the build_command function // 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::<Result<IndexSet<_>, _>>()?; .collect::<Result<IndexSet<_>, _>>()?;
if keep_display { if keep_display {
names.extend(StripChunks::DISPLAY.iter().cloned()); names.extend(DISPLAY_CHUNKS.iter().cloned());
} }
opts.strip = StripChunks::Keep(names) opts.strip = StripChunks::Keep(names)
} }