use std::error::Error;
use std::ffi::OsStr;
use std::fmt;
use cxx::UniquePtr;
#[cxx::bridge(namespace = "rahmen_exiv2")]
mod ffi {
unsafe extern "C++" {
include!("src/shim.h");
type Image;
fn open_image(path: &str) -> Result<UniquePtr<Image>>;
fn tag_interpreted(image: &Image, key: &str) -> Result<String>;
}
}
#[derive(Debug)]
pub struct Exiv2Error(String);
impl fmt::Display for Exiv2Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "exiv2 error: {}", self.0)
}
}
impl Error for Exiv2Error {}
pub struct Metadata {
image: UniquePtr<ffi::Image>,
}
impl Metadata {
pub fn new_from_path<P: AsRef<OsStr>>(path: P) -> Result<Self, Exiv2Error> {
let path = path
.as_ref()
.to_str()
.ok_or_else(|| Exiv2Error("path is not valid UTF-8".to_string()))?;
let image = ffi::open_image(path).map_err(|e| Exiv2Error(e.what().to_string()))?;
Ok(Self { image })
}
pub fn get_tag_interpreted_string(&self, tag: &str) -> Result<String, Exiv2Error> {
ffi::tag_interpreted(&self.image, tag).map_err(|e| Exiv2Error(e.what().to_string()))
}
}