Skip to main content

webpkit_cli/
cli.rs

1//! Command-line argument vocabulary shared by the binaries.
2
3pub mod brand;
4pub mod cwebp;
5pub mod dwebp;
6
7use clap::ValueEnum;
8
9/// Pixel byte order, mirroring [`webpkit::lossless::PixelLayout`].
10#[derive(Debug, Clone, Copy, Default, ValueEnum)]
11pub enum Layout {
12    /// `R, G, B, A`.
13    #[default]
14    Rgba8,
15    /// `A, R, G, B`.
16    Argb8,
17    /// `B, G, R, A`.
18    Bgra8,
19}
20
21impl From<Layout> for webpkit::lossless::PixelLayout {
22    fn from(layout: Layout) -> Self {
23        match layout {
24            Layout::Rgba8 => Self::Rgba8,
25            Layout::Argb8 => Self::Argb8,
26            Layout::Bgra8 => Self::Bgra8,
27        }
28    }
29}
30
31/// Encoder effort, mirroring [`webpkit::lossless::Effort`].
32#[derive(Debug, Clone, Copy, Default, ValueEnum)]
33pub enum Method {
34    /// Fastest: literal + subtract-green only.
35    Fast,
36    /// Balanced (the default): LZ77 + color cache.
37    #[default]
38    Balanced,
39    /// Smallest: adds Tier 3 forward transforms and meta-Huffman on top of Balanced.
40    Best,
41}
42
43// The lossless and lossy encoders now share one `Effort` dial (re-exported from
44// `webpkit` as `webpkit::Effort` / `webpkit::lossless::Effort`), so a single
45// conversion covers both `--lossless` and `--lossy` requests. Adding a preset to
46// `Effort` requires a matching variant here and an update to
47// `tests::EXPECTED_PRESETS` below (it guards against drift).
48impl From<Method> for webpkit::Effort {
49    fn from(method: Method) -> Self {
50        match method {
51            Method::Fast => Self::Fast,
52            Method::Balanced => Self::Balanced,
53            Method::Best => Self::Best,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use clap::ValueEnum;
61    use webpkit::lossless::Effort as LosslessMethod;
62
63    use super::Method;
64
65    /// The lossless presets the CLI mirrors. `webpkit::lossless::Effort` is
66    /// `#[non_exhaustive]`, so its variants cannot be enumerated from here — this
67    /// explicit list is the synchronization anchor. A new lossless preset must be
68    /// added both as a [`Method`] variant and to this array.
69    const EXPECTED_PRESETS: &[LosslessMethod] = &[
70        LosslessMethod::Fast,
71        LosslessMethod::Balanced,
72        LosslessMethod::Best,
73    ];
74
75    /// Every `cli::Method` maps onto a distinct lossless preset, and together they
76    /// cover exactly the expected set — so a preset added to only one side (or a
77    /// collapsed mapping) trips this test.
78    #[test]
79    fn cli_method_mirrors_every_lossless_preset() {
80        let mapped: Vec<LosslessMethod> = Method::value_variants()
81            .iter()
82            .map(|&m| LosslessMethod::from(m))
83            .collect();
84
85        for preset in EXPECTED_PRESETS {
86            assert_eq!(
87                mapped.iter().filter(|&m| m == preset).count(),
88                1,
89                "preset {preset:?} must be covered by exactly one cli::Method",
90            );
91        }
92        assert_eq!(mapped.len(), EXPECTED_PRESETS.len());
93    }
94}