Skip to main content

webpkit_cli/
metadata.rs

1//! `-metadata` selection: which ICC / Exif / XMP fields to carry.
2//!
3//! This project preserves metadata by default (kinder than cwebp, which strips
4//! it), so an unspecified `--metadata` means "keep everything the source has".
5
6use clap::ValueEnum;
7use webpkit::lossless::Metadata;
8
9/// A single choice accepted by `--metadata` (comma-separated).
10#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
11pub enum MetadataField {
12    /// Keep ICC, Exif, and XMP.
13    All,
14    /// Strip everything (a bare `VP8L` output).
15    None,
16    /// Keep the ICC color profile.
17    Icc,
18    /// Keep Exif.
19    Exif,
20    /// Keep XMP.
21    Xmp,
22}
23
24/// Which metadata fields to keep, resolved from the `--metadata` flags.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Selection {
27    /// Keep the ICC color profile.
28    pub icc: bool,
29    /// Keep Exif.
30    pub exif: bool,
31    /// Keep XMP.
32    pub xmp: bool,
33}
34
35impl Selection {
36    /// Keep all metadata — the default when `--metadata` is unspecified.
37    #[must_use]
38    pub const fn all() -> Self {
39        Self {
40            icc: true,
41            exif: true,
42            xmp: true,
43        }
44    }
45
46    /// Keep nothing.
47    #[must_use]
48    pub const fn none() -> Self {
49        Self {
50            icc: false,
51            exif: false,
52            xmp: false,
53        }
54    }
55
56    /// Fold the parsed `--metadata` values into a selection.
57    ///
58    /// An empty list keeps everything (the preserve-by-default policy). `None`
59    /// clears the accumulator; later fields re-enable individual kinds.
60    #[must_use]
61    pub fn from_fields(fields: &[MetadataField]) -> Self {
62        if fields.is_empty() {
63            return Self::all();
64        }
65        let mut sel = Self::none();
66        for field in fields {
67            match field {
68                MetadataField::All => sel = Self::all(),
69                MetadataField::None => sel = Self::none(),
70                MetadataField::Icc => sel.icc = true,
71                MetadataField::Exif => sel.exif = true,
72                MetadataField::Xmp => sel.xmp = true,
73            }
74        }
75        sel
76    }
77
78    /// Project a source [`Metadata`] down to the selected fields.
79    #[must_use]
80    pub fn apply(self, source: &Metadata) -> Metadata {
81        Metadata {
82            icc_profile: self.icc.then(|| source.icc_profile.clone()).flatten(),
83            exif: self.exif.then(|| source.exif.clone()).flatten(),
84            xmp: self.xmp.then(|| source.xmp.clone()).flatten(),
85        }
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use webpkit::lossless::Metadata;
92
93    use super::{MetadataField, Selection};
94
95    fn full() -> Metadata {
96        Metadata {
97            icc_profile: Some(vec![1]),
98            exif: Some(vec![2]),
99            xmp: Some(vec![3]),
100        }
101    }
102
103    #[test]
104    fn no_flags_preserves_everything() {
105        assert_eq!(Selection::from_fields(&[]), Selection::all());
106        assert_eq!(Selection::from_fields(&[]).apply(&full()), full());
107    }
108
109    #[test]
110    fn none_strips_all() {
111        let out = Selection::from_fields(&[MetadataField::None]).apply(&full());
112        assert!(out.is_empty());
113    }
114
115    #[test]
116    fn icc_keeps_only_icc() {
117        let out = Selection::from_fields(&[MetadataField::Icc]).apply(&full());
118        assert!(out.icc_profile.is_some() && out.exif.is_none() && out.xmp.is_none());
119    }
120
121    #[test]
122    fn none_then_field_re_enables_that_field() {
123        let out =
124            Selection::from_fields(&[MetadataField::None, MetadataField::Exif]).apply(&full());
125        assert!(out.icc_profile.is_none() && out.exif.is_some() && out.xmp.is_none());
126    }
127}