Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(MINOR) Rename XmpMeta::does_property_exist to XmpMeta::contains_property #97

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ xmp_toolkit = "0.6.0"
The `XmpMeta::array_property` method has been renamed to `XmpMeta::property_array`
to make it consistent with the other typed property getters.

The `XmpMeta::does_property_exist` method has been renamed to `XmpMeta::contains_property`
for consistency with other Rust container types.

### Upgrading to 0.6 from earlier versions

The `XmpMeta::property` method has been changed to return `Option<XmpValue<String>>`
Expand Down
6 changes: 3 additions & 3 deletions src/tests/xmp_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ fn open_and_edit_file() {
m.set_property("http://purl.org/dc/terms/", "provenance", &"blah".into())
.unwrap();

assert!(m.does_property_exist("http://purl.org/dc/terms/", "provenance"));
assert!(!m.does_property_exist("http://purl.org/dc/terms/", "provenancx"));
assert!(m.contains_property("http://purl.org/dc/terms/", "provenance"));
assert!(!m.contains_property("http://purl.org/dc/terms/", "provenancx"));

if m.does_property_exist(xmp_ns::XMP, "MetadataDate") {
if m.contains_property(xmp_ns::XMP, "MetadataDate") {
let updated_time = XmpDateTime::current().unwrap();
m.set_property_date(xmp_ns::XMP, "MetadataDate", &updated_time.into())
.unwrap();
Expand Down
56 changes: 28 additions & 28 deletions src/tests/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,34 @@ mod register_namespace {
}
}

mod contains_property {
use crate::{tests::fixtures::*, xmp_ns, XmpMeta};

#[test]
fn exists() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(m.contains_property(xmp_ns::XMP, "CreatorTool"));
}

#[test]
fn doesnt_exist() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.contains_property(xmp_ns::XMP, "RandomProperty"));
}

#[test]
fn empty_namespace() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.contains_property("", "CreatorTool"));
}

#[test]
fn empty_prop_name() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.contains_property(xmp_ns::XMP, ""));
}
}

mod property {
use crate::{tests::fixtures::*, xmp_ns, XmpMeta, XmpValue};

Expand Down Expand Up @@ -991,31 +1019,3 @@ mod set_property_date {
);
}
}

mod does_property_exist {
use crate::{tests::fixtures::*, xmp_ns, XmpMeta};

#[test]
fn exists() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(m.does_property_exist(xmp_ns::XMP, "CreatorTool"));
}

#[test]
fn doesnt_exist() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.does_property_exist(xmp_ns::XMP, "RandomProperty"));
}

#[test]
fn empty_namespace() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.does_property_exist("", "CreatorTool"));
}

#[test]
fn empty_prop_name() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();
assert!(!m.does_property_exist(xmp_ns::XMP, ""));
}
}
44 changes: 22 additions & 22 deletions src/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ impl XmpMeta {
}
}

/// Returns `true` if the metadata block contains a property by this name.
///
/// ## Arguments
///
/// * `schema_ns`: The namespace URI; see [`XmpMeta::property()`].
///
/// * `prop_name`: The name of the property. Can be a general path
/// expression. Must not be an empty string. See [`XmpMeta::property()`]
/// for namespace prefix usage.
///
/// ## Error handling
///
/// Any errors (for instance, empty or invalid namespace or property name)
/// are ignored; the function will return `false` in such cases.
pub fn contains_property(&self, schema_ns: &str, prop_name: &str) -> bool {
let c_ns = CString::new(schema_ns).unwrap_or_default();
let c_name = CString::new(prop_name).unwrap_or_default();

let r = unsafe { ffi::CXmpMetaDoesPropertyExist(self.m, c_ns.as_ptr(), c_name.as_ptr()) };
r != 0
}

/// Gets a simple string property value.
///
/// When specifying a namespace and path (in this and all other accessors):
Expand Down Expand Up @@ -612,28 +634,6 @@ impl XmpMeta {

XmpError::raise_from_c(&err)
}

/// Reports whether a property currently exists.
///
/// ## Arguments
///
/// * `schema_ns`: The namespace URI; see [`XmpMeta::property()`].
///
/// * `prop_name`: The name of the property. Can be a general path
/// expression. Must not be an empty string. See [`XmpMeta::property()`]
/// for namespace prefix usage.
///
/// ## Error handling
///
/// Any errors (for instance, empty or invalid namespace or property name)
/// are ignored; the function will return `false` in such cases.
pub fn does_property_exist(&self, schema_ns: &str, prop_name: &str) -> bool {
let c_ns = CString::new(schema_ns).unwrap_or_default();
let c_name = CString::new(prop_name).unwrap_or_default();

let r = unsafe { ffi::CXmpMetaDoesPropertyExist(self.m, c_ns.as_ptr(), c_name.as_ptr()) };
r != 0
}
}

impl FromStr for XmpMeta {
Expand Down