use std::ffi::CString;
use apple_cf::cg::CGSize;
use crate::{ffi, MediaPlayerError};
pub struct Artwork {
pub(crate) ptr: *mut core::ffi::c_void,
}
unsafe impl Send for Artwork {}
unsafe impl Sync for Artwork {}
impl std::fmt::Debug for Artwork {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Artwork")
.field("ptr", &self.ptr)
.finish()
}
}
impl Artwork {
pub fn from_path(path: &str) -> Result<Self, MediaPlayerError> {
let c_path = CString::new(path)
.map_err(|e| MediaPlayerError::InvalidArgument(e.to_string()))?;
let ptr = unsafe { ffi::mp_artwork_new_from_path(c_path.as_ptr()) };
if ptr.is_null() {
Err(MediaPlayerError::Framework(format!(
"failed to load artwork from path: {path}"
)))
} else {
Ok(Self { ptr })
}
}
pub fn from_path_with_size(path: &str, bounds_size: CGSize) -> Result<Self, MediaPlayerError> {
let c_path = CString::new(path)
.map_err(|e| MediaPlayerError::InvalidArgument(e.to_string()))?;
let ptr = unsafe {
ffi::mp_artwork_new_from_path_with_size(
c_path.as_ptr(),
bounds_size.width,
bounds_size.height,
)
};
if ptr.is_null() {
Err(MediaPlayerError::Framework(format!(
"failed to load artwork from path: {path}"
)))
} else {
Ok(Self { ptr })
}
}
}
impl Drop for Artwork {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::mp_artwork_release(self.ptr) }
}
}
}