From 856613d1fb98fa49f9f2ffcf6f12a1efde5ccff7 Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Tue, 1 Nov 2022 18:11:58 -0700 Subject: [PATCH 1/2] Replace `XmpMeta::from_str_requiring_xmp_meta` with `XmpMeta::from_str_with_options` --- src/lib.rs | 2 +- src/tests/xmp_meta.rs | 22 ++++++++++++++-------- src/xmp_meta.rs | 43 +++++++++++++++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4712fd7..5c803aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ mod xmp_value; pub use xmp_date_time::{XmpDate, XmpDateTime, XmpTime, XmpTimeZone}; pub use xmp_error::{XmpError, XmpErrorType, XmpResult}; pub use xmp_file::{OpenFileOptions, XmpFile}; -pub use xmp_meta::{ArrayProperty, ItemPlacement, ToStringOptions, XmpMeta}; +pub use xmp_meta::{ArrayProperty, FromStrOptions, ItemPlacement, ToStringOptions, XmpMeta}; pub use xmp_value::XmpValue; #[cfg(test)] diff --git a/src/tests/xmp_meta.rs b/src/tests/xmp_meta.rs index bbb4fed..8186914 100644 --- a/src/tests/xmp_meta.rs +++ b/src/tests/xmp_meta.rs @@ -147,8 +147,8 @@ mod from_str { } } -mod from_str_requiring_xmp_meta { - use crate::{tests::fixtures::*, XmpMeta, XmpValue}; +mod from_str_with_options { + use crate::{tests::fixtures::*, FromStrOptions, XmpMeta, XmpValue}; const NO_META: &str = r#" XmpResult { + pub fn from_str_with_options(s: &str, options: FromStrOptions) -> XmpResult { let mut err = ffi::CXmpError::default(); let bytes = s.as_bytes(); - let options = if require_xmp_meta { 1 } else { 0 }; - let m = unsafe { - ffi::CXmpMetaParseFromBuffer(&mut err, bytes.as_ptr(), bytes.len() as u32, options) + ffi::CXmpMetaParseFromBuffer( + &mut err, + bytes.as_ptr(), + bytes.len() as u32, + options.options, + ) }; XmpError::raise_from_c(&err)?; @@ -1862,6 +1859,32 @@ pub(crate) fn no_cpp_toolkit() -> XmpError { } } +/// Provides options for configuring the XMP parsing behavior +/// provided by [`XmpMeta::from_str_with_options`]. +#[derive(Clone, Default, Debug, Eq, PartialEq)] +pub struct FromStrOptions { + pub(crate) options: u32, +} + +impl FromStrOptions { + pub(crate) const REQUIRE_XMP_META: u32 = 0x0001; + // pub(crate) const PARSE_MORE_BUFFERS: u32 = 0x0002; + pub(crate) const STRICT_ALIASING: u32 = 0x0004; + + /// Require a surrounding `x:xmpmeta` element. + pub fn require_xmp_meta(mut self) -> Self { + self.options |= Self::REQUIRE_XMP_META; + self + } + + /// Do not reconcile alias differences. If differences are found, + /// return an `Err` result. + pub fn strict_aliasing(mut self) -> Self { + self.options |= Self::STRICT_ALIASING; + self + } +} + /// Provides options for configuring the XMP serialization behavior /// provided by [`XmpMeta::to_string_with_options`]. /// From 6a2ca67bf58b689e2a51dc5f9ea252f28b00b9a5 Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Tue, 1 Nov 2022 18:56:39 -0700 Subject: [PATCH 2/2] Add test coverage for `strict_aliasing()` case --- src/tests/fixtures/mod.rs | 24 ++++++++++++++++++++++++ src/tests/xmp_meta.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/tests/fixtures/mod.rs b/src/tests/fixtures/mod.rs index 676340a..09cffad 100644 --- a/src/tests/fixtures/mod.rs +++ b/src/tests/fixtures/mod.rs @@ -183,3 +183,27 @@ pub(crate) const LOCALIZED_TEXT_EXAMPLE: &str = r#" + + + PDF Author + XMP Author + + + + XMP Authors [1] + + + + + + DC Creator [1] + + + + + "#; diff --git a/src/tests/xmp_meta.rs b/src/tests/xmp_meta.rs index 8186914..d72d64c 100644 --- a/src/tests/xmp_meta.rs +++ b/src/tests/xmp_meta.rs @@ -148,7 +148,7 @@ mod from_str { } mod from_str_with_options { - use crate::{tests::fixtures::*, FromStrOptions, XmpMeta, XmpValue}; + use crate::{tests::fixtures::*, FromStrOptions, XmpError, XmpErrorType, XmpMeta, XmpValue}; const NO_META: &str = r#" DC Creator [1] " + ); + } + #[test] fn bad_xmp() { // TXMPMeta::ParseFromBuffer doesn't seem to throw exceptions,