#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodePolicy {
pub allow_icc: Option<bool>,
pub allow_exif: Option<bool>,
pub allow_xmp: Option<bool>,
pub allow_progressive: Option<bool>,
pub allow_animation: Option<bool>,
pub allow_truncated: Option<bool>,
pub strict: Option<bool>,
}
const _: () = assert!(core::mem::size_of::<DecodePolicy>() == 7);
impl DecodePolicy {
pub const fn none() -> Self {
Self {
allow_icc: None,
allow_exif: None,
allow_xmp: None,
allow_progressive: None,
allow_animation: None,
allow_truncated: None,
strict: None,
}
}
pub const fn strict() -> Self {
Self {
allow_icc: Some(false),
allow_exif: Some(false),
allow_xmp: Some(false),
allow_progressive: Some(false),
allow_animation: Some(false),
allow_truncated: Some(false),
strict: Some(true),
}
}
pub const fn permissive() -> Self {
Self {
allow_icc: Some(true),
allow_exif: Some(true),
allow_xmp: Some(true),
allow_progressive: Some(true),
allow_animation: Some(true),
allow_truncated: Some(true),
strict: Some(false),
}
}
pub const fn with_allow_icc(mut self, v: bool) -> Self {
self.allow_icc = Some(v);
self
}
pub const fn with_allow_exif(mut self, v: bool) -> Self {
self.allow_exif = Some(v);
self
}
pub const fn with_allow_xmp(mut self, v: bool) -> Self {
self.allow_xmp = Some(v);
self
}
pub const fn with_allow_progressive(mut self, v: bool) -> Self {
self.allow_progressive = Some(v);
self
}
pub const fn with_allow_animation(mut self, v: bool) -> Self {
self.allow_animation = Some(v);
self
}
pub const fn with_allow_truncated(mut self, v: bool) -> Self {
self.allow_truncated = Some(v);
self
}
pub const fn with_strict(mut self, v: bool) -> Self {
self.strict = Some(v);
self
}
pub const fn resolve_icc(&self, default: bool) -> bool {
match self.allow_icc {
Some(v) => v,
None => default,
}
}
pub const fn resolve_exif(&self, default: bool) -> bool {
match self.allow_exif {
Some(v) => v,
None => default,
}
}
pub const fn resolve_xmp(&self, default: bool) -> bool {
match self.allow_xmp {
Some(v) => v,
None => default,
}
}
pub const fn resolve_progressive(&self, default: bool) -> bool {
match self.allow_progressive {
Some(v) => v,
None => default,
}
}
pub const fn resolve_animation(&self, default: bool) -> bool {
match self.allow_animation {
Some(v) => v,
None => default,
}
}
pub const fn resolve_truncated(&self, default: bool) -> bool {
match self.allow_truncated {
Some(v) => v,
None => default,
}
}
pub const fn resolve_strict(&self, default: bool) -> bool {
match self.strict {
Some(v) => v,
None => default,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EncodePolicy {
pub embed_icc: Option<bool>,
pub embed_exif: Option<bool>,
pub embed_xmp: Option<bool>,
}
const _: () = assert!(core::mem::size_of::<EncodePolicy>() == 3);
impl EncodePolicy {
pub const fn none() -> Self {
Self {
embed_icc: None,
embed_exif: None,
embed_xmp: None,
}
}
pub const fn strip_all() -> Self {
Self {
embed_icc: Some(false),
embed_exif: Some(false),
embed_xmp: Some(false),
}
}
pub const fn preserve_all() -> Self {
Self {
embed_icc: Some(true),
embed_exif: Some(true),
embed_xmp: Some(true),
}
}
pub const fn with_embed_icc(mut self, v: bool) -> Self {
self.embed_icc = Some(v);
self
}
pub const fn with_embed_exif(mut self, v: bool) -> Self {
self.embed_exif = Some(v);
self
}
pub const fn with_embed_xmp(mut self, v: bool) -> Self {
self.embed_xmp = Some(v);
self
}
pub const fn resolve_icc(&self, default: bool) -> bool {
match self.embed_icc {
Some(v) => v,
None => default,
}
}
pub const fn resolve_exif(&self, default: bool) -> bool {
match self.embed_exif {
Some(v) => v,
None => default,
}
}
pub const fn resolve_xmp(&self, default: bool) -> bool {
match self.embed_xmp {
Some(v) => v,
None => default,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_none_is_all_none() {
let p = DecodePolicy::none();
assert_eq!(p.allow_icc, None);
assert_eq!(p.allow_exif, None);
assert_eq!(p.allow_xmp, None);
assert_eq!(p.allow_progressive, None);
assert_eq!(p.allow_animation, None);
assert_eq!(p.allow_truncated, None);
assert_eq!(p.strict, None);
}
#[test]
fn decode_strict_denies_all() {
let p = DecodePolicy::strict();
assert_eq!(p.allow_icc, Some(false));
assert_eq!(p.allow_exif, Some(false));
assert_eq!(p.allow_animation, Some(false));
assert_eq!(p.strict, Some(true));
}
#[test]
fn decode_permissive_allows_all() {
let p = DecodePolicy::permissive();
assert_eq!(p.allow_icc, Some(true));
assert_eq!(p.allow_truncated, Some(true));
assert_eq!(p.strict, Some(false));
}
#[test]
fn decode_builder_overrides() {
let p = DecodePolicy::strict().with_allow_icc(true);
assert_eq!(p.allow_icc, Some(true));
assert_eq!(p.allow_exif, Some(false)); }
#[test]
fn decode_resolve_with_default() {
let p = DecodePolicy::none();
assert!(p.resolve_icc(true));
assert!(!p.resolve_icc(false));
let p = DecodePolicy::strict();
assert!(!p.resolve_icc(true)); }
#[test]
fn encode_none_is_all_none() {
let p = EncodePolicy::none();
assert_eq!(p.embed_icc, None);
assert_eq!(p.embed_exif, None);
assert_eq!(p.embed_xmp, None);
}
#[test]
fn encode_strip_all() {
let p = EncodePolicy::strip_all();
assert_eq!(p.embed_icc, Some(false));
assert_eq!(p.embed_exif, Some(false));
assert_eq!(p.embed_xmp, Some(false));
}
#[test]
fn encode_preserve_all() {
let p = EncodePolicy::preserve_all();
assert_eq!(p.embed_icc, Some(true));
assert_eq!(p.embed_exif, Some(true));
assert_eq!(p.embed_xmp, Some(true));
}
#[test]
fn encode_builder_overrides() {
let p = EncodePolicy::strip_all().with_embed_icc(true);
assert_eq!(p.embed_icc, Some(true));
assert_eq!(p.embed_exif, Some(false)); }
#[test]
fn encode_resolve_with_default() {
let p = EncodePolicy::none();
assert!(p.resolve_icc(true));
assert!(!p.resolve_icc(false));
let p = EncodePolicy::strip_all();
assert!(!p.resolve_icc(true));
}
#[test]
fn static_construction() {
static _DECODE: DecodePolicy = DecodePolicy::strict().with_allow_icc(true);
static _ENCODE: EncodePolicy = EncodePolicy::strip_all().with_embed_icc(true);
}
#[test]
fn default_is_none() {
assert_eq!(DecodePolicy::default(), DecodePolicy::none());
assert_eq!(EncodePolicy::default(), EncodePolicy::none());
}
}