use std::ffi::CStr;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
InitFailed(String),
Vips(String),
Other(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InitFailed(s) => write!(f, "vips init failed: {s}"),
Error::Vips(s) => write!(f, "vips error: {s}"),
Error::Other(s) => write!(f, "other error: {s}"),
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Other(e.to_string())
}
}
pub fn take_vips_error() -> Option<String> {
unsafe {
let ptr = vips_sys::vips_error_buffer();
if ptr.is_null() {
return None;
}
let msg = CStr::from_ptr(ptr).to_string_lossy().into_owned();
vips_sys::vips_error_clear();
if msg.trim().is_empty() {
None
} else {
Some(msg)
}
}
}
#[inline]
pub fn code_to_result(code: i32) -> Result<()> {
if code == 0 {
Ok(())
} else {
let msg = take_vips_error().unwrap_or_else(|| "Unknown error from libvips".to_string());
Err(Error::Vips(msg))
}
}