Skip to content

Commit 3ce88eb

Browse files
committed
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`.
1 parent d7f6757 commit 3ce88eb

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/cli.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use clap::{value_parser, Arg, ArgAction, Command};
21
use std::path::PathBuf;
32

3+
use clap::{value_parser, Arg, ArgAction, Command};
4+
5+
include!("display_chunks.rs");
6+
47
pub fn build_command() -> Command {
58
// Note: clap 'wrap_help' is enabled to automatically wrap lines according to terminal width.
69
// 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
118121
.arg(
119122
Arg::new("strip")
120123
.help("Strip metadata (safe, all, or comma-separated list)\nCAUTION: 'all' will convert APNGs to standard PNGs")
121-
.long_help("\
124+
.long_help(format!("\
122125
Strip metadata chunks, where <mode> is one of:
123126
124127
safe => Strip all non-critical chunks, except for the following:
125-
cICP, iCCP, sRGB, pHYs, acTL, fcTL, fdAT
128+
{}
126129
all => Strip all non-critical chunks
127130
<list> => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM'
128131
129132
CAUTION: 'all' will convert APNGs to standard PNGs.
130133
131134
Note that 'bKGD', 'sBIT' and 'hIST' will be forcibly stripped if the color type or bit \
132-
depth is changed, regardless of any options set.")
135+
depth is changed, regardless of any options set.",
136+
DISPLAY_CHUNKS
137+
.iter()
138+
.map(|c| String::from_utf8_lossy(c))
139+
.collect::<Vec<_>>()
140+
.join(", ")))
133141
.long("strip")
134142
.value_name("mode")
135143
.conflicts_with("strip-safe"),

src/display_chunks.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// List of chunks that affect image display and will be kept when using the `Safe` chunk strip option
2+
pub const DISPLAY_CHUNKS: [[u8; 4]; 7] = [
3+
*b"cICP", *b"iCCP", *b"sRGB", *b"pHYs", *b"acTL", *b"fcTL", *b"fdAT",
4+
];

src/headers.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rgb::{RGB16, RGBA8};
55
use crate::{
66
colors::{BitDepth, ColorType},
77
deflate::{crc32, inflate},
8+
display_chunks::DISPLAY_CHUNKS,
89
error::PngError,
910
interlace::Interlacing,
1011
AtomicMin, Deflaters, PngResult,
@@ -86,18 +87,12 @@ pub enum StripChunks {
8687
}
8788

8889
impl StripChunks {
89-
/// List of chunks that affect image display and will be kept when using the `Safe` option
90-
// NOTE: If this list is updated, the documentation in `cli` must also be updated
91-
pub const DISPLAY: [[u8; 4]; 7] = [
92-
*b"cICP", *b"iCCP", *b"sRGB", *b"pHYs", *b"acTL", *b"fcTL", *b"fdAT",
93-
];
94-
9590
pub(crate) fn keep(&self, name: &[u8; 4]) -> bool {
9691
match &self {
9792
StripChunks::None => true,
9893
StripChunks::Keep(names) => names.contains(name),
9994
StripChunks::Strip(names) => !names.contains(name),
100-
StripChunks::Safe => Self::DISPLAY.contains(name),
95+
StripChunks::Safe => DISPLAY_CHUNKS.contains(name),
10196
StripChunks::All => false,
10297
}
10398
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub use crate::{
6262
mod atomicmin;
6363
mod colors;
6464
mod deflate;
65+
mod display_chunks;
6566
mod error;
6667
mod evaluate;
6768
mod filters;

src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use log::{error, warn, Level, LevelFilter};
2727
use oxipng::{Deflaters, InFile, Options, OutFile, RowFilter, StripChunks};
2828
use rayon::prelude::*;
2929

30+
use crate::cli::DISPLAY_CHUNKS;
31+
3032
fn main() {
3133
let matches = cli::build_command()
3234
// 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(
281283
})
282284
.collect::<Result<IndexSet<_>, _>>()?;
283285
if keep_display {
284-
names.extend(StripChunks::DISPLAY.iter().cloned());
286+
names.extend(DISPLAY_CHUNKS.iter().cloned());
285287
}
286288
opts.strip = StripChunks::Keep(names)
287289
}

0 commit comments

Comments
 (0)