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 convenience function for reading XMP from a file #46

Merged
merged 3 commits into from
Jun 22, 2022
Merged
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
61 changes: 56 additions & 5 deletions src/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
// specific language governing permissions and limitations under
// each license.

use std::ffi::{CStr, CString};
use std::{
ffi::{CStr, CString},
path::Path,
};

use crate::ffi;
use crate::xmp_date_time::XmpDateTime;
use crate::{ffi, OpenFileOptions, XmpDateTime, XmpFile, XmpFileError};

/// The `XmpMeta` struct allows access to the XMP Toolkit core services.
///
Expand Down Expand Up @@ -47,6 +49,24 @@ impl XmpMeta {
XmpMeta { m }
}

/// Reads the XMP from a file without keeping the file open.
///
/// This is a convenience function for read-only workflows.
///
/// If no XMP is found in the file, will return an empty `XmpMeta`
/// struct (i.e. same as `XmpMeta::new()`).
///
/// ## Arguments
///
/// * `path`: Path to the file to be read
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, XmpFileError> {
let mut f = XmpFile::new();

f.open_file(path, OpenFileOptions::OPEN_ONLY_XMP)?;

Ok(f.xmp().unwrap_or_else(Self::new))
}

/// Registers a namespace URI with a suggested prefix.
///
/// If the URI is not registered but the suggested prefix
Expand Down Expand Up @@ -136,7 +156,7 @@ impl XmpMeta {
}
}

/// Creates or sets a property value using an `XmpDateTeim` structure.
/// Creates or sets a property value using an `XmpDateTime` structure.
///
/// This is the simplest property setter. Use it for top-level
/// simple properties.
Expand Down Expand Up @@ -164,7 +184,7 @@ impl XmpMeta {
}
}

/// Rreports whether a property currently exists.
/// Reports whether a property currently exists.
///
/// ## Arguments
///
Expand All @@ -186,11 +206,42 @@ impl XmpMeta {
mod tests {
use super::*;

use std::{env, path::PathBuf};

fn fixture_path(name: &str) -> PathBuf {
let root_dir = &env::var("CARGO_MANIFEST_DIR").unwrap();
let mut path = PathBuf::from(root_dir);
path.push("tests/fixtures");
path.push(name);
path
}

#[test]
fn new_empty() {
let mut _m = XmpMeta::new();
}

#[test]
fn from_file() {
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap();

assert_eq!(
m.property("http://ns.adobe.com/xap/1.0/", "CreatorTool")
.unwrap(),
"Adobe Photoshop CS2 Windows"
);

assert_eq!(
m.property("http://ns.adobe.com/photoshop/1.0/", "ICCProfile")
.unwrap(),
"Dell 1905FP Color Profile"
);

assert!(m
.property("http://ns.adobe.com/photoshop/1.0/", "ICCProfilx")
.is_none());
}

#[test]
fn register_namespace() {
assert_eq!(
Expand Down