1use clap::ValueEnum;
7use webpkit::lossless::Metadata;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
11pub enum MetadataField {
12 All,
14 None,
16 Icc,
18 Exif,
20 Xmp,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Selection {
27 pub icc: bool,
29 pub exif: bool,
31 pub xmp: bool,
33}
34
35impl Selection {
36 #[must_use]
38 pub const fn all() -> Self {
39 Self {
40 icc: true,
41 exif: true,
42 xmp: true,
43 }
44 }
45
46 #[must_use]
48 pub const fn none() -> Self {
49 Self {
50 icc: false,
51 exif: false,
52 xmp: false,
53 }
54 }
55
56 #[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 #[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}