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

Add XmpMeta::array_len #122

Merged
merged 1 commit into from
Oct 30, 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
23 changes: 23 additions & 0 deletions src/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,29 @@ extern "C" {
#endif
}

bool CXmpMetaCountArrayItems(CXmpMeta* m,
CXmpError* outError,
const char* arrayNS,
const char* arrayName,
AdobeXMPCommon::uint32* outValue) {
*outValue = 0;

#ifndef NOOP_FFI
try {
*outValue = m->m.CountArrayItems(arrayNS, arrayName);
return true;
}
catch (XMP_Error& e) {
copyErrorForResult(e, outError);
}
catch (...) {
signalUnknownError(outError);
}
#endif

return false;
}

const char* CXmpMetaGetStructField(CXmpMeta* m,
CXmpError* outError,
const char* schemaNS,
Expand Down
8 changes: 8 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ extern "C" {
item_options: u32,
);

pub(crate) fn CXmpMetaCountArrayItems(
meta: *const CXmpMeta,
out_error: *mut CXmpError,
array_ns: *const c_char,
array_name: *const c_char,
count: *mut u32,
) -> bool;

pub(crate) fn CXmpMetaSetStructField(
meta: *mut CXmpMeta,
out_error: *mut CXmpError,
Expand Down
43 changes: 43 additions & 0 deletions src/tests/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,49 @@ mod append_array_item {
}
}

mod array_len {
use std::str::FromStr;

use crate::{tests::fixtures::*, XmpMeta};

#[test]
fn happy_path_creator_seq() {
let m = XmpMeta::from_str(PURPLE_SQUARE_XMP).unwrap();
assert_eq!(
m.array_len("http://purl.org/dc/elements/1.1/", "creator"),
1
);
}

#[test]
fn happy_path_creator_bag() {
let m = XmpMeta::from_str(PURPLE_SQUARE_XMP).unwrap();
assert_eq!(
m.array_len("http://purl.org/dc/elements/1.1/", "subject"),
6
);
}

#[test]
fn init_fail() {
let m = XmpMeta::new_fail();
assert_eq!(
m.array_len("http://purl.org/dc/elements/1.1/", "creator"),
0
);
}

#[test]
fn no_such_property() {
let m = XmpMeta::from_str(PURPLE_SQUARE_XMP).unwrap();

assert_eq!(
m.array_len("http://purl.org/dc/elements/1.1/", "creatorx"),
0
);
}
}

mod set_struct_field {
use std::str::FromStr;

Expand Down
31 changes: 31 additions & 0 deletions src/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,37 @@ impl XmpMeta {
}
}

/// Reports the number of items currently defined in an array.
///
/// ## Arguments
///
/// * `array_ns` and `array_name`: See [Accessing
/// properties](#accessing-properties).
///
/// If any error occurs (for instance, the array does not exist),
/// this function will return 0.
pub fn array_len(&self, array_ns: &str, array_name: &str) -> usize {
let mut result: u32 = 0;

if let Some(m) = self.m {
let c_array_ns = CString::new(array_ns).unwrap_or_default();
let c_array_name = CString::new(array_name.as_bytes()).unwrap_or_default();
let mut err = ffi::CXmpError::default();

unsafe {
ffi::CXmpMetaCountArrayItems(
m,
&mut err,
c_array_ns.as_ptr(),
c_array_name.as_ptr(),
&mut result,
);
}
}

result as usize
}

/// Creates or sets the value of a field within a nested structure,
/// using a string value.
///
Expand Down